Source

django-envconf / management / commands / startenv.py

import os
from django.core.management.base import LabelCommand, CommandError
from django.utils.importlib import import_module
from envconf.management import utils

class Command(LabelCommand):
    help = "Creates a Django settings environment directory structure for the given environment name in the current directory."
    args = "[environmentname]"
    label = 'environment name'

    requires_model_validation = False
    # Can't import settings during this command, because they haven't
    # necessarily been created.
    can_import_settings = False

    def handle_label(self, env_name, directory = None, **options):
        if directory is None:
            directory = os.getcwd()

        project_name = os.path.basename(directory)

        if env_name == project_name:
            raise CommandError("You cannot create an environment with the same name"
                               " (%r) as your project." % env_name)

        try:
            import_module(env_name)
        except ImportError:
            pass
        else:
            raise CommandError("%r conflicts with the name of an existing Python module and cannot be used as an environment name. Please try another name." % env_name)
	os.mkdir(directory)
        utils.copy_helper(self.style, 'environment', env_name, directory)

class ProjectCommand(Command):
    help = ("Creates a Django environment directory structure for the given environment name"
            " in this project's directory.")

    def __init__(self, project_directory):
        super(ProjectCommand, self).__init__()
        self.project_directory = project_directory

    def handle_label(self, env_name, **options):
        super(ProjectCommand, self).handle_label(env_name, self.project_directory, **options)