예제 #1
0
def run_migrations(args, options, executor_codename, schema_name, allow_atomic=True):
    from django.core.management import color
    from django.core.management.base import OutputWrapper
    from django.db import connection

    style = color.color_style()

    def style_func(msg):
        return '[%s:%s] %s' % (
            style.NOTICE(executor_codename),
            style.NOTICE(schema_name),
            msg
        )

    connection.set_schema(schema_name)
    stdout = OutputWrapper(sys.stdout)
    stdout.style_func = style_func
    stderr = OutputWrapper(sys.stderr)
    stderr.style_func = style_func
    if int(options.get('verbosity', 1)) >= 1:
        stdout.write(style.NOTICE("=== Starting migration"))
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)

    try:
        transaction.commit()
        connection.close()
        connection.connection = None
    except transaction.TransactionManagementError:
        if not allow_atomic:
            raise

        # We are in atomic transaction, don't close connections
        pass

    connection.set_schema_to_public()
예제 #2
0
def run_migrations(args, options, executor_codename, schema_name, tenant_type='',
                   allow_atomic=True, idx=None, count=None):
    from django.core.management import color
    from django.core.management.base import OutputWrapper
    from django.db import connections
    style = color.color_style()

    def style_func(msg):
        percent_str = ''
        if idx is not None and count is not None and count > 0:
            percent_str = '%d/%d (%s%%) ' % (idx + 1, count, int(100 * (idx + 1) / count))

        message = '[%s%s:%s] %s' % (
            percent_str,
            style.NOTICE(executor_codename),
            style.NOTICE(schema_name),
            msg
        )
        signal_message = '[%s%s:%s] %s' % (
            percent_str,
            executor_codename,
            schema_name,
            msg
        )
        schema_migrate_message.send(run_migrations, message=signal_message)
        return message

    connection = connections[options.get('database', get_tenant_database_alias())]
    connection.set_schema(schema_name, tenant_type=tenant_type)

    # ensure that django_migrations table is created in the schema before migrations run, otherwise the migration
    # table in the public schema gets picked and no migrations are applied
    migration_recorder = MigrationRecorder(connection)
    migration_recorder.ensure_schema()

    stdout = OutputWrapper(sys.stdout)
    stdout.style_func = style_func
    stderr = OutputWrapper(sys.stderr)
    stderr.style_func = style_func
    if int(options.get('verbosity', 1)) >= 1:
        stdout.write(style.NOTICE("=== Starting migration"))
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)

    try:
        transaction.commit()
        connection.close()
        connection.connection = None
    except transaction.TransactionManagementError:
        if not allow_atomic:
            raise

        # We are in atomic transaction, don't close connections
        pass

    connection.set_schema_to_public()
    schema_migrated.send(run_migrations, schema_name=schema_name)
예제 #3
0
def run_migrations(args,
                   options,
                   executor_codename,
                   schema_name,
                   allow_atomic=True,
                   idx=None,
                   count=None):
    from django.core.management import color
    from django.core.management.base import OutputWrapper
    from django.db import connections

    style = color.color_style()

    def style_func(msg):
        percent_str = ''
        if idx is not None and count is not None and count > 0:
            percent_str = '%d/%d (%s%%) ' % (idx + 1, count,
                                             int(100 * (idx + 1) / count))
        return '[%s%s:%s] %s' % (percent_str, style.NOTICE(executor_codename),
                                 style.NOTICE(schema_name), msg)

    include_public = True if (options.get('shared')
                              or schema_name == 'public') else False
    connection = connections[get_tenant_database_alias()]
    connection.set_schema(schema_name, include_public=include_public)

    stdout = OutputWrapper(sys.stdout)
    stdout.style_func = style_func
    stderr = OutputWrapper(sys.stderr)
    stderr.style_func = style_func
    if int(options.get('verbosity', 1)) >= 1:
        stdout.write(style.NOTICE("=== Starting migration"))
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)

    try:
        transaction.commit()
        connection.close()
        connection.connection = None
    except transaction.TransactionManagementError:
        if not allow_atomic:
            raise

        # We are in atomic transaction, don't close connections
        pass

    connection.set_schema_to_public()
예제 #4
0
def run_migrations(args,
                   options,
                   executor_codename,
                   schema_name,
                   allow_atomic=True):
    from django.core.management import color
    from django.core.management.base import OutputWrapper
    from django.db import connections

    PUBLIC_SCHEMA_NAME = get_public_schema_name()

    options['database'] = settings.TENANT_DATABASE
    if schema_name == PUBLIC_SCHEMA_NAME:
        options['database'] = DEFAULT_DB_ALIAS

    style = color.color_style()

    def style_func(msg):
        return '[%s:%s:%s] %s' % (options['database'],
                                  style.NOTICE(executor_codename),
                                  style.NOTICE(schema_name), msg)

    connections[options['database']].set_schema(schema_name)

    stdout = OutputWrapper(sys.stdout)
    stdout.style_func = style_func
    stderr = OutputWrapper(sys.stderr)
    stderr.style_func = style_func
    if int(options.get('verbosity', 1)) >= 1:
        stdout.write(style.NOTICE("=== Starting migration"))
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)

    try:
        transaction.commit()
        connections[options['database']].close()
        connections[options['database']].connection = None
    except transaction.TransactionManagementError:
        if not allow_atomic:
            raise

        # We are in atomic transaction, don't close connections
        pass
예제 #5
0
    def __init__(self, stdout=None, stderr=None, no_color=False):
        super().__init__(stdout, stderr, no_color)

        # 检测目录位置
        if self.NEED_PROJECT:
            settings_path = os.path.join(os.getcwd(), 'deeru')
            settings_py = os.path.join(settings_path, 'settings.py')

            if not os.path.exists(settings_py):
                raise CommandError('该命令需要在工程目录下运行')

        self.error = self.stderr.write

        info_out = OutputWrapper(sys.stdout)
        info_out.style_func = self.style.WARNING
        self.info = info_out.write

        success_out = OutputWrapper(sys.stdout)
        success_out.style_func = self.style.SUCCESS
        self.success = success_out.write
예제 #6
0
def run_migrations(args, options, executor_codename, schema_name, allow_atomic=True, idx=None, count=None):
    from django.core.management import color
    from django.core.management.base import OutputWrapper
    from django.db import connections

    style = color.color_style()

    def style_func(msg):
        percent_str = ''
        if idx is not None and count is not None and count > 0:
            percent_str = '%d/%d (%s%%) ' % (idx + 1, count, int(100 * (idx + 1) / count))
        return '[%s%s:%s] %s' % (
            percent_str,
            style.NOTICE(executor_codename),
            style.NOTICE(schema_name),
            msg
        )

    connection = connections[get_tenant_database_alias()]
    connection.set_schema(schema_name)

    stdout = OutputWrapper(sys.stdout)
    stdout.style_func = style_func
    stderr = OutputWrapper(sys.stderr)
    stderr.style_func = style_func
    if int(options.get('verbosity', 1)) >= 1:
        stdout.write(style.NOTICE("=== Starting migration"))
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)

    try:
        transaction.commit()
        connection.close()
        connection.connection = None
    except transaction.TransactionManagementError:
        if not allow_atomic:
            raise

        # We are in atomic transaction, don't close connections
        pass

    connection.set_schema_to_public()
예제 #7
0
def run_migrations(args,
                   options,
                   executor_codename,
                   schema_name,
                   allow_atomic=True):
    from django.core.management import color
    from django.core.management.base import OutputWrapper
    from django.db import connection

    style = color.color_style()

    def style_func(msg):
        return '[%s:%s] %s' % (style.NOTICE(executor_codename),
                               style.NOTICE(schema_name), msg)

    stdout = OutputWrapper(sys.stdout)
    stdout.style_func = style_func
    stderr = OutputWrapper(sys.stderr)
    stderr.style_func = style_func
    if int(options.get('verbosity', 1)) >= 1:
        stdout.write(
            style.NOTICE("=== Running migrate for schema %s" % schema_name))

    connection.set_schema(schema_name)
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)

    try:
        transaction.commit()
        connection.close()
        connection.connection = None
    except transaction.TransactionManagementError:
        if not allow_atomic:
            raise

        # We are in atomic transaction, don't close connections
        pass

    connection.set_schema_to_public()
예제 #8
0
    def handle(self, *args, **options):
        self.error = self.stderr.write

        info_out = OutputWrapper(sys.stdout)
        info_out.style_func = self.style.WARNING
        self.info = info_out.write

        success_out = OutputWrapper(sys.stdout)
        success_out.style_func = self.style.SUCCESS
        self.success = success_out.write

        self.info("初始化中:")
        try:
            os.mkdir('log')
        except:
            pass
        with open('./log/init.log', 'a')as f:
            f.write('开始初始化(%s)\n' % str(now()))

        # ============

        self.info('初始化数据库 ... ')

        with open('./log/init.log', 'a')as f:
            f.write('初始化数据库\n')
            try:

                management.call_command('migrate', stdout=f)
                # management.call_command('makemigrations', 'app', stdout=f)
            except:

                traceback.print_exc(file=f)
                self.error('初始化数据库 ... [失败],更多信息查看 ./log/init.log ')
                raise

        self.success('初始化数据库 ... [完成]')

        # ============

        self.info('初始化静态文件 ... ')
        with open('./log/init.log', 'a')as f:
            f.write('初始化静态文件\n')
            try:
                management.call_command('collectstatic', '--noinput', stdout=f)
            except:
                traceback.print_exc(file=f)
                self.error('初始化静态文件 ... [失败],更多信息查看 ./log/init.log ')
                raise

        self.success('初始化静态文件 ... [完成]')

        # ============

        self.info('创建管理员账户 ... ')

        username = input('输入管理账户登录名(默认:admin):')
        if not username:
            username = '******'
        password = getpass('输入密码(默认:123456):')
        if not password:
            password = '******'
        with open('./log/init.log', 'a')as f:
            try:
                flag = True
                User._default_manager.db_manager('default').create_superuser(username, None, password)

            except:
                flag = False
                traceback.print_exc(3)

                traceback.print_exc(file=f)
                self.error('创建管理员账户 ... [失败],更多信息查看 ./log/init.log ')


            finally:
                if not flag:
                    return

        # ============
        self.success('创建管理员账户 ... [完成]')

        self.success('\n初始化完成 !!')
예제 #9
0
    def handle(self, *args, **options):
        self.error = self.stderr.write

        info_out = OutputWrapper(sys.stdout)
        info_out.style_func = self.style.WARNING
        self.info = info_out.write

        success_out = OutputWrapper(sys.stdout)
        success_out.style_func = self.style.SUCCESS
        self.success = success_out.write

        self.nwp = options['nwp']
        self.ncontent = options['ncontent']
        self.cover = options['cover']
        model = options['model']

        with open(options['xml_path']) as f:
            s = f.read()
        self.root = ET.fromstring(s)[0]

        self.category = {}

        self.tag = {}

        self.article = []
        self.comment = {}

        if 'c' in model or 'a' in model:
            self.info('导入分类中...')

            self.get_category()
            self.save_category()

            self.success('导入分类完成')

        if 't' in model or 'a' in model:
            self.info('导入标签中...')

            self.get_tag()
            self.save_tag()

            self.success('导入标签完成')

        if 'a' in model:
            self.info('导入文章中...')
            self.get_article()
            self.article.sort(key=lambda x: x['created_time'])
            self.save_article()
            self.success('导入文章完成')

            self.info('导入文章分类中...')
            self.save_article_category()
            self.success('导入文章分类完成')

            self.info('导入文章标签中...')
            self.save_article_tag()
            self.success('导入文章标签完成')

            self.info('导入评论中...')
            self.get_comment()
            self.save_comment()
            self.success('导入评论完成')
예제 #10
0
import click
import json
import re

import sys
from django.core.management.base import OutputWrapper
from django_extensions.management.color import color_style

LOG_RE = "^\\[(?P<timestamp>\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{6})\\] (?P<level>\\w+) \\[(?P<code_ns>[^:]+):(?P<code_line>\\d+)\\] (?P<jmsg>.*)$"
h = re.compile(LOG_RE)

stdout = OutputWrapper(sys.stdout)
stderr = OutputWrapper(sys.stderr)
style = color_style()
stderr.style_func = style.ERROR


@click.command()
@click.argument('input', type=click.Path(exists=True))
@click.argument('user', type=str)
def user_logs(input, user):
    logs = []
    stdout.write(
        style.SUCCESS("Parsing '{}' logs from '{}' ".format(user, input)))
    with open(input, 'r') as f:
        errors_count = 0
        for l in f:
            matches = h.match(l)
            level = matches.group('level')
            try:
                jmsg = json.loads(matches.group('jmsg'))[0]