<commit_before>##############################################################################
# Copyright 2010 North Carolina State University                             #
#                                                                            #
#   Licensed under the Apache License, Version 2.0 (the "License");          #
#   you may not use this file except in compliance with the License.         #
#   You may obtain a copy of the License at                                  #
#                                                                            #
#       http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                            #
#   Unless required by applicable law or agreed to in writing, software      #
#   distributed under the License is distributed on an "AS IS" BASIS,        #
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
#   See the License for the specific language governing permissions and      #
#   limitations under the License.                                           #
##############################################################################

"""
Django Project Deployer

The deployer library's job is to deploy a ready built Django project onto an
Apache+mod_wsgi setup. Functionality for other webservers may be added later.

"""

import os
import os.path
import subprocess
import re
import tempfile
import grp
import random
import shutil
import time

import opus
from opus.lib.conf import OpusConfig
from opus.lib.log import get_logger
import opus.lib.deployer.ssl
log = get_logger()

class DeploymentException(Exception):
    pass

class ProjectDeployer(object):
    """The project deployer. Each method performs a specific deployment action.
    The typical workflow for using the deployer is to create a deployment
    object, and call these methods in roughly this order:
    
    * secure_project() should be called first to lock down the settings and set
      permissions before any sensitive information is pushed to the
      configuration files
    * configure_database() which will set the database configuration
      parameters, pushing sensitive information into config files.
    * sync-database() which will run django's syncdb function and create the
      admin superuser for the project. This pushes sensitive information to
      the database.
    * set_paths() pushes a few absolute directory paths to the configuration
    * configure_apache() creates a wsgi entry point file and apache
      configuration file, and restarts apache. This should be the last method
      called, apache will start serving project files right after this returns.

    If using Django, the deployment model DeployedProject's deploy() method
    does all of the above.

    """

    def __init__(self, projectdir):
        """Initialize the Project Deployer with the directory root of the
        project to be deployed.

        """
        self.projectdir = projectdir

        # Find the project name by taking the last component of the path
        path = os.path.abspath(self.projectdir)
        self.projectname = os.path.basename(path)

        self.uid = None

        # Go ahead and eat up the config file into memory
        self.config = OpusConfig(os.path.join(self.projectdir, "opussettings.json"))

    def set_paths(self):
        """Sets the paths for the TEMPLATE_DIRS and the LOG_DIR
        settings
        
        """
        self.config['TEMPLATE_DIRS'] = (os.path.join(self.projectdir, "templates"),)
        self.config['LOG_DIR'] = os.path.join(self.projectdir, 'log')
        self.config['MEDIA_ROOT'] = os.path.join(self.projectdir, 'media/')
        self.config['OPUS_SECURE_UPLOADS'] = os.path.join(self.projectdir, "opus_secure_uploads/")
        self.config.save()


    def configure_database(self, engine, *args):
        """Configure the Django database
        
        engine is one of the following:
        For 'postgresql_psycopg2', 'postgresql', 'mysql', or 'oracle' the next
        five parameters should be the name, user, password, host, and port

        For 'sqlite3' no other parameters are used, an sqlite3 database is
        created automatically.
        """

        dbuser = ''
        dbpassword = ''
        dbhost = ''
        dbport = ''
        if engine == "sqlite3":
            dbname = os.path.join(self.projectdir, "sqlite", "database.sqlite")
        elif engine in ('postgresql_psycopg2', 'postgresql', 'mysql', 'oracle'):
            if len(args) < 3:
                raise TypeError("You must specify the database username and password")
            dbname = args[0]
            if not dbname:
                raise ValueError("You must specify a value for database name")
            dbuser = args[1]<commit_msg>new manage.py for deployed projects that works<commit_after>##############################################################################
# Copyright 2010 North Carolina State University                             #
#                                                                            #
#   Licensed under the Apache License, Version 2.0 (the "License");          #
#   you may not use this file except in compliance with the License.         #
#   You may obtain a copy of the License at                                  #
#                                                                            #
#       http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                            #
#   Unless required by applicable law or agreed to in writing, software      #
#   distributed under the License is distributed on an "AS IS" BASIS,        #
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
#   See the License for the specific language governing permissions and      #
#   limitations under the License.                                           #
##############################################################################

"""
Django Project Deployer

The deployer library's job is to deploy a ready built Django project onto an
Apache+mod_wsgi setup. Functionality for other webservers may be added later.

"""

import os
import os.path
import subprocess
import re
import tempfile
import grp
import random
import shutil
import time

import opus
from opus.lib.conf import OpusConfig
from opus.lib.log import get_logger
import opus.lib.deployer.ssl
log = get_logger()

class DeploymentException(Exception):
    pass

class ProjectDeployer(object):
    """The project deployer. Each method performs a specific deployment action.
    The typical workflow for using the deployer is to create a deployment
    object, and call these methods in roughly this order:
    
    * secure_project() should be called first to lock down the settings and set
      permissions before any sensitive information is pushed to the
      configuration files
    * configure_database() which will set the database configuration
      parameters, pushing sensitive information into config files.
    * sync-database() which will run django's syncdb function and create the
      admin superuser for the project. This pushes sensitive information to
      the database.
    * set_paths() pushes a few absolute directory paths to the configuration
    * configure_apache() creates a wsgi entry point file and apache
      configuration file, and restarts apache. This should be the last method
      called, apache will start serving project files right after this returns.

    If using Django, the deployment model DeployedProject's deploy() method
    does all of the above.

    """

    def __init__(self, projectdir):
        """Initialize the Project Deployer with the directory root of the
        project to be deployed.

        """
        self.projectdir = projectdir

        # Find the project name by taking the last component of the path
        path = os.path.abspath(self.projectdir)
        self.projectname = os.path.basename(path)

        self.uid = None

        # Go ahead and eat up the config file into memory
        self.config = OpusConfig(os.path.join(self.projectdir, "opussettings.json"))

    def set_paths(self):
        """Sets the paths for the TEMPLATE_DIRS and the LOG_DIR
        settings
        
        """
        self.config['TEMPLATE_DIRS'] = (os.path.join(self.projectdir, "templates"),)
        self.config['LOG_DIR'] = os.path.join(self.projectdir, 'log')
        self.config['MEDIA_ROOT'] = os.path.join(self.projectdir, 'media/')
        self.config['OPUS_SECURE_UPLOADS'] = os.path.join(self.projectdir, "opus_secure_uploads/")
        self.config.save()

        # Set a new manage.py with appropriate paths
        manage = """#!/usr/bin/env python
from django.core.management import execute_from_command_line

import os, os.path
import sys
projectpath = {projectdir!r}
sys.path.append(projectpath)
sys.path.append({opuspath!r})
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
os.environ['OPUS_SETTINGS_FILE'] = os.path.join(projectpath, 'opussettings.json')
try:
    open(os.path.join(projectpath, "settings.py"), 'r')
except IOError, e:
    sys.stderr.write("Error: Can't open the file 'settings.py' in %r.\\n" % projectpath)
    sys.stderr.write(str(e)+'\\n')
    sys.exit(1)

if __name__ == "__main__":
    execute_from_command_line()
"""
        with open(os.path.join(self.projectdir, "manage.py"), 'w') as manageout:
            manageout.write(manage.format(
                projectdir=self.projectdir,
                opuspath=os.path.dirname(opus.__path__[0])))
        

    def configure_database(self, engine, *args):
        """Configure the Django database
        
        engine is one of the following:
        For 'postgresql_psycopg2', 'postgresql', 'mysql', or 'oracle' the next
        five parameters should be the name, user, password, host, and port

        For 'sqlite3' no other parameters are used, an sqlite3 database is
        created automatically.
        """

        dbuser = ''
        dbpassword = ''
        dbhost = ''
        dbport = ''
        if engine == "sqlite3":
            dbname = os.path.join(self.projectdir, "sqlite", "database.sqlite")
        elif engine in ('postgresql_psycopg2', 'postgresql', 'mysql', 'oracle'):
            if len(args) < 3:
                raise TypeError("You must specify the database username and password")
            dbname = args[0]
            if not dbname:
                raise ValueError("You must specify a value for database name")
            dbuser = args[1]
{'fraction_non_alphanumeric': 0.10469483568075118, 'fraction_numerical': 0.003192488262910798, 'mean_word_length': 2.3409661229611043, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2463455', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2599, 'n_words': 986}