def _syncdb(self):
     if not self._db_current():
         log.info("Creating database tables...")
         args = ['', 'syncdb', '--noinput', '--migrate']
         if not self.verbose:
             args = args + ["--verbosity", "0"]
         ManagementUtility(args).execute()
     else:
         log.info("Database tables already OK")
예제 #2
0
 def run_management_command(self, *args):
     from django.core.management import ManagementUtility
     args = ['manage.py'] + list(args)
     utility = ManagementUtility(args)
     try:
         utility.execute()
     except SystemExit:
         pass
     print('')
예제 #3
0
 def test_version_is_printed_once(self):
     args = ['manage.py', '--version']
     utility = ManagementUtility(argv=args)
     self.begin_capture()
     try:
         utility.execute()
     finally:
         self.end_capture()
     expected = get_version()
     self.assertTrue(self.capture['stdout'].count(expected) == 1)
예제 #4
0
 def test_update_settings(self):
     self.assertTrue(settings.DEBUG)
     args = ['manage.py', 'settings', '--django_debug=False', 'DEBUG']
     utility = ManagementUtility(argv=args)
     self.begin_capture()
     try:
         utility.execute()
     finally:
         self.end_capture()
     self.assertTrue('False' in self.capture['stdout'])
예제 #5
0
def report_data(dumper, commands_to_skip):
    """
    Fetches data from management commands and reports it to dumper.

    :type dumper _xml.XmlDumper
    :type commands_to_skip list
    :param commands_to_skip list of commands to skip
    :param dumper: destination to report
    """
    utility = ManagementUtility()
    for command_name in get_commands().keys():

        if command_name in commands_to_skip:
            sys.stderr.write(
                "Skipping command '{0}' due to config\n".format(command_name))
            continue

        fetcher = _Fetcher(utility, command_name)
        fetcher.daemon = True
        fetcher.start()
        fetcher.join(int(os.getenv("_PYCHARM_DJANGO_DEFAULT_TIMEOUT", "2")))
        command = fetcher.result
        if not command:
            if fetcher.command_lead_to_exception:
                sys.stderr.write(
                    "Command '{0}' skipped\n".format(command_name))
                continue
            else:
                sys.stderr.write(
                    "Command '{0}' took too long and may freeze everything. Consider adding it to 'skip commands' list\n"
                    .format(command_name))
                sys.exit(1)

        use_argparse = False
        try:
            use_argparse = command.use_argparse
        except AttributeError:
            pass

        try:
            parser = command.create_parser("", command_name)
        except Exception as e:
            sys.stderr.write("Error parsing command {0}: {1}\n".format(
                command_name, e))
            continue

        dumper.start_command(
            command_name=command_name,
            command_help_text=VersionAgnosticUtils().to_unicode(
                command.usage("")).replace("%prog", command_name))
        module_to_use = _argparse if use_argparse else _optparse  # Choose appropriate module: argparse, optparse
        module_to_use.process_command(dumper, command, parser)
        dumper.close_command()
예제 #6
0
    class PycharmTestCommand(
            ManagementUtility().fetch_command("test").__class__):
        def get_runner(self):
            TEST_RUNNER = 'django_test_runner.run_tests'
            test_path = TEST_RUNNER.split('.')
            # Allow for Python 2.5 relative paths
            if len(test_path) > 1:
                test_module_name = '.'.join(test_path[:-1])
            else:
                test_module_name = '.'
            test_module = __import__(test_module_name, {}, {}, test_path[-1])
            test_runner = getattr(test_module, test_path[-1])
            return test_runner

        def handle(self, *test_labels, **options):
            # handle south migration in tests
            commands = management.get_commands()
            if hasattr(settings, "SOUTH_TESTS_MIGRATE"
                       ) and not settings.SOUTH_TESTS_MIGRATE:
                # point at the core syncdb command when creating tests
                # tests should always be up to date with the most recent model structure
                commands['syncdb'] = 'django.core'
            elif 'south' in settings.INSTALLED_APPS:
                try:
                    from south.management.commands import MigrateAndSyncCommand
                    commands['syncdb'] = MigrateAndSyncCommand()
                    from south.hacks import hacks
                    if hasattr(hacks, "patch_flush_during_test_db_creation"):
                        hacks.patch_flush_during_test_db_creation()
                except ImportError:
                    commands['syncdb'] = 'django.core'

            verbosity = int(options.get('verbosity', 1))
            interactive = options.get('interactive', True)
            failfast = options.get('failfast', False)
            TestRunner = self.get_runner()

            if not inspect.ismethod(TestRunner):
                our_options = {
                    "verbosity": int(verbosity),
                    "interactive": interactive,
                    "failfast": failfast
                }
                options.update(our_options)
                failures = TestRunner(test_labels, **options)
            else:
                test_runner = TestRunner(verbosity=verbosity,
                                         interactive=interactive,
                                         failfast=failfast)
                failures = test_runner.run_tests(test_labels)

            if failures:
                sys.exit(bool(failures))
예제 #7
0
def runtests(*test_args):

    parent = os.path.dirname(os.path.abspath(__file__))
    os.environ['DJANGO_SETTINGS_MODULE'] = 'consent.tests.settings'
    sys.path.insert(0, parent)

    from django.core.management import ManagementUtility

    utility = ManagementUtility()
    command = utility.fetch_command('test')
    command.execute(verbosity=1)
    sys.exit()
예제 #8
0
    def cmd_help_text(self, cmd: str) -> str:
        """Get the help message for a given management command.

        Also asserts that stderr is empty and the command exists with status code 0."""
        stdout = io.StringIO()
        stderr = io.StringIO()
        with mock.patch("sys.stdout", stdout), mock.patch("sys.stderr", stderr):
            util = ManagementUtility(["manage.py", cmd, "--help"])
            with self.assertSystemExit(0):
                util.execute()

        self.assertEqual(stderr.getvalue(), "")
        return stdout.getvalue()
예제 #9
0
def execute_from_command_line(argv=None):
    """
    A simple method that runs a ManagementUtility.
    """
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "colab.settings")
    from django.conf import settings

    if not hasattr(settings, 'SECRET_KEY') and 'initconfig' in sys.argv:
        command = initconfig.Command()
        command.handle()
    else:
        utility = ManagementUtility(argv)
        utility.execute()
예제 #10
0
    def handle(self, *args, **options):
        """
        Run and profile the specified management command with the provided
        arguments.
        """
        if not self.use_argparse and not len(args):
            self.print_help(sys.argv[0], 'profile')
            sys.exit(1)
        if not options['sort'] and not options['path']:
            self.stdout.write(
                'Output file path is required for call graph generation')
            sys.exit(1)

        if self.use_argparse:
            command_name = options['other_command']
        else:
            command_name = args[0]
        utility = ManagementUtility(sys.argv)
        command = utility.fetch_command(command_name)
        parser = command.create_parser(sys.argv[0], command_name)
        if self.use_argparse:
            command_options = parser.parse_args(options['command_arguments'])
            command_args = vars(command_options).pop('args', ())
        else:
            command_options, command_args = parser.parse_args(list(args[1:]))

        if command_name == 'test' and django_settings.TEST_RUNNER == 'django_nose.NoseTestSuiteRunner':
            # Ugly hack: make it so django-nose won't have nosetests choke on
            # our parameters
            BaseCommand.option_list += self.custom_options

        if options['backend'] == 'yappi' or (settings.YADP_PROFILER_BACKEND
                                             == 'yappi'
                                             and not options['backend']):
            import yet_another_django_profiler.yadp_yappi as yadp_yappi
            profiler = yadp_yappi.YappiProfile(wall=options['clock'] == 'wall')
        else:
            profiler = cProfile.Profile()

        if 'testing' not in options:
            atexit.register(output_results, profiler, options, self.stdout)
        profiler.runcall(call_command,
                         command_name,
                         *command_args,
                         stderr=self.stderr,
                         stdout=self.stdout,
                         **vars(command_options))
        if 'testing' in options:
            output_results(profiler, options, self.stdout)
        else:
            sys.exit(0)
예제 #11
0
파일: base.py 프로젝트: yaka-es/django-ca
    def call_command(self, command, *argv, **kwargs):
        argv = ['manage.py'] + list(argv)
        utility = ManagementUtility(argv)
        command = utility.fetch_command(command)
        parser = command.create_parser('manage.py', command)
        options = parser.parse_args(argv[1:])
        cmd_options = vars(options)

        cmd_options.setdefault('stdout', StringIO())
        cmd_options.setdefault('stderr', StringIO())

        stdin = kwargs.pop('stdin', StringIO())
        with patch('sys.stdin', stdin):
            command.execute(**cmd_options)
        return cmd_options['stdout'].getvalue(), cmd_options['stderr'].getvalue()
예제 #12
0
파일: base.py 프로젝트: zwd1990/django-ca
    def cmd_e2e(self, cmd, stdin=None, stdout=None, stderr=None):
        """Call a management command the way manage.py does.

        Unlike call_command, this method also tests the argparse configuration of the called command.
        """
        stdout = stdout or StringIO()
        stderr = stderr or StringIO()
        if stdin is None:
            stdin = StringIO()

        with patch('sys.stdin', stdin), patch('sys.stdout', stdout), patch('sys.stderr', stderr):
            util = ManagementUtility(['manage.py', ] + list(cmd))
            util.execute()

        return stdout.getvalue(), stderr.getvalue()
예제 #13
0
    def start(self, args):
        project_name = args.project_name
        print('Gabo is starting {0}...'.format(project_name))
        PROJECT_PACKAGE = Path(__file__).resolve().parent.parent
        macondo_project_path = os.path.join(PROJECT_PACKAGE, 'macondo_project')

        django_args = [
            'django-admin.py',
            'startproject',
            '--template={0}'.format(macondo_project_path),
            project_name
        ]

        django_management = ManagementUtility(django_args)
        django_management.execute()
예제 #14
0
def create_project(parser, options, args):

    # Validate args
    if len(args) < 2:
        parser.error("Please specify a name for your Wagtail installation")
    elif len(args) > 3:
        print(args)
        parser.error("Too many arguments")

    project_name = args[1]
    try:
        dest_dir = args[2]
    except IndexError:
        dest_dir = None

    # Make sure given name is not already in use by another python package/module.
    try:
        __import__(project_name)
    except ImportError:
        pass
    else:
        parser.error("'%s' conflicts with the name of an existing "
                     "Python module and cannot be used as a project "
                     "name. Please try another name." % project_name)

    print("Creating a Wagtail project called %(project_name)s" % {'project_name': project_name})  # noqa

    # Create the project from the Wagtail template using startapp

    # First find the path to Wagtail
    import wagtail
    wagtail_path = os.path.dirname(wagtail.__file__)
    template_path = os.path.join(wagtail_path, 'project_template')

    # Call django-admin startproject
    utility_args = ['django-admin.py',
                    'startproject',
                    '--template=' + template_path,
                    '--ext=html,rst',
                    project_name]

    if dest_dir:
        utility_args.append(dest_dir)

    utility = ManagementUtility(utility_args)
    utility.execute()

    print("Success! %(project_name)s has been created" % {'project_name': project_name})  # noqa
예제 #15
0
    def handle(self, *args, **kwargs):
        """
        Handle command execution
        :param args:
        :param kwargs:
        :return: None
        """
        args = [sys.argv[0], 'makemigrations']

        for app in settings.INSTALLED_APPS:
            app_dir = os.path.join(settings.BASE_DIR, app)
            if os.path.isdir(app_dir):
                args.append(app)

        utility = ManagementUtility(args)
        utility.execute()
예제 #16
0
def schemamigration():
    # turn ``schemamigration.py --initial`` into
    # ``manage.py schemamigration cmsplugin_disqus --initial`` and setup the
    # enviroment
    from django.conf import settings

    from django.core.management import ManagementUtility
    settings.configure(INSTALLED_APPS=INSTALLED_APPS,
                       ROOT_URLCONF=ROOT_URLCONF,
                       DATABASES=DATABASES,
                       TEMPLATE_CONTEXT_PROCESSORS=TEMPLATE_CONTEXT_PROCESSORS)
    argv = list(sys.argv)
    argv.insert(1, 'schemamigration')
    argv.insert(2, 'djangocms_style')
    utility = ManagementUtility(argv)
    utility.execute()
예제 #17
0
def create_project(parser, options, args):
    # Validate args
    if len(args) < 2:
        parser.error("Please specify a name for your wagtail installation")
    elif len(args) > 2:
        parser.error("Too many arguments")

    project_name = args[1]

    # Make sure given name is not already in use by another python package/module.
    try:
        __import__(project_name)
    except ImportError:
        pass
    else:
        parser.error("'%s' conflicts with the name of an existing "
                     "Python module and cannot be used as a project "
                     "name. Please try another name." % project_name)

    # Make sure directory does not already exist
    if os.path.exists(project_name):
        print('A directory called %(project_name)s already exists. \
            Please choose another name for your wagtail project or remove the existing directory.'
              % {'project_name': project_name})
        sys.exit(errno.EEXIST)

    print("Creating a wagtail project called %(project_name)s" %
          {'project_name': project_name})

    # Create the project from the wagtail template using startapp

    # First find the path to wagtail
    import wagtail
    wagtail_path = os.path.dirname(wagtail.__file__)
    template_path = os.path.join(wagtail_path, 'project_template')

    # Call django-admin startproject
    utility = ManagementUtility([
        'django-admin.py', 'startproject', '--template=' + template_path,
        '--name=Vagrantfile', '--ext=html,rst', project_name
    ])
    utility.execute()

    print("Success! %(project_name)s is created" %
          {'project_name': project_name})
예제 #18
0
def manage(command, args=[], in_background=False):
    """
    Run a django command on the kalite project

    :param command: The django command string identifier, e.g. 'runserver'
    :param args: List of options to parse to the django management command
    :param in_background: Creates a sub-process for the command
    """
    # Import here so other commands can run faster
    if not in_background:
        utility = ManagementUtility([os.path.basename(sys.argv[0]), command] + args)
        # This ensures that 'kalite' is printed in help menus instead of
        # 'kalitectl.py' (a part from the top most text in `kalite manage help`
        utility.prog_name = 'kalite manage'
        utility.execute()
    else:
        thread = ManageThread(command, args=args)
        thread.start()
예제 #19
0
    def handle(self, *args, **kwargs):
        """
        Handle command execution
        :param args:
        :param kwargs:
        :return: None
        """
        args = (sys.argv[0], 'loaddata')

        for app in settings.INSTALLED_APPS:
            app_dir = os.path.join(settings.BASE_DIR, app)
            if os.path.isdir(app_dir):
                print(f'Loading Fixtures for {app}')

                utility = ManagementUtility([*args, app])
                p = Thread(target=utility.execute)
                p.start()
                p.join()
                print('')
예제 #20
0
    def run_from_argv(self, argv):
        try:
            command_name = argv[2]
            argv.pop(1)
            utility = ManagementUtility(argv)
            command_class = utility.fetch_command(command_name)
            handle = command_class.handle

            def locking_handle(self, *args, **options):
                with distributedlock(command_name):
                    handle(self, *args, **options)

            command_class.handle = types.MethodType(locking_handle,
                                                    command_class)
            command_class.run_from_argv(utility.argv)
        except IndexError:
            raise CommandError('Missing arguments')
        except LockNotAcquiredError:
            raise CommandError('%s command is already locked' % command_name)
예제 #21
0
def report_data(dumper):
    """
    Fetches data from management commands and reports it to dumper.

    :type dumper _xml.XmlDumper
    :param dumper: destination to report
    """
    utility = ManagementUtility()
    for command_name in get_commands().keys():
        try:
            command = utility.fetch_command(command_name)
        except ImproperlyConfigured:
            continue  # TODO: Log somehow

        assert isinstance(command, BaseCommand)
        dumper.start_command(
            command_name=command_name,
            command_help_text=str(
                command.usage("").replace("%prog", command_name)),
            # TODO: support subcommands
            command_args_text=str(command.args))
        for opt in command.option_list:
            num_of_args = int(opt.nargs) if opt.nargs else 0
            opt_type = None
            if num_of_args > 0:
                # If option accepts arg, we need to determine its type. It could be int, choices, or something other
                # See https://docs.python.org/2/library/optparse.html#standard-option-types
                if opt.type in ["int", "long"]:
                    opt_type = "int"
                elif opt.choices:
                    assert isinstance(opt.choices,
                                      list), "Choices should be list"
                    opt_type = opt.choices

            # There is no official way to access this field, so I use protected one. At least it is public API.
            # noinspection PyProtectedMember
            dumper.add_command_option(
                long_opt_names=opt._long_opts,
                short_opt_names=opt._short_opts,
                help_text=opt.help,
                argument_info=(num_of_args, opt_type) if num_of_args else None)
        dumper.close_command()
예제 #22
0
def schemamigration():

    #
    # turn ``schemamigration.py --initial`` into:
    #   ``manage.py schemamigration aldryn_segmentation --initial``
    # and setup the environment.
    #

    from django.conf import settings

    from django.core.management import ManagementUtility
    settings.configure(INSTALLED_APPS=INSTALLED_APPS,
                       ROOT_URLCONF=ROOT_URLCONF,
                       DATABASES=DATABASES,
                       TEMPLATE_CONTEXT_PROCESSORS=TEMPLATE_CONTEXT_PROCESSORS)
    argv = list(sys.argv)
    argv.insert(1, 'schemamigration')
    argv.insert(2, 'aldryn_segmentation')
    utility = ManagementUtility(argv)
    utility.execute()
예제 #23
0
def run_django_command(command, args):
    try:
        sys.path.append(path.join(MAIN_DIR, "server"));
        server_py = path.join(MAIN_DIR, "server", "manage.py")
        
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
        
        # Prepare argv
        argv = list(args)
        argv.insert(0, command)        
        argv.insert(0, server_py)
        
        from django.core.management import ManagementUtility
        commander = ManagementUtility(argv)
        commander.execute()
    except:
        print "There was an error running a the '%s' command with '%s'" % (command, args)
        sys.exit(1)
        
    return
예제 #24
0
    def run(self, project_name=None, dest_dir=None):
        # Make sure given name is not already in use by another python package/module.
        try:
            __import__(project_name)
        except ImportError:
            pass
        else:
            sys.exit("'%s' conflicts with the name of an existing "
                     "Python module and cannot be used as a project "
                     "name. Please try another name." % project_name)

        print("Creating a Wagtail project called %(project_name)s" %
              {"project_name": project_name})  # noqa

        # Create the project from the Wagtail template using startapp

        # First find the path to Wagtail
        import wagtail

        wagtail_path = os.path.dirname(wagtail.__file__)
        template_path = os.path.join(wagtail_path, "project_template")

        # Call django-admin startproject
        utility_args = [
            "django-admin",
            "startproject",
            "--template=" + template_path,
            "--ext=html,rst",
            "--name=Dockerfile",
            project_name,
        ]

        if dest_dir:
            utility_args.append(dest_dir)

        utility = ManagementUtility(utility_args)
        utility.execute()

        print("Success! %(project_name)s has been created" %
              {"project_name": project_name})  # noqa
예제 #25
0
def run_django_command(command, args, require_settings = True):
    try:
        sys.path.append(path.join(MAIN_DIR, "server"));
        server_py = path.join(MAIN_DIR, "server", "manage.py")

        # Differentiates between those manage.py commands that require
        # a working settings.py, and those that can run without it, like 
        # startapp.
        if require_settings:
            os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
        
        # Prepare argv
        argv = list(args)
        argv.insert(0, command)        
        argv.insert(0, server_py)
        
        from django.core.management import ManagementUtility
        commander = ManagementUtility(argv)
        commander.execute()
    except Exception, e:
        print "There was an error running the '%s' command with '%s'." % (command, args)
        raise
예제 #26
0
def djangomigration():
    # turn ``djangomigration.py`` into
    # ``manage.py makemigrations djangocms_grid`` and setup the
    # environment
    from django.conf import settings

    from django.core.management import ManagementUtility
    settings.configure(
        INSTALLED_APPS=INSTALLED_APPS,
        ROOT_URLCONF=ROOT_URLCONF,
        DATABASES=DATABASES,
        TEMPLATE_CONTEXT_PROCESSORS=TEMPLATE_CONTEXT_PROCESSORS,
        MIGRATION_MODULES=MIGRATION_MODULES,
        LANGUAGE_CODE=LANGUAGE_CODE,
        LANGUAGES=LANGUAGES,
        SITE_ID=SITE_ID,
    )
    argv = list(sys.argv)
    argv.insert(1, 'makemigrations')
    argv.insert(2, 'djangocms_grid')
    utility = ManagementUtility(argv)
    utility.execute()
예제 #27
0
def report_data(dumper):
    """
    Fetches data from management commands and reports it to dumper.

    :type dumper _xml.XmlDumper
    :param dumper: destination to report
    """
    utility = ManagementUtility()
    for command_name in get_commands().keys():
        try:
            command = utility.fetch_command(command_name)
        except Exception as e:
            sys.stderr.write("Error fetching command {0}: {1}\n".format(
                command_name, e))
            continue

        assert isinstance(command, BaseCommand)

        use_argparse = False
        try:
            use_argparse = command.use_argparse
        except AttributeError:
            pass

        try:
            parser = command.create_parser("", command_name)
        except Exception as e:
            sys.stderr.write("Error parsing command {0}: {1}\n".format(
                command_name, e))
            continue

        dumper.start_command(
            command_name=command_name,
            command_help_text=VersionAgnosticUtils().to_unicode(
                command.usage("")).replace("%prog", command_name))
        module_to_use = _argparse if use_argparse else _optparse  # Choose appropriate module: argparse, optparse
        module_to_use.process_command(dumper, command, parser)
        dumper.close_command()
예제 #28
0
파일: ra.py 프로젝트: yomore/django-ra-erp
    def run(self, project_name=None, dest_dir=None):
        # Make sure given name is not already in use by another python package/module.
        try:
            __import__(project_name)
        except ImportError:
            pass
        else:
            sys.exit("'%s' conflicts with the name of an existing "
                     "Python module and cannot be used as a project "
                     "name. Please try another name." % project_name)

        print("Creating a Ra project called %(project_name)s" %
              {'project_name': project_name})  # noqa

        # Create the project from the Ra template using startapp

        # First find the path to Ra
        import ra
        ra_path = os.path.dirname(ra.__file__)
        template_path = os.path.join(ra_path, 'project_template',
                                     'project_template')

        # Call django-admin startproject
        utility_args = [
            'django-admin.py', 'startproject', '--template=' + template_path,
            '--ext=html,rst', project_name
        ]

        if dest_dir:
            utility_args.append(dest_dir)

        utility = ManagementUtility(utility_args)
        utility.execute()

        print("Success! %(project_name)s has been created" %
              {'project_name': project_name})  # noqa
예제 #29
0
def startproject(c):
    utility = ManagementUtility(["django-admin", "startproject", site_name])
    utility.execute()
예제 #30
0
파일: tests.py 프로젝트: zzqq0103/django
 def _run_autocomplete(self):
     util = ManagementUtility(argv=sys.argv)
     with captured_stdout() as stdout:
         with suppress(SystemExit):
             util.autocomplete()
     return stdout.getvalue().strip().split('\n')