Beispiel #1
0
def _split_project():
    global args
    global profiles
    global projectname
    global settings

    profiles = ('develop', 'staging', 'production')
    filenames = ('__init__', 'common') + profiles

    _print_verbose(2, 'Creating directories ...')
    os.mkdir('%s.media' % projectname)
    os.mkdir('%s.static' % projectname)
    os.mkdir('%s.templates' % projectname)
    os.mkdir(os.path.join(projectname, 'settings'))

    _print_verbose(
        2, 'Converting settings to deployment profiles (%s) ...' %
        ', '.join(profiles))
    os.rename(os.path.join(projectname, 'settings.py'),
              os.path.join(projectname, 'settings', 'common.py'))

    settings = DjangoSettingsManager(projectname, *filenames)
    settings.append_lines(
        '__init__', '"""',
        'Modularized settings generated by django Organice setup. http://organice.io',
        'This solution follows the second recommendation from',
        'http://www.sparklewise.com/django-settings-for-production-and-development-best-practices/',
        '"""', 'from .develop import *  # noqa')
    for prof in profiles:
        settings.append_lines(
            prof,
            '# Django project settings for %s environment' % prof.capitalize(),
            '', 'from .common import *  # noqa')

    # out-of-the-box Django values relevant for deployment
    settings.delete_var('common', 'SITE_ID')
    settings.insert_lines('common', '_ = lambda s: s', '', 'SITE_ID = 1')
    settings.replace_line('common', 'import os',
                          'from os.path import abspath, dirname, join')
    settings.set_value('common', 'BASE_DIR',
                       'dirname(dirname(dirname(abspath(__file__))))')
    settings.set_value('common', 'MEDIA_ROOT',
                       "join(BASE_DIR, '%s.media')" % projectname)
    settings.set_value('common', 'STATIC_ROOT',
                       "join(BASE_DIR, '%s.static')" % projectname)
    settings.set_value('common', 'MEDIA_URL', "'/media/'")
    settings.set_value('common', 'USE_I18N', False)
    settings.move_var('common', profiles, 'DEBUG')
    settings.move_var('common', profiles, 'ALLOWED_HOSTS')
    settings.move_var('common', profiles, 'DATABASES')
    settings.move_var('common', profiles, 'MEDIA_ROOT')
    settings.move_var('common', profiles, 'STATIC_ROOT')
    settings.move_var('common', profiles, 'SECRET_KEY')
    for prof in ('staging', 'production'):
        settings.set_value(prof, 'DEBUG', False)
        settings.set_value_lines(
            prof, 'ALLOWED_HOSTS', '[', "    '%s.organice.io'," %
            (args.account if args.account else projectname),
            "    '%s'," % (args.domain if args.domain else 'www.example.com'),
            ']')
def _split_project():
    global args
    global profiles
    global projectname
    global settings

    profiles = ('develop', 'staging', 'production')
    filenames = ('__init__', 'common') + profiles

    _print_verbose(2, 'Creating directories ...')
    os.mkdir('%s.media' % projectname)
    os.mkdir('%s.static' % projectname)
    os.mkdir('%s.templates' % projectname)
    os.mkdir(os.path.join(projectname, 'settings'))

    _print_verbose(2, 'Converting settings to deployment profiles (%s) ...' % ', '.join(profiles))
    os.rename(os.path.join(projectname, 'settings.py'),
              os.path.join(projectname, 'settings', 'common.py'))

    settings = DjangoSettingsManager(projectname, *filenames)
    settings.append_lines('__init__',
                          '"""',
                          'Modularized settings generated by django Organice setup. http://organice.io',
                          'This solution follows the second recommendation from',
                          'http://www.sparklewise.com/django-settings-for-production-and-development-best-practices/',
                          '"""',
                          'from .develop import *  # noqa')
    for prof in profiles:
        settings.append_lines(prof,
                              '# Django project settings for %s environment' % prof.capitalize(),
                              '',
                              'from .common import *  # noqa')

    # out-of-the-box Django values relevant for deployment
    settings.delete_var('common', 'SITE_ID')
    settings.insert_lines('common',
                          '_ = lambda s: s',
                          '',
                          'SITE_ID = 1')
    settings.replace_line('common', 'import os', 'from os.path import abspath, dirname, join')
    settings.set_value('common', 'BASE_DIR', 'dirname(dirname(dirname(abspath(__file__))))')
    settings.set_value('common', 'MEDIA_ROOT', "join(BASE_DIR, '%s.media')" % projectname)
    settings.set_value('common', 'STATIC_ROOT', "join(BASE_DIR, '%s.static')" % projectname)
    settings.set_value('common', 'MEDIA_URL', "'/media/'")
    settings.set_value('common', 'USE_I18N', False)
    settings.move_var('common', profiles, 'DEBUG')
    settings.move_var('common', profiles, 'ALLOWED_HOSTS')
    settings.move_var('common', profiles, 'DATABASES')
    settings.move_var('common', profiles, 'MEDIA_ROOT')
    settings.move_var('common', profiles, 'STATIC_ROOT')
    settings.move_var('common', profiles, 'SECRET_KEY')
    for prof in ('staging', 'production'):
        settings.set_value(prof, 'DEBUG', False)
        settings.set_value_lines(prof, 'ALLOWED_HOSTS', '[',
                                 "    '%s.organice.io'," % (args.account if args.account else projectname),
                                 "    '%s'," % (args.domain if args.domain else 'www.example.com'),
                                 ']')
def _split_project():
    global args
    global profiles
    global settings

    profiles = ('develop', 'staging', 'production')
    filenames = ('__init__', 'common') + profiles

    _print_verbose(2, 'Creating directories ...')
    os.mkdir('%s.media' % args.projectname)
    os.mkdir('%s.static' % args.projectname)
    os.mkdir('%s.templates' % args.projectname)
    os.mkdir(os.path.join(args.projectname, 'settings'))

    _print_verbose(
        2, 'Converting settings to deployment profiles (%s) ...' %
        ', '.join(profiles))
    os.rename(os.path.join(args.projectname, 'settings.py'),
              os.path.join(args.projectname, 'settings', 'common.py'))

    settings = DjangoSettingsManager(args.projectname, *filenames)
    settings.append_lines(
        '__init__', '"""',
        'Modularized settings generated by django Organice setup. http://organice.io',
        'This solution follows the second recommendation from',
        'http://www.sparklewise.com/django-settings-for-production-and-development-best-practices/',
        '"""', 'from .develop import *  # noqa')
    for prof in profiles:
        settings.append_lines(
            prof,
            '# Django project settings for %s environment' % prof.capitalize(),
            '', 'from .common import *  # noqa')

    # out-of-the-box Django values relevant for deployment
    settings.delete_var('common', 'SITE_ID')
    settings.insert_lines(
        'common', 'from django.utils.translation import ugettext_lazy as _',
        '', 'SITE_ID = 1')
    settings.replace_line('common', 'import os', '')
    settings.insert_lines('common',
                          'from os.path import abspath, dirname, join')
    settings.set_value('common', 'BASE_DIR',
                       'dirname(dirname(dirname(abspath(__file__))))')
    settings.set_value('common', 'MEDIA_ROOT',
                       "join(BASE_DIR, '%s.media')" % args.projectname)
    settings.set_value('common', 'STATIC_ROOT',
                       "join(BASE_DIR, '%s.static')" % args.projectname)
    settings.set_value('common', 'MEDIA_URL', "'/media/'")
    settings.set_value('common', 'USE_I18N', True)
    settings.set_value('common', 'USE_L10N', True)
    settings.move_var('common', profiles, 'DEBUG')
    settings.move_var('common', profiles, 'ALLOWED_HOSTS')
    settings.append_lines(
        'develop', "DEVELOP_APPS = (", "    'behave_django',", ")", "", "try:",
        "    from importlib import import_module",
        "    for pkg in DEVELOP_APPS:", "        import_module(pkg)",
        "    INSTALLED_APPS += DEVELOP_APPS", "except ImportError:",
        "    from warnings import warn",
        "    warn('Development packages missing. Please run `make develop`', Warning)"
    )
    settings.move_var('common', profiles, 'DATABASES')
    settings.move_var('common', profiles, 'MEDIA_ROOT')
    settings.move_var('common', profiles, 'STATIC_ROOT')
    settings.move_var('common', profiles, 'SECRET_KEY')

    account_domain = '%s.organice.io' % args.account
    for prof in ('staging', 'production'):
        settings.set_value(prof, 'DEBUG', False)
        settings.set_value_lines(
            prof, 'ALLOWED_HOSTS', '[',
            "    '%s'," % (args.domain if args.domain else account_domain),
            "    '%s'," %
            (account_domain if args.domain else 'www.%s' % account_domain),
            ']')
def _create_project():
    global args
    global profiles
    global projectname
    global settings

    mode0755 = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
    profiles = ('develop', 'staging', 'production')
    filenames = ('__init__', 'common') + profiles

    if args.manage == 'multi':
        if os.path.isfile('manage.py'):
            print('Deleting manage.py to allow multi-settings platform setup ...')
            os.unlink('manage.py')

    print('Generating project %s ...' % projectname)
    code = call(['django-admin.py', 'startproject', projectname, '.'])
    if code != 0:
        return code
    os.chmod('manage.py', mode0755)

    if args.manage == 'multi':
        print('Removing project specific configuration from manage.py ...')
        with open('manage.py', 'a+') as f:
            lines = f.readlines()
            f.seek(0)
            f.truncate()
            for line in lines:
                if not 'import os' in line and not 'DJANGO_SETTINGS_MODULE' in line:
                    f.write(line)

    print('Creating directories ...')
    os.mkdir('%s.media' % projectname)
    os.mkdir('%s.static' % projectname)
    os.mkdir('%s.templates' % projectname)
    os.mkdir(os.path.join(projectname, 'settings'))

    print('Converting settings to deployment profiles (%s) ...' % ', '.join(profiles))
    os.rename(os.path.join(projectname, 'settings.py'),
              os.path.join(projectname, 'settings', 'common.py'))

    settings = DjangoSettingsManager(projectname, *filenames)
    settings.append_lines('__init__',
                          '"""',
                          'Modularized settings generated by django Organice setup. http://organice.io',
                          'This solution follows the second recommendation from',
                          'http://www.sparklewise.com/django-settings-for-production-and-development-best-practices/',
                          '"""',
                          'from .develop import *')
    for prof in profiles:
        settings.append_lines(prof,
                              '# Django project settings for %s environment' % prof.capitalize(),
                              '',
                              'from .common import *')

    # out-of-the-box Django values relevant for deployment
    settings.delete_var('common', 'SITE_ID')
    settings.insert_lines('common',
                          '_ = lambda s: s',
                          'import os',
                          'PROJECT_PATH = os.sep.join(__file__.split(os.sep)[:-3])',
                          '',
                          'SITE_ID = 1')
    settings.set_value('common', 'MEDIA_ROOT', "os.path.join(PROJECT_PATH, '%s.media')" % projectname)
    settings.set_value('common', 'STATIC_ROOT', "os.path.join(PROJECT_PATH, '%s.static')" % projectname)
    settings.set_value('common', 'MEDIA_URL', "'/media/'")
    settings.set_value('common', 'USE_I18N', False)
    settings.move_var('common', profiles, 'DEBUG')
    settings.move_var('common', profiles, 'TEMPLATE_DEBUG')
    settings.move_var('common', profiles, 'ALLOWED_HOSTS')
    settings.move_var('common', profiles, 'DATABASES')
    settings.move_var('common', profiles, 'MEDIA_ROOT')
    settings.move_var('common', profiles, 'STATIC_ROOT')
    settings.move_var('common', profiles, 'SECRET_KEY')
    for prof in ('staging', 'production'):
        settings.set_value(prof, 'DEBUG', False)
        settings.set_value_lines(prof, 'ALLOWED_HOSTS',
                                 '[',
                                 "    '%s.organice.io'," % (args.account if args.account else projectname),
                                 "    '%s'," % (args.domain if args.domain else 'www.example.com'),
                                 ']')