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) use_argparse = False try: use_argparse = command.use_argparse except AttributeError: pass dumper.start_command(command_name=command_name, command_help_text=str(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, command.create_parser("", command_name)) dumper.close_command()
def handle(self, *args, **options): """ Run and profile the specified management command with the provided arguments. """ if 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) command_name = args[0] utility = ManagementUtility(sys.argv) command = utility.fetch_command(command_name) parser = command.create_parser(sys.argv[0], command_name) command_options, command_args = parser.parse_args(list(args[1:])) if command_name == 'test' and 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': import yet_another_django_profiler.yadp_yappi as yadp_yappi profiler = yadp_yappi.YappiProfile() else: profiler = cProfile.Profile() atexit.register(output_results, profiler, options, self.stdout) profiler.runcall(call_command, command_name, *command_args, **command_options.__dict__) sys.exit(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: continue # TODO: Log somehow. Probably print to output? assert isinstance(command, BaseCommand) use_argparse = False try: use_argparse = command.use_argparse except AttributeError: pass dumper.start_command(command_name=command_name, command_help_text=str( 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, command.create_parser("", command_name)) dumper.close_command()
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(): command = utility.fetch_command(command_name) 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: opt_type = opt.type if opt.type in Option.TYPES else "" # Empty for unknown # 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( opt_type=opt_type, choices=opt.choices, long_opt_names=opt._long_opts, short_opt_names=opt._short_opts, help_text=opt.help, num_of_args=opt.nargs, ) dumper.close_command()
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()
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)
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()
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)
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)
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()
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()
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()
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()
import time import sys import os sys.path.append(os.getcwd()+"/site-packages") sys.path.append(os.getcwd()) from django.core.management import setup_environ, ManagementUtility import settings setup_environ(settings) if __name__ == '__main__': utility = ManagementUtility() command = utility.fetch_command('runwsgiserver') command.execute(use_reloader=False)
def django_init(self): """ Checks for the required data and initializes the application. """ # manage.py os.environ.setdefault("DJANGO_SETTINGS_MODULE", SETTINGS) # django.core.management.execute_from_command_line(argv=ARGS) """ A simple method that runs a ManagementUtility. """ from django.core.management import ManagementUtility utility = ManagementUtility(ARGS) # utility.execute() """ Given the command-line arguments, this figures out which subcommand is being run, creates a parser appropriate to that command, and runs it. """ from django.core.management import LaxOptionParser # For backwards compatibility: get_version() used to be in this module. from django import get_version from django.core.management.base import BaseCommand, handle_default_options # Preprocess options to extract --settings and --pythonpath. # These options could affect the commands that are available, so they # must be processed early. parser = LaxOptionParser(usage="%prog subcommand [options] [args]", version=get_version(), option_list=BaseCommand.option_list) utility.autocomplete() try: options, args = parser.parse_args(utility.argv) handle_default_options(options) except: pass # Ignore any option errors at this point. subcommand = utility.argv[1] klass = utility.fetch_command(subcommand) # klass.run_from_argv(utility.argv) """ Set up any environment changes requested (e.g., Python path and Django settings), then run this command. If the command raises a ``CommandError``, intercept it and print it sensibly to stderr. If the ``--traceback`` option is present or the raised ``Exception`` is not ``CommandError``, raise it. """ from django.core.management.base import CommandError parser = klass.create_parser(utility.argv[0], utility.argv[1]) options, args = parser.parse_args(utility.argv[2:]) handle_default_options(options) options = options.__dict__ # klass.execute(*args, **options.__dict__) """ Try to execute this command, performing model validation if needed (as controlled by the attribute ``klass.requires_model_validation``, except if force-skipped). """ from django.core.management.base import OutputWrapper klass.stdout = OutputWrapper(options.get('stdout', sys.stdout)) klass.stderr = OutputWrapper(options.get('stderr', sys.stderr), klass.style.ERROR) #klass.can_import_settings = True from django.conf import settings saved_locale = None #klass.leave_locale_alone = False # Only mess with locales if we can assume we have a working # settings file, because django.utils.translation requires settings # (The final saying about whether the i18n machinery is active will be # found in the value of the USE_I18N setting) #klass.can_import_settings = True # Switch to US English, because django-admin.py creates database # content like permissions, and those shouldn't contain any # translations. from django.utils import translation saved_locale = translation.get_language() translation.activate('en-us') try: # Validation is called explicitly each time the server is reloaded. #klass.requires_model_validation = False addrport = args[0] print 'addrport %s' % addrport args = args[1:] # klass.handle(addrport='', *args, **options) import re from django.core.management.commands.runserver import naiveip_re, DEFAULT_PORT from django.conf import settings if not settings.DEBUG and not settings.ALLOWED_HOSTS: raise CommandError('You must set settings.ALLOWED_HOSTS if DEBUG is False.') klass.use_ipv6 = options.get('use_ipv6') if klass.use_ipv6 and not socket.has_ipv6: raise CommandError('Your Python does not support IPv6.') if args: raise CommandError('Usage is runserver %s' % klass.args) klass._raw_ipv6 = False if not addrport: klass.addr = '' klass.port = DEFAULT_PORT else: m = re.match(naiveip_re, addrport) if m is None: raise CommandError('"%s" is not a valid port number ' 'or address:port pair.' % addrport) klass.addr, _ipv4, _ipv6, _fqdn, klass.port = m.groups() if not klass.port.isdigit(): raise CommandError("%r is not a valid port number." % klass.port) if klass.addr: if _ipv6: klass.addr = klass.addr[1:-1] klass.use_ipv6 = True klass._raw_ipv6 = True elif klass.use_ipv6 and not _fqdn: raise CommandError('"%s" is not a valid IPv6 address.' % klass.addr) if not klass.addr: klass.addr = '::1' if klass.use_ipv6 else '127.0.0.1' klass._raw_ipv6 = bool(klass.use_ipv6) # klass.run(*args, **options) """ Runs the server, using the autoreloader if needed """ #from django.utils import autoreload use_reloader = options.get('use_reloader') if use_reloader: # use queue and threading to start httpd # skip for now print 'reloader bypassed for Windows service' pass # klass.inner_run(*args, **options) import errno import socket from django.utils import six from django.utils.six.moves import socketserver from django.core.servers.basehttp import WSGIServer, WSGIRequestHandler from datetime import datetime from django.conf import settings from django.utils import translation threading = options.get('use_threading') shutdown_message = options.get('shutdown_message', '') quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C' klass.stdout.write("Validating models...\n\n") klass.validate(display_num_errors=True) klass.stdout.write(( "%(started_at)s\n" "Django version %(version)s, using settings %(settings)r\n" "Starting development server at http://%(addr)s:%(port)s/\n" "Quit the server with %(quit_command)s.\n" ) % { "started_at": datetime.now().strftime('%B %d, %Y - %X'), "version": klass.get_version(), "settings": settings.SETTINGS_MODULE, "addr": '[%s]' % klass.addr if klass._raw_ipv6 else klass.addr, "port": klass.port, "quit_command": quit_command, }) # django.core.management.base forces the locale to en-us. We should # set it up correctly for the first request (particularly important # in the "--noreload" case). translation.activate(settings.LANGUAGE_CODE) try: handler = klass.get_handler(*args, **options) # run(addr=klass.addr, port=int(klass.port), wsgi_handler=handler, # ipv6=klass.use_ipv6, threading=threading) server_address = (klass.addr, int(klass.port)) if threading: httpd_cls = type(str('WSGIServer'), (socketserver.ThreadingMixIn, WSGIServer), {}) else: httpd_cls = WSGIServer httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=klass.use_ipv6) httpd.set_app(handler) except socket.error as e: # Use helpful error messages instead of ugly tracebacks. ERRORS = { errno.EACCES: "You don't have permission to access that port.", errno.EADDRINUSE: "That port is already in use.", errno.EADDRNOTAVAIL: "That IP address can't be assigned-to.", } try: error_text = ERRORS[e.errno] except KeyError: error_text = str(e) klass.stderr.write("Error: %s" % error_text) # Need to use an OS exit because sys.exit doesn't work in a thread os._exit(1) finally: if saved_locale is not None: translation.activate(saved_locale) return httpd