def configure_apache(self, apache_conf_dir, httpport, sslport,
            servername_suffix, pythonpath="", secureops="secureops",
            ssl_crt=None, ssl_key=None, ssl_chain=None):
        """Configures apache to serve this Django project.
        apache_conf_dir should be apache's conf.d directory where a .conf file
        can be dropped

        httpport and sslport are used in the port part of the <VirtualHost>
        directive in the apache config file. These can be None to omit serving
        on that port/protocol.

        servername_suffix is a string that will be appended to the end of the
        project name for the apache ServerName directive

        ssl_crt and ssl_key, if specified, will be used in lieu of a self
        signed certificate.

        """
        # Check if our dest file exists, so as not to overwrite it
        config_path = os.path.join(apache_conf_dir, "opus"+self.projectname+".conf")

        # Write out a wsgi config to the project dir
        wsgi_dir = os.path.join(self.projectdir, "wsgi")
        try:
            os.mkdir(wsgi_dir)
        except __HOLE__, e:
            import errno
            if e.errno != errno.EEXIST:
                raise
            # Directory already exists, no big deal

        if pythonpath:
            ppdirective = "sys.path.append({0!r})\n".format(pythonpath)
        else:
            ppdirective = ""

        envpath = glob(os.path.join(self.projectdir, "env",
            "lib", "python*", "site-packages"))[0]

        with open(os.path.join(wsgi_dir, "django.wsgi"), 'w') as wsgi:
            wsgi.write("""
import os
import sys
import site

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
os.environ['OPUS_SETTINGS_FILE'] = {settingspath!r}
os.environ['CELERY_LOADER'] = 'django'

{additionalpaths}

# Any dependencies for installed applications go in here
site.addsitedir({envpath!r})

# Needed so that apps can import their own things without having to know the
# project name.
sys.path.append({projectpath!r})


#import django.core.handlers.wsgi
#application = django.core.handlers.wsgi.WSGIHandler()

import opus.lib.profile
application = opus.lib.profile.OpusWSGIHandler()
""".format(projectname = self.projectname,
           projectpath = self.projectdir,
           additionalpaths = ppdirective,
           settingspath = os.path.join(self.projectdir, "opussettings.json"),
           envpath = envpath
           ))


        # Discover the group under which to run the daemon proceses. Should be
        # an unpriviliged group, try to discover what that is.
        for group in ('nogroup',
                      'nobody',
                      ):
            try:
                groupinfo = grp.getgrnam(group)
            except KeyError:
                pass
            else:
                break
        else:
            raise DeploymentException("Couldn't guess the unprivileged group to use. Bailing")

        # Write out apache config
        # I know the following lines are confusing. Perhaps a TODO later would
        # be to push most of the templates out of the code
        with open(config_path, 'w') as config:
            config.write("""WSGIDaemonProcess {name} threads=4 processes=2 home={projectpath} maximum-requests=1000 user={user} group={group} display-name={projectname}
            """.format(
                    name="opus"+self.projectname,
                    user="opus"+self.projectname,
                    group=group,
                    projectname=self.projectname,
                    projectpath=self.projectdir,
                ))
            for port in (httpport, sslport):
                if not port: continue
                if port == sslport:
                    if not (ssl_crt and ssl_key):
                        ssl_crt = os.path.join(self.projectdir, "ssl.crt")
                        ssl_key = os.path.join(self.projectdir, "ssl.key")
                        ssl_chain = ""
                    else:
                        ssl_chain = "SSLCertificateChainFile " + ssl_chain
                    ssllines = """
                        SSLEngine On
                        SSLCertificateFile {0}
                        SSLCertificateKeyFile {1}
                        {2}
                        """.format(ssl_crt, ssl_key, ssl_chain)
                else:
                    ssllines = ""
                config.write("""
                    <VirtualHost {namevirtualhost}>
                        {ssllines}
                        ServerName {projectname}{servername_suffix}
                        Alias /adminmedia {adminmedia}
                        Alias /media {mediadir}
                        WSGIProcessGroup {name}
                        WSGIApplicationGroup %{{GLOBAL}}
                        WSGIScriptAlias / {wsgifile}
                        <Directory {wsgidir}>
                            Order allow,deny
                            Allow from all
                        </Directory>
                        <Directory {mediadir}>
                            Order allow,deny
                            Allow from all
                        </Directory>
                    </VirtualHost>
                    \n""".format(
                    port=port,
                    ssllines=ssllines,
                    projectname=self.projectname,
                    servername_suffix=servername_suffix,
                    mediadir=os.path.join(self.projectdir, "media"),
                    name="opus"+self.projectname,
                    namevirtualhost="*:{0}".format(port),
                    wsgidir=wsgi_dir,
                    wsgifile=os.path.join(wsgi_dir,"django.wsgi"),
                    adminmedia=os.path.join(__import__("django").__path__[0], 'contrib','admin','media'),
                    ))

        # Restart apache gracefully
        proc = subprocess.Popen([secureops,"-r"],
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT)
        output = proc.communicate()[0]
        ret = proc.wait()
        if ret:
            raise DeploymentException("Could not restart apache. {0}".format(output))
dataset/ETHPy150Open bmbouter/Opus/opus/lib/deployer/__init__.py/ProjectDeployer.configure_apache