Source

django-scm-deploytools / deploytools / management / commands / create_deploy_tools.py

import os, stat

from django.core.management.base import BaseCommand
from django.template import loader, Context

import deploytools


class Command(BaseCommand):
    def handle(self, *args, **kwargs):

        base_folder = os.getcwd()
        project_name = os.path.basename(base_folder)

        print base_folder
        print "Assuming the project's name is: {0}".format(project_name)

        #project_name = os.path.basename(base_folder)

        if not os.path.isdir(os.path.join(base_folder, 'deploy')):
            print "Making deploy folder"
            os.makedirs(os.path.join(base_folder, 'deploy'))

        c = Context({
                'project_name': project_name
        })

        t = loader.get_template('deploytools/base/deploy/nginx')
        content = t.render(c)

        fh = open(os.path.join(base_folder, 'deploy', 'nginx.conf'), 'w')
        fh.write(content)
        fh.close()

        t = loader.get_template('deploytools/base/fabfile.py')
        content = t.render(c)

        fh = open(os.path.join(base_folder, 'fabfile.py'), 'w')
        fh.write(content)
        fh.close()

        t = loader.get_template('deploytools/base/deploy/__init__.py')
        content = t.render(c)

        fh = open(os.path.join(base_folder, 'deploy', '__init__.py'), 'w')
        fh.write(content)
        fh.close()

        fh = open(
                os.path.join(
                    os.path.dirname(
                        deploytools.__file__),
                    'templates/deploytools/base/deploy/servers.py'
                ),
                'r'
            )

        content = fh.read()
        fh.close()

        fh = open(os.path.join(base_folder, 'deploy', 'servers.py'), 'w')
        fh.write(content.replace('{{project_name}}', c['project_name']))
        fh.close()

        from pip.locations import bin_py

        t = loader.get_template('deploytools/ddeploy')
        content = t.render(Context({
            'project_name': project_name,
            'python_folder': base_folder,
            'project_root': os.path.realpath(base_folder),
            'bin_folder': bin_py,
        }))

        command_path = os.path.join(bin_py, 'deploy_{0}'.format(project_name))
        print command_path
        fh = open(command_path, 'w')
        fh.write(content)
        fh.close()

        ch_value = os.stat(command_path)

        os.chmod(command_path, ch_value.st_mode | stat.S_IXUSR)

        #locations.bin_py