Example #1
0
 def handle(self, *args, **options):
     self._setup_logger(options["logfile"], options["loglevel"])
     replay_failed = options["replay_failed"]
     use_reloader = options["use_reloader"]
     self.pidfile = options["pidfile"]
     self.workers = options["workers"]
     if self.workers:
         self.workers = int(self.workers)
     else:
         # avoid a value of 0
         self.workers = None
     self.stop_check_interval = 1
     stop_requested = options["stop_requested"]
     if stop_requested:
         self._request_stop()
         return
     signal.signal(signal.SIGTERM, signal_handler)
     self._handle_pidfile()
     self.daemonize = options["daemonize"]
     if self.daemonize:
         become_daemon()
     # close the db connection before spawning the pool workers so each gets a new one
     db.close_connection()
     global pool
     pool = multiprocessing.Pool(self.workers)
     if use_reloader:
         autoreload.main(lambda: self._handle(use_reloader, replay_failed))
     else:
         self._handle(use_reloader, replay_failed)
Example #2
0
 def server(self, bind='127.0.0.1', port=8000):
     self.configure()
     if os.environ.get("RUN_MAIN") != "true":
         from django.contrib.auth.models import User
         if not User.objects.filter(is_superuser=True).exists():
             usr = User()
             usr.username = '******'
             usr.email = '*****@*****.**'
             usr.set_password('admin')
             usr.is_superuser = True
             usr.is_staff = True
             usr.is_active = True
             usr.save()
             print('')
             print(
                 "A admin user (username: admin, password: admin) has been created.")
             print('')
     from django.contrib.staticfiles.management.commands import runserver
     rs = runserver.Command()
     rs.stdout = sys.stdout
     rs.stderr = sys.stderr
     rs.use_ipv6 = False
     rs._raw_ipv6 = False
     rs.addr = bind
     rs.port = port
     autoreload.main(rs.inner_run, (), {
         'addrport': '%s:%s' % (bind, port),
         'insecure_serving': True,
         'use_threading': True
     })
Example #3
0
 def handle(self, *args, **options):
     use_reloader = options.get("use_reloader", True)
     replay_failed = options.get("replay_failed", False)
     if use_reloader:
         autoreload.main(lambda: self._handle(use_reloader, replay_failed))
     else:
         self._handle(use_reloader, replay_failed)
Example #4
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        logging.basicConfig()
        logger = logging.getLogger('process_queue')

        if not settings.USING_RABBITMQ:
            logger.error("Cannot run a queue processor when USING_RABBITMQ is False!")
            sys.exit(1)

        def run_threaded_workers(logger):
            for queue_name in get_active_worker_queues():
                logger.info('launching queue worker thread ' + queue_name)
                td = Threaded_worker(queue_name)
                td.start()

        if options['all']:
            autoreload.main(run_threaded_workers, (logger,))
        else:
            queue_name = options['queue_name']
            worker_num = options['worker_num']

            logger.info("Worker %d connecting to queue %s" % (worker_num, queue_name))
            worker = get_worker(queue_name)
            worker.setup()

            def signal_handler(signal, frame):
                logger.info("Worker %d disconnecting from queue %s" % (worker_num, queue_name))
                worker.stop()
                sys.exit(0)
            signal.signal(signal.SIGTERM, signal_handler)
            signal.signal(signal.SIGINT, signal_handler)

            worker.start()
Example #5
0
def server(bind='127.0.0.1', port=8000, migrate_cmd=False):
    from cms.utils.compat.dj import get_user_model
    from django.utils import autoreload

    if os.environ.get("RUN_MAIN") != "true":
        _create_db(migrate_cmd)
        User = get_user_model()
        if not User.objects.filter(is_superuser=True).exists():
            usr = create_user('admin', '*****@*****.**', 'admin', is_staff=True, is_superuser=True)
            print('')
            print('A admin user (username: %s, password: admin) has been created.' % usr.get_username())
            print('')
    from django.contrib.staticfiles.management.commands import runserver
    rs = runserver.Command()
    rs.stdout = sys.stdout
    rs.stderr = sys.stderr
    rs.use_ipv6 = False
    rs._raw_ipv6 = False
    rs.addr = bind
    rs.port = port
    autoreload.main(rs.inner_run, (), {
        'addrport': '%s:%s' % (bind, port),
        'insecure_serving': True,
        'use_threading': True
    })
Example #6
0
 def handle(self, *args, **options):
     use_reloader = options.get('use_reloader', True)
     
     if use_reloader:
         autoreload.main(lambda: self._handle())
     else:
         self._handle()
Example #7
0
    def handle(self, *args, **options):
        from django.conf import settings

        if settings.DEBUG:
            autoreload.main(self.inner_run, None, options)
        else:
            self.watch()
Example #8
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        logging.basicConfig()
        logger = logging.getLogger('process_queue')

        def exit_with_three(signal, frame):
            # type: (int, FrameType) -> None
            """
            This process is watched by Django's autoreload, so exiting
            with status code 3 will cause this process to restart.
            """
            logger.warn("SIGUSR1 received. Restarting this queue processor.")
            sys.exit(3)

        if not settings.USING_RABBITMQ:
            # Make the warning silent when running the tests
            if settings.TEST_SUITE:
                logger.info("Not using RabbitMQ queue workers in the test suite.")
            else:
                logger.error("Cannot run a queue processor when USING_RABBITMQ is False!")
            sys.exit(1)

        def run_threaded_workers(queues, logger):
            # type: (List[str], logging.Logger) -> None
            cnt = 0
            for queue_name in queues:
                if not settings.DEVELOPMENT:
                    logger.info('launching queue worker thread ' + queue_name)
                cnt += 1
                td = Threaded_worker(queue_name)
                td.start()
            assert len(queues) == cnt
            logger.info('%d queue worker threads were launched' % (cnt,))

        if options['all']:
            signal.signal(signal.SIGUSR1, exit_with_three)
            autoreload.main(run_threaded_workers, (get_active_worker_queues(), logger))
        elif options['multi_threaded']:
            signal.signal(signal.SIGUSR1, exit_with_three)
            queues = options['multi_threaded']
            autoreload.main(run_threaded_workers, (queues, logger))
        else:
            queue_name = options['queue_name']
            worker_num = options['worker_num']

            logger.info("Worker %d connecting to queue %s" % (worker_num, queue_name))
            worker = get_worker(queue_name)
            worker.setup()

            def signal_handler(signal, frame):
                # type: (int, FrameType) -> None
                logger.info("Worker %d disconnecting from queue %s" % (worker_num, queue_name))
                worker.stop()
                sys.exit(0)
            signal.signal(signal.SIGTERM, signal_handler)
            signal.signal(signal.SIGINT, signal_handler)
            signal.signal(signal.SIGUSR1, signal_handler)

            worker.start()
Example #9
0
def server(bind='127.0.0.1', port=8000, migrate_cmd=False, app_name=None, migration=None):
    if os.environ.get("RUN_MAIN") != "true":
        from django.contrib.auth import get_user_model  # must be imported lazily
        if DJANGO_1_6:
            from south.management.commands import syncdb, migrate
            if migrate_cmd:
                syncdb.Command().handle_noargs(interactive=False, verbosity=1,
                                               database='default')
                if app_name:
                    migrate.Command().handle(interactive=False, verbosity=1, app=app_name,
                                             target=migration)
                else:
                    migrate.Command().handle(interactive=False, verbosity=1)
            else:
                syncdb.Command().handle_noargs(interactive=False, verbosity=1,
                                               database='default',
                                               migrate=False, migrate_all=True)
                migrate.Command().handle(interactive=False, verbosity=1,
                                         fake=True)
        else:
            if app_name:
                call_command("migrate", app_name, migration, database='default')
            else:
                call_command("migrate", database='default')
        User = get_user_model()
        if not User.objects.filter(is_superuser=True).exists():
            usr = User()

            if(User.USERNAME_FIELD != 'email'):
                setattr(usr, User.USERNAME_FIELD, 'admin')

            usr.email = '*****@*****.**'
            usr.set_password('admin')
            usr.is_superuser = True
            usr.is_staff = True
            usr.is_active = True
            usr.save()
            print('')
            print("A admin user (username: admin, password: admin) "
                  "has been created.")
            print('')
    from django.contrib.staticfiles.management.commands import runserver
    rs = runserver.Command()
    try:
        from django.core.management.base import OutputWrapper
        rs.stdout = OutputWrapper(sys.stdout)
        rs.stderr = OutputWrapper(sys.stderr)
    except ImportError:
        rs.stdout = sys.stdout
        rs.stderr = sys.stderr
    rs.use_ipv6 = False
    rs._raw_ipv6 = False
    rs.addr = bind
    rs.port = port
    autoreload.main(rs.inner_run, (), {
        'addrport': '%s:%s' % (bind, port),
        'insecure_serving': True,
        'use_threading': True
    })
Example #10
0
    def handle(self, addrport='', *args, **options):
        import django
        from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
        from django.core.handlers.wsgi import WSGIHandler
        if args:
            raise CommandError('Usage is runserver %s' % self.args)
        if not addrport:
            addr = ''
            port = '8000'
        else:
            try:
                addr, port = addrport.split(':')
            except ValueError:
                addr, port = '', addrport
        if not addr:
            addr = '127.0.0.1'

        if not port.isdigit():
            raise CommandError("%r is not a valid port number." % port)

        use_reloader = options.get('use_reloader', True)
        admin_media_path = options.get('admin_media_path', '')
        shutdown_message = options.get('shutdown_message', '')
        quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'

        def inner_run():
            from django.conf import settings
            print("Validating models...")
            self.validate(display_num_errors=True)
            print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE))
            print("Development server is running at http://%s:%s/" % (addr, port))
            print("Quit the server with %s." % quit_command)
            try:
                path = admin_media_path or django.__path__[0] + '/contrib/admin/media'
                handler = AdminMediaHandler(WSGIHandler(), path)
                run(addr, int(port), handler)
            except WSGIServerException as e:
                # Use helpful error messages instead of ugly tracebacks.
                ERRORS = {
                    13: "You don't have permission to access that port.",
                    98: "That port is already in use.",
                    99: "That IP address can't be assigned-to.",
                }
                try:
                    error_text = ERRORS[e.args[0].args[0]]
                except (AttributeError, KeyError):
                    error_text = str(e)
                sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n')
                # Need to use an OS exit because sys.exit doesn't work in a thread
                os._exit(1)
            except KeyboardInterrupt:
                if shutdown_message:
                    print(shutdown_message)
                sys.exit(0)
        if use_reloader:
            from django.utils import autoreload
            autoreload.main(inner_run)
        else:
            inner_run()
Example #11
0
def main(*args, **kwargs):
    args = sys.argv
    config = None
    conf = {}

    config = utils.get_config_file()
    parser = OptionParser()
    parser.add_option("-t", "--debug-toolbar", dest="toolbar",
                      action="store_true", default=False)
    parser.add_option("-i", "--non-interactive", dest="interactive",
                      action="store_true", default=False)
    parser.add_option("-m", "--no-media", dest="no_media",
                      action="store_true", default=False)
    parser.add_option("--host", dest="host",
                      action="store", default="0.0.0.0")
    parser.add_option("-p", "--port", dest="port",
                      action="store", default="8000")
    parser.add_option("--threads", dest="threads",
                      action="store", default="2")
    options, args = parser.parse_args()

    if options.toolbar:
        conf['toolbar'] = True

    if options.interactive:
        conf['no_error'] = True

    if options.no_media:
        conf['no_media'] = True

    if config is None:
        config = utils.get_config_file()

    if 'request' in args:
        app = make_app({}, no_error=True)
        req = webob.Request.blank(args[1])
        try:
            resp = req.get_response(app)
        except:
            raise
        else:
            if resp.charset:
                body = resp.text
            else:
                body = resp.body
            if not isinstance(body, str):
                body = body.decode('utf8')
            print(body)
    else:
        config = utils.get_config_file()
        app = make_app(conf)
        from django.utils import autoreload
        autoreload.main(serve, (app,), {
            'expose_tracebacks': True,
            'host': options.host,
            'port': options.port,
            'threads': options.threads,
        })
        return
Example #12
0
 def handle(self, *args, **options):
     self._setup_logger(options.get('logfile', None), options.get('loglevel', 'info'))
     use_reloader = options.get('use_reloader', True)
     replay_failed = options.get('replay_failed', False)
     if use_reloader:
         autoreload.main(lambda: self._handle(use_reloader, replay_failed))
     else:
         self._handle(use_reloader, replay_failed)
Example #13
0
    def handle(self, *args, **options):
        if len(args) == 1:
            host, port = args[0].split(':')
            port = int(port)
        else:
            host, port = 'localhost', 8000

        autoreload.main(self.main, (host,port))
Example #14
0
    def run(self, **options):
        """Run the server, using the autoreloader if needed."""
        use_reloader = options['use_reloader']

        if use_reloader:
            autoreload.main(self.inner_run, None, options)
        else:
            self.inner_run(None, **options)
Example #15
0
 def run(self):
     from sentry.wsgi import application
     def inner_run():
         wsgi.server(eventlet.listen((self.host, self.port)), application)
         
     if self.debug:
         from django.utils import autoreload
         autoreload.main(inner_run)
     else:
         inner_run()
Example #16
0
def runcelery(*args, **options):
  #CeleryCommand().handle_argv(['worker', '--app=desktop.celery', '--concurrency=1', '--loglevel=DEBUG'])
  opts = ['runcelery', 'worker', '--app=' + options['app'], '--concurrency=' + str(options['concurrency']), '--loglevel=' + options['loglevel']]
  drop_privileges_if_necessary(CELERY_OPTIONS)
  if conf.DEV.get():
    autoreload.main(celery_main, (opts,))
  else:
    celery_main(opts)
  LOG.error("Failed to exec '%s' with argument '%s'" % args)
  sys.exit(-1)
    def handle(self, *args, **options):
        """
        Handles serviced command with autoreload feature.
        """
        use_reloader = options.get('use_reloader')

        if use_reloader:
            autoreload.main(self.inner_run, args, options)
        else:
            self.inner_run(*args, **options)
Example #18
0
    def run(self, **options):
        """
        Runs the server, using the autoreloader if needed
        """
        use_reloader = options.get("use_reloader")

        if use_reloader:
            autoreload.main(self.inner_run, None, options)
        else:
            self.inner_run(None, **options)
Example #19
0
    def testroute(self, conf, *args):
        """ Uses Django's autoreload functionality to automatically restart the 
        routing server when code is changed.  """
        from django.utils import autoreload

        def run():
            self.route(conf, args)

        # run our route command using Django's autoreload
        autoreload.main(run)
Example #20
0
    def run(self, *args, **options):
        """
        Runs the server, using the autoreloader if needed
        """
        use_reloader = options.get('use_reloader')

        if use_reloader:
            autoreload.main(self.inner_run, args, options)
        else:
            self.inner_run(*args, **options)
Example #21
0
    def run(self, **options):
        """
        Runs the server, using the autoreloader if needed
        """
        use_reloader = options.get('use_reloader', False)
        use_multiple = options.get('use_multiple', False)

        if use_reloader and not use_multiple:
            autoreload.main(self.inner_run, (), options)
        else:
            self.inner_run(**options)
Example #22
0
 def handle(self, port=None, *args, **options):
     if not port:
         self.port = DEFAULT_PORT
     else:
         self.port = port
     if not self.port.isdigit():
         raise CommandError("%r is not a valid port number." % self.port)        
     if settings.DEBUG:
         autoreload.main(self.inner_run, args, options)
     else:
         self.inner_run(*args, **options)
Example #23
0
 def handle(self, *args, **options):
     if options['port']:
         self.port = options['port']
     else:
         self.port = self.default_port
     if not self.port.isdigit():
         raise CommandError("%r is not a valid port number." % self.port)
     if settings.DEBUG:
         autoreload.main(self.inner_run, args, options)
     else:
         self.inner_run(*args, **options)
Example #24
0
def server(bind='127.0.0.1', port=8000, migrate_cmd=False):
    if os.environ.get("RUN_MAIN") != "true":
        from cms.utils.compat.dj import get_user_model
        if DJANGO_1_6:
            from south.management.commands import syncdb, migrate
            if migrate_cmd:
                syncdb.Command().handle_noargs(interactive=False,
                                               verbosity=1,
                                               database='default')
                migrate.Command().handle(interactive=False, verbosity=1)
            else:
                syncdb.Command().handle_noargs(interactive=False,
                                               verbosity=1,
                                               database='default',
                                               migrate=False,
                                               migrate_all=True)
                migrate.Command().handle(interactive=False,
                                         verbosity=1,
                                         fake=True)
        else:
            call_command("migrate", database='default')
        User = get_user_model()
        if not User.objects.filter(is_superuser=True).exists():
            usr = User()

            if (User.USERNAME_FIELD != 'email'):
                setattr(usr, User.USERNAME_FIELD, 'admin')

            usr.email = '*****@*****.**'
            usr.set_password('admin')
            usr.is_superuser = True
            usr.is_staff = True
            usr.is_active = True
            usr.save()
            print('')
            print(
                "A admin user (username: admin, password: admin) has been created."
            )
            print('')
    from django.contrib.staticfiles.management.commands import runserver
    rs = runserver.Command()
    rs.stdout = sys.stdout
    rs.stderr = sys.stderr
    rs.use_ipv6 = False
    rs._raw_ipv6 = False
    rs.addr = bind
    rs.port = port
    autoreload.main(
        rs.inner_run, (), {
            'addrport': '%s:%s' % (bind, port),
            'insecure_serving': True,
            'use_threading': True
        })
Example #25
0
def server(bind='127.0.0.1',
           port=8000,
           migrate_cmd=False,
           verbose=1):  # pragma: no cover
    try:
        from channels.log import setup_logger
        from channels.management.commands import runserver
        logger = setup_logger('django.channels', 1)
        use_channels = True
    except ImportError:
        from django.contrib.staticfiles.management.commands import runserver
        use_channels = False
        logger = None

    if os.environ.get('RUN_MAIN') != 'true':
        _create_db(migrate_cmd)
        User = get_user_model()
        if not User.objects.filter(is_superuser=True).exists():
            usr = create_user('admin',
                              '*****@*****.**',
                              'admin',
                              is_staff=True,
                              is_superuser=True)
            print('')
            print('A admin user (username: %s, password: admin) '
                  'has been created.' % usr.get_username())
            print('')
    rs = runserver.Command()
    try:
        from django.core.management.base import OutputWrapper
        rs.stdout = OutputWrapper(sys.stdout)
        rs.stderr = OutputWrapper(sys.stderr)
    except ImportError:
        rs.stdout = sys.stdout
        rs.stderr = sys.stderr
    rs.use_ipv6 = False
    rs._raw_ipv6 = False
    rs.addr = bind
    rs.port = port
    if logger:
        rs.logger = logger
    if use_channels:
        rs.http_timeout = 60
        rs.websocket_handshake_timeout = 5
    autoreload.main(
        rs.inner_run, (), {
            'addrport': '%s:%s' % (bind, port),
            'insecure_serving': True,
            'use_static_handler': True,
            'use_threading': True,
            'verbosity': verbose,
            'use_reloader': True
        })
Example #26
0
    def run(self, **options):
        """Run the server, using the autoreloader if needed."""
        use_reloader = options['use_reloader']

        # 1 main对inner_run做了一层包装, 真正的 HTTP Server处理逻辑见inner_run函数
        if use_reloader:
            # 1.0 未设置--noreload时执行, 封装 self.inner_run, 创建一个新的子线程
            MY(4, '\n\tReloader:', options)
            autoreload.main(self.inner_run, None, options)
        else:
            # 1.1 inner_run
            MY(4, '\n\tUnloader(inner):', options)
            self.inner_run(None, **options)
Example #27
0
def start_server_with_admin(options):
    """
    Start CherryPy server
    """

    global SERVER

    if options['daemonize'] == '1' and options['server_user'] and options[
            'server_group']:
        #ensure the that the daemon runs as specified user
        change_uid_gid(options['server_user'], options['server_group'])

    from cherrypy.wsgiserver import CherryPyWSGIServer as Server
    #from wsgiserver import CherryPyWSGIServer as Server
    from django.core.handlers.wsgi import WSGIHandler
    from django.core.servers.basehttp import AdminMediaHandler

    path = django.__path__[0] + '/contrib/admin/media'
    #dispatcher = StaticFilesHandler(AdminMediaHandler(WSGIHandler(), path))
    threads = int(options['threads'])
    SERVER = Server(
        (options['host'], int(options['port'])),
        WSGIHandler(),
        numthreads=threads,
        max=threads,
        server_name=options['server_name'],
        verbose=int(options.get('verbose', 0)),
        shutdown_timeout=int(options.get('shutdown_timeout', 10)),
        request_queue_size=int(options.get('request_queue_size', 5)),
    )

    def inner_run():
        print "Validating models..."
        command = Command()
        command.stdout = sys.stdout
        command.validate(display_num_errors=True)

        print 'starting server with options %s' % options
        print "\nDjango version %s, using settings %r" % (
            django.get_version(), settings.SETTINGS_MODULE)
        print "Development server is running at http://%s:%s/" % (
            options['host'], options['port'])
        print "Quit the server with <CTRL>+C."
        try:
            SERVER.start()
        except KeyboardInterrupt:
            print 'closing...'
            SERVER.stop()

    from django.utils import autoreload
    autoreload.main(inner_run)
 def run(self, **options):
     """Run the server, using the autoreloader if needed."""
     if self.development_mode:
         if hasattr(autoreload, "run_with_reloader"):
             autoreload.run_with_reloader(self.inner_run, **options)
         else:
             autoreload.main(self.inner_run, None, options)
     else:
         self.stdout.write((
             "Starting gRPC server at %(address)s\n"
         ) % {
             "address": self.address,
         })
         self._serve()
Example #29
0
def runcelery(*args, **options):
    #CeleryCommand().handle_argv(['worker', '--app=desktop.celery', '--concurrency=1', '--loglevel=DEBUG'])
    opts = [
        'runcelery', 'worker', '--app=' + options['app'],
        '--concurrency=' + str(options['concurrency']),
        '--loglevel=' + options['loglevel']
    ]
    drop_privileges_if_necessary(CELERY_OPTIONS)
    if conf.DEV.get():
        autoreload.main(celery_main, (opts, ))
    else:
        celery_main(opts)
    LOG.error("Failed to exec '%s' with argument '%s'" % args)
    sys.exit(-1)
Example #30
0
    def run(self, *args, **options):
        """
        Runs the server, using the autoreloader if needed
        """
        use_reloader = options.get('use_reloader')
        num_process = int(options.get('num_process'))

        # Only enable auto-reloader if we are running in single-process mode
        # (the default). Otherwise, we disable it because it only leads to all
        # sorts of problems.
        if use_reloader and num_process == 1:
            autoreload.main(self.inner_run, args, options)
        else:
            self.inner_run(*args, **options)
Example #31
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        logging.basicConfig()
        logger = logging.getLogger('process_queue')

        if not settings.USING_RABBITMQ:
            # Make the warning silent when running the tests
            if settings.TEST_SUITE:
                logger.info(
                    "Not using RabbitMQ queue workers in the test suite.")
            else:
                logger.error(
                    "Cannot run a queue processor when USING_RABBITMQ is False!"
                )
            sys.exit(1)

        def run_threaded_workers(logger):
            # type: (logging.Logger) -> None
            cnt = 0
            for queue_name in get_active_worker_queues():
                if not settings.DEVELOPMENT:
                    logger.info('launching queue worker thread ' + queue_name)
                cnt += 1
                td = Threaded_worker(queue_name)
                td.start()
            logger.info('%d queue worker threads were launched' % (cnt, ))

        if options['all']:
            autoreload.main(run_threaded_workers, (logger, ))
        else:
            queue_name = options['queue_name']
            worker_num = options['worker_num']

            logger.info("Worker %d connecting to queue %s" %
                        (worker_num, queue_name))
            worker = get_worker(queue_name)
            worker.setup()

            def signal_handler(signal, frame):
                # type: (int, FrameType) -> None
                logger.info("Worker %d disconnecting from queue %s" %
                            (worker_num, queue_name))
                worker.stop()
                sys.exit(0)

            signal.signal(signal.SIGTERM, signal_handler)
            signal.signal(signal.SIGINT, signal_handler)

            worker.start()
Example #32
0
    def run(self):
        from sentry.wsgi import application

        def inner_run():
            # Install eventlet patches after everything else has been run,
            # and inside our server thread
            patcher.monkey_patch()

            wsgi.server(eventlet.listen((self.host, self.port)), application)

        if self.debug:
            from django.utils import autoreload
            autoreload.main(inner_run)
        else:
            inner_run()
Example #33
0
 async def run(self, **options):
     """Run the server, using the autoreloader if needed."""
     if self.development_mode:
         if hasattr(autoreload, "run_with_reloader"):
             autoreload.run_with_reloader(self.inner_run, **options)
         else:
             autoreload.main(self.inner_run, None, options)
     else:
         logger.info(
             ("Starting async gRPC server at %(address)s\n") % {
                 "address": self.address,
             },
             extra={"emit_to_server": False},
         )
         await self._serve()
    def handle(self, *args, **options):
        if os.path.exists(INPROGRESS_FILE):
            os.remove(INPROGRESS_FILE)

        def inner_run():
            try:
                open(INPROGRESS_FILE, 'wb').close()

                test_command = get_test_command()
                test_command.handle(*args, **options)
            finally:
                if os.path.exists(INPROGRESS_FILE):
                    os.remove(INPROGRESS_FILE)

        autoreload.main(inner_run)
Example #35
0
    def handle(self, *args, **options):
        if os.path.exists(INPROGRESS_FILE):
            os.remove(INPROGRESS_FILE)

        def inner_run():
            try:
                open(INPROGRESS_FILE, 'wb').close()

                test_command = get_test_command()
                test_command.handle(*args, **options)
            finally:
                if os.path.exists(INPROGRESS_FILE):
                    os.remove(INPROGRESS_FILE)

        autoreload.main(inner_run)
Example #36
0
def start_server_with_admin(options):
    """
    Start CherryPy server
    """
    
    global SERVER
    
    if options['daemonize'] == '1' and options['server_user'] and options['server_group']:
        #ensure the that the daemon runs as specified user
        change_uid_gid(options['server_user'], options['server_group'])
    
    from cherrypy.wsgiserver import CherryPyWSGIServer as Server
    #from wsgiserver import CherryPyWSGIServer as Server
    from django.core.handlers.wsgi import WSGIHandler
    from django.core.servers.basehttp import AdminMediaHandler

    path = django.__path__[0] + '/contrib/admin/media'
    #dispatcher = StaticFilesHandler(AdminMediaHandler(WSGIHandler(), path))
    threads = int(options['threads'])
    SERVER = Server(
        (options['host'], int(options['port'])),
        WSGIHandler(),
        numthreads=threads,
        max=threads, 
        server_name=options['server_name'],
        verbose=int(options.get('verbose', 0)),
        shutdown_timeout = int(options.get('shutdown_timeout',10)),
       
        request_queue_size = int(options.get('request_queue_size', 5)),
    )
    def inner_run():
        print "Validating models..."
        command = Command()
        command.stdout = sys.stdout
        command.validate(display_num_errors=True)

        print 'starting server with options %s' % options
        print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
        print "Development server is running at http://%s:%s/" % (options['host'], options['port'])
        print "Quit the server with <CTRL>+C."
        try:
            SERVER.start()
        except KeyboardInterrupt:
            print 'closing...'
            SERVER.stop()

    from django.utils import autoreload
    autoreload.main(inner_run)
Example #37
0
    def handle(self, *test_labels, **options):
        self.config = {}

        options['verbosity'] = int(options.get('verbosity'))
        if options.get('liveserver') is not None:
            os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = options[
                'liveserver']
            del options['liveserver']

        if os.path.isfile(self.config_file):
            print "\n * Running tests!\n"
            from django.conf import settings
            with open(self.config_file, 'r') as fhl:
                self.config = json.loads(fhl.read())
            for (orig_name, test_name) in self.config.get('db', {}).items():
                for (db_name, conf) in settings.DATABASES.items():
                    #self.flush_database(name)
                    if conf["NAME"] == orig_name:
                        conf["NAME"] = test_name
                        #print "DB SET: %s -> %s" % (orig_name, test_name)

            settings.DEBUG = False
        else:
            self.set_title('setup', **options)

            # Make a module we can import and use to re-run at will
            if not os.path.isdir(self.am_path):
                os.makedirs(self.am_path)
            with open(self.am_file, 'w') as fhl:
                fhl.write("# Force tests to reload with this file\n")

            self.config = self.setup_databases(**options)
            self.save_config()

            print "\n -= Testing Service Running; use [CTRL]+[C] to exit =-\n"

        sys.path.append(self.am_path)
        try:
            __import__(self.auto_module[-1])
        except ImportError:
            self.teardown_autotest((([]), []))
            print("Config error, I've cleaned up, please try again.")
            sys.exit(2)

        try:
            autoreload.main(self.inner_run, test_labels, options)
        except OSError:
            print "Exiting autorun."
Example #38
0
    def handle(self, *test_labels, **options):
        self.app = apps.get_app_config('autotest')
        self.config = {}

        options['verbosity'] = int(options.get('verbosity'))
        if options.get('liveserver') is not None:
            os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = options['liveserver']
            del options['liveserver']

        if os.path.isfile(self.config_file):
            print "\n * Running tests!\n"
            from django.conf import settings
            with open(self.config_file, 'r') as fhl:
                self.config = json.loads(fhl.read())
            for (orig_name, test_name) in self.config.get('db', {}).items():
                for (db_name, conf) in settings.DATABASES.items():
                    #self.flush_database(name)
                    if conf["NAME"] == orig_name:
                        conf["NAME"] = test_name
                        #print "DB SET: %s -> %s" % (orig_name, test_name)

            settings.DEBUG = False
        else:
            self.set_title('setup', **options)

            # Make a module we can import and use to re-run at will
            if not os.path.isdir(self.am_path):
                os.makedirs(self.am_path)
            with open(self.am_file, 'w') as fhl:
                fhl.write("# Force tests to reload with this file\n")

            self.config = self.setup_databases(**options)
            self.save_config()

            print "\n -= Testing Service Running; use [CTRL]+[C] to exit =-\n"

        sys.path.append(self.am_path)
        try:
            __import__(self.auto_module[-1])
        except ImportError:
            self.teardown_autotest((([]),[]))
            print("Config error, I've cleaned up, please try again.")
            sys.exit(2)

        try:
            autoreload.main(self.inner_run, test_labels, options)
        except OSError:
            print "Exiting autorun."
Example #39
0
    def handle(self, addrport='', *args, **options):
        use_reloader = options['use_reloader']

        if args:
            raise CommandError('Usage is runevserver %s' % self.args)

        if not addrport:
            addr = ''
            port = '8000'
        else:
            try:
                addr, port = addrport.split(':')
            except ValueError:
                addr, port = '', addrport
        if not addr:
            addr = '127.0.0.1'

        if not port.isdigit():
            raise CommandError("%r is not a valid port number." % port)

        port = int(port)

        quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'

        pid = os.getpid()

        def inner_run():
            from django.conf import settings
            print "Validating models..."
            self.validate(display_num_errors=True)
            print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
            print "Development server is running at http://%s:%s/" % (addr, port)
            print "Quit the server with %s." % quit_command
            try:
                import evserver.main
                evserver.main.main( ['--framework','django', '-l','%s:%s' %(addr,port)] )
                os.kill(pid, signal.SIGKILL)
                os._exit(1)
            except KeyboardInterrupt:
                os.kill(pid, signal.SIGKILL)
                sys.exit(0)

        if use_reloader:
            from django.utils import autoreload
            autoreload.main(inner_run)
        else:
            inner_run()
Example #40
0
def runcelery(*args, **options):
    # Native does not load Hue's config
    # CeleryCommand().handle_argv(['worker', '--app=desktop.celery', '--concurrency=1', '--loglevel=DEBUG'])
    opts = [
        'runcelery', 'worker', '--app=' + options['app'],
        '--concurrency=' + str(options['concurrency']),
        '--loglevel=' + options['loglevel']
    ]
    drop_privileges_if_necessary(CELERY_OPTIONS)

    if conf.DEV.get():
        autoreload.main(celery_main, (opts, ))
    else:
        celery_main(opts)

    LOG.info("Stopping command '%s'" % ' '.join(opts))
    sys.exit(-1)
Example #41
0
def server(bind="127.0.0.1", port=8000, migrate_cmd=False):
    if os.environ.get("RUN_MAIN") != "true":
        from cms.utils.compat.dj import get_user_model

        if DJANGO_1_6:
            from south.management.commands import syncdb, migrate

            if migrate_cmd:
                syncdb.Command().handle_noargs(interactive=False, verbosity=1, database="default")
                migrate.Command().handle(interactive=False, verbosity=1)
            else:
                syncdb.Command().handle_noargs(
                    interactive=False, verbosity=1, database="default", migrate=False, migrate_all=True
                )
                migrate.Command().handle(interactive=False, verbosity=1, fake=True)
        else:
            call_command("migrate", database="default")
        User = get_user_model()
        if not User.objects.filter(is_superuser=True).exists():
            usr = User()

            if User.USERNAME_FIELD != "email":
                setattr(usr, User.USERNAME_FIELD, "admin")

            usr.email = "*****@*****.**"
            usr.set_password("admin")
            usr.is_superuser = True
            usr.is_staff = True
            usr.is_active = True
            usr.save()
            print("")
            print("A admin user (username: admin, password: admin) has been created.")
            print("")
    from django.contrib.staticfiles.management.commands import runserver

    rs = runserver.Command()
    rs.stdout = sys.stdout
    rs.stderr = sys.stderr
    rs.use_ipv6 = False
    rs._raw_ipv6 = False
    rs.addr = bind
    rs.port = port
    autoreload.main(
        rs.inner_run, (), {"addrport": "%s:%s" % (bind, port), "insecure_serving": True, "use_threading": True}
    )
Example #42
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--port', default='8000')
    parser.add_argument('-b', '--bind', default='127.0.0.1')
    args = parser.parse_args()
    new_db = not os.path.exists('cmstestdb.sqlite')
    with temp_dir() as STATIC_ROOT:
        with temp_dir() as MEDIA_ROOT:
            configure(
                "sqlite://localhost/cmstestdb.sqlite",
                ROOT_URLCONF='cms.test_utils.project.urls',
                STATIC_ROOT=STATIC_ROOT,
                MEDIA_ROOT=MEDIA_ROOT,
            )
            if os.environ.get("RUN_MAIN") != "true":
                from django.core.management import call_command
                call_command('syncdb', interactive=False, migrate_all=new_db)
                call_command('migrate', interactive=False, fake=new_db)
                from django.contrib.auth.models import User
                if not User.objects.filter(is_superuser=True).exists():
                    usr = User()
                    usr.username = '******'
                    usr.email = '*****@*****.**'
                    usr.set_password('admin')
                    usr.is_superuser = True
                    usr.is_staff = True
                    usr.is_active = True
                    usr.save()
                    print
                    print "A admin user (username: admin, password: admin) has been created."
                    print
            from django.contrib.staticfiles.management.commands import runserver
            rs = runserver.Command()
            rs.stdout = sys.stdout
            rs.stderr = sys.stderr
            rs.use_ipv6 = False
            rs._raw_ipv6 = False
            rs.addr = args.bind
            rs.port = args.port
            autoreload.main(
                rs.inner_run, (), {
                    'addrport': '%s:%s' % (args.bind, args.port),
                    'insecure_serving': True,
                    'use_threading': True
                })
Example #43
0
    def run(self, *args, **options):
        """
        Runs the server, using the autoreloader if needed
        """
        use_reloader = options.get('use_reloader')
        deploy_type = options.get('deploy_type')

        if deploy_type:
            deploy(deploy_type)
        else:
            deploy()

        if use_reloader:
            if deploy_type:
                autoreload.gen_filenames = gen_filenames
            autoreload.main(self.inner_run, args, options)
        else:
            self.inner_run(*args, **options)
def runwsgiserver(argset=[], **kwargs):
    # Get the options
    options = CPWSGI_OPTIONS.copy()
    options.update(kwargs)
    for x in argset:
        if "=" in x:
            k, v = x.split('=', 1)
        else:
            k, v = x, True
        if v == 'False' or v == 'false':
            v = False
            # print "found false", v
        options[k.lower()] = v

    if "help" in options:
        print CPWSGI_HELP
        return

    if "stop" in options:
        stop_server(options['pidfile'])
        return True

    if options['daemonize']:
        if not options['pidfile']:
            options['pidfile'] = '/var/run/cpwsgi_%s.pid' % options['port']
        stop_server(options['pidfile'])

        from django.utils.daemonize import become_daemon
        if options['workdir']:
            become_daemon(our_home_dir=options['workdir'])
        else:
            become_daemon()

        fp = open(options['pidfile'], 'w')
        fp.write("%d\n" % os.getpid())
        fp.close()
    if options['autoreload']:
        from django.utils import autoreload

    # Start the webserver
    if options['autoreload']:
        autoreload.main(start_server_servestatic, (), options)
    else:
        start_server_servestatic(options)
Example #45
0
    def handle(self, *test_labels, **options):
        options['verbosity'] = int(options.get('verbosity'))
        if options.get('liveserver') is not None:
            os.environ[SRV_ADDR] = options['liveserver']
            del options['liveserver']

        try:
            assert (os.path.isfile(self.config_file))
            print("\n * Running tests!\n")
            from django.conf import settings
            with open(self.config_file, 'r') as fhl:
                try:
                    self.config = json.loads(fhl.read())
                except ValueError:
                    print(" {!} Broken state file from last run, deleting.")
                    os.unlink(self.config_file)
                    assert (False)
            for (orig_name, test_name) in self.config.get('db', {}).items():
                for (_, conf) in settings.DATABASES.items():
                    if conf["NAME"] == orig_name:
                        conf["NAME"] = test_name

            settings.DEBUG = False
        except AssertionError as err:
            set_title('setup')
            self.poke_module()

            self.config = self.setup_databases(**options)
            self.save_config()

            print("\n -= Testing Service Running; use [CTRL]+[C] to exit =-\n")

        sys.path.append(self.am_path)
        try:
            __import__(self.auto_module[-1])
        except ImportError:
            self.teardown_autotest((([]), []))
            sys.stdout.write("Config error, I've cleaned up, try again.\n")
            sys.exit(2)

        try:
            autoreload.main(self.inner_run, test_labels, options)
        except OSError:
            print("Exiting autorun.")
Example #46
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        logging.basicConfig()
        logger = logging.getLogger('process_queue')

        if not settings.USING_RABBITMQ:
            logger.error(
                "Cannot run a queue processor when USING_RABBITMQ is False!")
            sys.exit(1)

        def run_threaded_workers(logger):
            for queue_name in get_active_worker_queues():
                logger.info('launching queue worker thread ' + queue_name)
                td = Threaded_worker(queue_name)
                td.start()

        if options['all']:
            autoreload.main(run_threaded_workers, (logger, ))
        else:
            queue_name = options['queue_name']
            worker_num = options['worker_num']

            logger.info("Worker %d connecting to queue %s" %
                        (worker_num, queue_name))
            worker = get_worker(queue_name)
            worker.setup()

            def signal_handler(signal, frame):
                logger.info("Worker %d disconnecting from queue %s" %
                            (worker_num, queue_name))
                worker.stop()
                sys.exit(0)

            signal.signal(signal.SIGTERM, signal_handler)
            signal.signal(signal.SIGINT, signal_handler)

            worker.start()
Example #47
0
def worker(ignore_unknown_queues, **options):
    """Run background worker instance and autoreload if necessary."""

    from sentry.celery import app

    known_queues = frozenset(c_queue.name
                             for c_queue in app.conf.CELERY_QUEUES)

    if options["queues"] is not None:
        if not options["queues"].issubset(known_queues):
            unknown_queues = options["queues"] - known_queues
            message = "Following queues are not found: %s" % ",".join(
                sorted(unknown_queues))
            if ignore_unknown_queues:
                options["queues"] -= unknown_queues
                click.echo(message)
            else:
                raise click.ClickException(message)

    if options["exclude_queues"] is not None:
        if not options["exclude_queues"].issubset(known_queues):
            unknown_queues = options["exclude_queues"] - known_queues
            message = "Following queues cannot be excluded as they don't exist: %s" % ",".join(
                sorted(unknown_queues))
            if ignore_unknown_queues:
                options["exclude_queues"] -= unknown_queues
                click.echo(message)
            else:
                raise click.ClickException(message)

    if options["autoreload"]:
        from django.utils import autoreload

        # Note this becomes autoreload.run_with_reloader in django 2.2
        autoreload.main(run_worker, kwargs=options)
    else:
        run_worker(**options)
Example #48
0
 def handle(self, *args, **options):
     autoreload.main(restart_celery)
Example #49
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        logging.basicConfig()
        logger = logging.getLogger('process_queue')

        def exit_with_three(signal, frame):
            # type: (int, FrameType) -> None
            """
            This process is watched by Django's autoreload, so exiting
            with status code 3 will cause this process to restart.
            """
            logger.warning(
                "SIGUSR1 received. Restarting this queue processor.")
            sys.exit(3)

        if not settings.USING_RABBITMQ:
            # Make the warning silent when running the tests
            if settings.TEST_SUITE:
                logger.info(
                    "Not using RabbitMQ queue workers in the test suite.")
            else:
                logger.error(
                    "Cannot run a queue processor when USING_RABBITMQ is False!"
                )
            sys.exit(1)

        def run_threaded_workers(queues, logger):
            # type: (List[str], logging.Logger) -> None
            cnt = 0
            for queue_name in queues:
                if not settings.DEVELOPMENT:
                    logger.info('launching queue worker thread ' + queue_name)
                cnt += 1
                td = Threaded_worker(queue_name)
                td.start()
            assert len(queues) == cnt
            logger.info('%d queue worker threads were launched' % (cnt, ))

        if options['all']:
            signal.signal(signal.SIGUSR1, exit_with_three)
            autoreload.main(run_threaded_workers,
                            (get_active_worker_queues(), logger))
        elif options['multi_threaded']:
            signal.signal(signal.SIGUSR1, exit_with_three)
            queues = options['multi_threaded']
            autoreload.main(run_threaded_workers, (queues, logger))
        else:
            queue_name = options['queue_name']
            worker_num = options['worker_num']

            logger.info("Worker %d connecting to queue %s" %
                        (worker_num, queue_name))
            worker = get_worker(queue_name)
            worker.setup()

            def signal_handler(signal, frame):
                # type: (int, FrameType) -> None
                logger.info("Worker %d disconnecting from queue %s" %
                            (worker_num, queue_name))
                worker.stop()
                sys.exit(0)

            signal.signal(signal.SIGTERM, signal_handler)
            signal.signal(signal.SIGINT, signal_handler)
            signal.signal(signal.SIGUSR1, signal_handler)

            worker.start()
Example #50
0
 def handle(self, *args, **options):
     logger.info("Starting development celery worker with autoreload...")
     autoreload.main(restart_celery)
Example #51
0
 def handle(self, *args, **options):
     self.stdout('This command auto reloads. No need to restart...')
     autoreload.main(do_something, args=None, kwargs=None)
Example #52
0
                handler = AdminMediaHandler(base_handler(), admin_media_path)
                if use_werkzeug:
                    run_simple(addr, int(port), DebuggedApplication(handler, True),
                        use_reloader=use_reloader, use_debugger=True)
                else:
                    run(addr, int(port), handler, mixin)
            except WSGIServerException, e:
                # Use helpful error messages instead of ugly tracebacks.
                ERRORS = {
                    13: "You don't have permission to access that port.",
                    98: "That port is already in use.",
                    99: "That IP address can't be assigned-to.",
                }
                try:
                    error_text = ERRORS[e.args[0].args[0]]
                except (AttributeError, KeyError):
                    error_text = str(e)
                sys.stderr.write(self.style.ERROR("Error: %s" % error_text) + '\n')
                # Need to use an OS exit because sys.exit doesn't work in a thread
                os._exit(1)
            except KeyboardInterrupt:
                if shutdown_message:
                    print shutdown_message
                sys.exit(0)

        if use_reloader:
            from django.utils import autoreload
            autoreload.main(inner_run)
        else:
            inner_run()
Example #53
0
    def handle(self, *args, **options):
        os.environ["DJANGO_SETTINGS_MODULE"] = "settings.dev"

        #necessary to reload in manage.py with new value of DJANGO_SETTINGS_MODULE
        autoreload.main(self.inner_run, args, options)
        self.stdout.write('Successfully execute Command dblocal.\n')
Example #54
0
def main(*args, **kwargs):
    args = sys.argv
    config = None
    conf = {}

    config = utils.get_config_file()
    parser = OptionParser()
    parser.add_option("-t",
                      "--debug-toolbar",
                      dest="toolbar",
                      action="store_true",
                      default=False)
    parser.add_option("-i",
                      "--non-interactive",
                      dest="interactive",
                      action="store_true",
                      default=False)
    parser.add_option("-m",
                      "--no-media",
                      dest="no_media",
                      action="store_true",
                      default=False)
    parser.add_option("--host", dest="host", action="store", default="0.0.0.0")
    parser.add_option("-p",
                      "--port",
                      dest="port",
                      action="store",
                      default="8000")
    parser.add_option("--threads", dest="threads", action="store", default="2")
    options, args = parser.parse_args()

    if options.toolbar:
        conf['toolbar'] = True

    if options.interactive:
        conf['no_error'] = True

    if options.no_media:
        conf['no_media'] = True

    if config is None:
        config = utils.get_config_file()

    if 'request' in args:
        app = make_app({}, no_error=True)
        req = webob.Request.blank(args[1])
        try:
            resp = req.get_response(app)
        except:
            raise
        else:
            if resp.charset:
                body = resp.text
            else:
                body = resp.body
            if not isinstance(body, str):
                body = body.decode('utf8')
            print(body)
    else:
        config = utils.get_config_file()
        app = make_app(conf)
        from django.utils import autoreload
        autoreload.main(
            serve, (app, ), {
                'expose_tracebacks': True,
                'host': options.host,
                'port': options.port,
                'threads': options.threads,
            })
        return
Example #55
0
def server(bind="127.0.0.1",
           port=8000,
           migrate_cmd=False,
           verbose=1):  # pragma: no cover
    try:
        from channels.log import setup_logger
        from channels.management.commands import runserver

        logger = setup_logger("django.channels", 1)
        use_channels = True
    except ImportError:
        from django.contrib.staticfiles.management.commands import runserver

        use_channels = False
        logger = None

    if os.environ.get("RUN_MAIN") != "true":
        _create_db(migrate_cmd)
        User = get_user_model()  # NOQA
        if not User.objects.filter(is_superuser=True).exists():
            usr = create_user("admin",
                              "*****@*****.**",
                              "admin",
                              is_staff=True,
                              is_superuser=True)
            print("")
            print("A admin user (username: %s, password: admin) "
                  "has been created." % usr.get_username())
            print("")
    rs = runserver.Command()
    try:
        from django.core.management.base import OutputWrapper

        rs.stdout = OutputWrapper(sys.stdout)
        rs.stderr = OutputWrapper(sys.stderr)
    except ImportError:
        rs.stdout = sys.stdout
        rs.stderr = sys.stderr
    rs.use_ipv6 = False
    rs._raw_ipv6 = False
    rs.addr = bind
    rs.port = port
    if logger:
        rs.logger = logger
    if use_channels:
        rs.http_timeout = 60
        rs.websocket_handshake_timeout = 5
    try:
        autoreload.run_with_reloader(
            rs.inner_run, **{
                "addrport": "%s:%s" % (bind, port),
                "insecure_serving": True,
                "use_static_handler": True,
                "use_threading": True,
                "verbosity": verbose,
                "use_reloader": True,
            })
    except AttributeError:
        autoreload.main(rs.inner_run,
                        kwargs={
                            "addrport": "%s:%s" % (bind, port),
                            "insecure_serving": True,
                            "use_static_handler": True,
                            "use_threading": True,
                            "verbosity": verbose,
                            "use_reloader": True,
                        })
    def handle(self, addrport='', *args, **options):
        import django
        import socket
        import errno
        from django.core.servers.basehttp import run
        try:
            from django.core.servers.basehttp import get_internal_wsgi_application as WSGIHandler
        except ImportError:
            from django.core.handlers.wsgi import WSGIHandler  # noqa

        try:
            from django.core.servers.basehttp import AdminMediaHandler
            HAS_ADMINMEDIAHANDLER = True
        except ImportError:
            HAS_ADMINMEDIAHANDLER = False

        try:
            from django.core.servers.basehttp import WSGIServerException as wsgi_server_exc_cls
        except ImportError:  # Django 1.6
            wsgi_server_exc_cls = socket.error

        if args:
            raise CommandError('Usage is runserver %s' % self.args)
        if not addrport:
            addr = ''
            port = '8000'
        else:
            try:
                addr, port = addrport.split(':')
            except ValueError:
                addr, port = '', addrport
        if not addr:
            addr = '127.0.0.1'

        if not port.isdigit():
            raise CommandError("%r is not a valid port number." % port)

        use_reloader = options.get('use_reloader', True)
        shutdown_message = options.get('shutdown_message', '')
        no_media = options.get('no_media', False)
        quit_command = (sys.platform
                        == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'

        def inner_run():
            import os
            import time
            try:
                import hotshot
                HAS_HOTSPOT = True
            except ImportError:
                HAS_HOTSPOT = False  # python 3.x
            USE_CPROFILE = options.get('use_cprofile', False)
            USE_LSPROF = options.get('use_lsprof', False)
            if USE_LSPROF:
                USE_CPROFILE = True
            if USE_CPROFILE:
                try:
                    import cProfile
                    USE_CPROFILE = True
                except ImportError:
                    print("cProfile disabled, module cannot be imported!")
                    USE_CPROFILE = False
            if USE_LSPROF and not USE_CPROFILE:
                raise CommandError(
                    "Kcachegrind compatible output format required cProfile from Python 2.5"
                )

            if not HAS_HOTSPOT and not USE_CPROFILE:
                raise CommandError(
                    "Hotspot profile library not found. (and not using cProfile)"
                )

            prof_path = options.get('prof_path', '/tmp')

            prof_file = options.get('prof_file',
                                    '{path}.{duration:06d}ms.{time}')
            if not prof_file.format(path='1', duration=2, time=3):
                prof_file = '{path}.{duration:06d}ms.{time}'
                print(
                    "Filename format is wrong. Default format used: '{path}.{duration:06d}ms.{time}'."
                )

            def get_exclude_paths():
                exclude_paths = []
                media_url = getattr(settings, 'MEDIA_URL', None)
                if media_url:
                    exclude_paths.append(media_url)
                static_url = getattr(settings, 'STATIC_URL', None)
                if static_url:
                    exclude_paths.append(static_url)
                admin_media_prefix = getattr(settings, 'ADMIN_MEDIA_PREFIX',
                                             None)
                if admin_media_prefix:
                    exclude_paths.append(admin_media_prefix)
                return exclude_paths

            def make_profiler_handler(inner_handler):
                def handler(environ, start_response):
                    path_info = environ['PATH_INFO']
                    # when using something like a dynamic site middleware is could be necessary
                    # to refetch the exclude_paths every time since they could change per site.
                    if no_media and any(
                            path_info.startswith(p)
                            for p in get_exclude_paths()):
                        return inner_handler(environ, start_response)
                    path_name = path_info.strip("/").replace('/',
                                                             '.') or "root"
                    profname = "%s.%d.prof" % (path_name, time.time())
                    profname = os.path.join(prof_path, profname)
                    if USE_CPROFILE:
                        prof = cProfile.Profile()
                    else:
                        prof = hotshot.Profile(profname)
                    start = datetime.now()
                    try:
                        return prof.runcall(inner_handler, environ,
                                            start_response)
                    finally:
                        # seeing how long the request took is important!
                        elap = datetime.now() - start
                        elapms = elap.seconds * 1000.0 + elap.microseconds / 1000.0
                        if USE_LSPROF:
                            kg = KCacheGrind(prof)
                            with open(profname, 'w') as f:
                                kg.output(f)
                        elif USE_CPROFILE:
                            prof.dump_stats(profname)
                        profname2 = prof_file.format(path=path_name,
                                                     duration=int(elapms),
                                                     time=int(time.time()))
                        profname2 = os.path.join(prof_path,
                                                 "%s.prof" % profname2)
                        if not USE_CPROFILE:
                            prof.close()
                        os.rename(profname, profname2)

                return handler

            print("Validating models...")
            if hasattr(self, 'check'):
                self.check(display_num_errors=True)
            else:
                self.validate(display_num_errors=True)
            print("\nDjango version %s, using settings %r" %
                  (django.get_version(), settings.SETTINGS_MODULE))
            print("Development server is running at http://%s:%s/" %
                  (addr, port))
            print("Quit the server with %s." % quit_command)
            path = options.get('admin_media_path', '')
            if not path:
                admin_media_path = os.path.join(django.__path__[0],
                                                'contrib/admin/static/admin')
                if os.path.isdir(admin_media_path):
                    path = admin_media_path
                else:
                    path = os.path.join(django.__path__[0],
                                        'contrib/admin/media')
            try:
                handler = WSGIHandler()
                if HAS_ADMINMEDIAHANDLER:
                    handler = AdminMediaHandler(handler, path)
                if USE_STATICFILES:
                    use_static_handler = options.get('use_static_handler',
                                                     True)
                    insecure_serving = options.get('insecure_serving', False)
                    if use_static_handler and (settings.DEBUG
                                               or insecure_serving):
                        handler = StaticFilesHandler(handler)
                handler = make_profiler_handler(handler)
                run(addr, int(port), handler)
            except wsgi_server_exc_cls 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.",
                }
                if not isinstance(e, socket.error):  # Django < 1.6
                    ERRORS[13] = ERRORS.pop(errno.EACCES)
                    ERRORS[98] = ERRORS.pop(errno.EADDRINUSE)
                    ERRORS[99] = ERRORS.pop(errno.EADDRNOTAVAIL)
                try:
                    if not isinstance(e, socket.error):  # Django < 1.6
                        error_text = ERRORS[e.args[0].args[0]]
                    else:
                        error_text = ERRORS[e.errno]
                except (AttributeError, KeyError):
                    error_text = str(e)
                sys.stderr.write(
                    self.style.ERROR("Error: %s" % error_text) + '\n')
                # Need to use an OS exit because sys.exit doesn't work in a thread
                os._exit(1)
            except KeyboardInterrupt:
                if shutdown_message:
                    print(shutdown_message)
                sys.exit(0)

        if use_reloader:
            from django.utils import autoreload
            autoreload.main(inner_run)
        else:
            inner_run()
Example #57
0
 def handle(self, *args, **kwargs):
     print('Starting celery worker with autoreload')
     autoreload.main(restart_celery)
Example #58
0
 def handle(self, *args, **options):
     autoreload.main(main)