Example #1
0
def main():
  shutdown.install_signal_handlers()
  # The timezone must be set in the devappserver2 process rather than just in
  # the runtime so printed log timestamps are consistent and the taskqueue stub
  # expects the timezone to be UTC. The runtime inherits the environment.
  os.environ['TZ'] = 'UTC'
  if hasattr(time, 'tzset'):
    # time.tzet() should be called on Unix, but doesn't exist on Windows.
    time.tzset()
  options = PARSER.parse_args()
  os.environ['MY_IP_ADDRESS'] = options.host
  os.environ['MY_PORT'] = str(options.port)
  os.environ['COOKIE_SECRET'] = appscale_info.get_secret()
  os.environ['NGINX_HOST'] = options.nginx_host

  if options.pidfile:
    with open(options.pidfile, 'w') as pidfile:
      pidfile.write(str(os.getpid()))

  dev_server = DevelopmentServer()
  try:
    dev_server.start(options)
    shutdown.wait_until_shutdown()
  finally:
    dev_server.stop()
Example #2
0
def main():
    shutdown.install_signal_handlers()
    # The timezone must be set in the devappserver2 process rather than just in
    # the runtime so printed log timestamps are consistent and the taskqueue stub
    # expects the timezone to be UTC. The runtime inherits the environment.
    os.environ['TZ'] = 'UTC'
    if hasattr(time, 'tzset'):
        # time.tzet() should be called on Unix, but doesn't exist on Windows.
        time.tzset()
    options = PARSER.parse_args()
    os.environ['MY_IP_ADDRESS'] = options.host
    os.environ['MY_PORT'] = str(options.port)
    os.environ['COOKIE_SECRET'] = appscale_info.get_secret()
    os.environ['NGINX_HOST'] = options.nginx_host

    if options.pidfile:
        with open(options.pidfile, 'w') as pidfile:
            pidfile.write(str(os.getpid()))

    dev_server = DevelopmentServer()
    try:
        dev_server.start(options)
        shutdown.wait_until_shutdown()
    finally:
        dev_server.stop()
def main():
    """Parses command line options and launches the API server."""
    shutdown.install_signal_handlers()

    options = PARSER.parse_args()
    logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
        options.dev_appserver_log_level])

    # Parse the application configuration if config_paths are provided, else
    # provide sensible defaults.
    if options.config_paths:
        app_config = application_configuration.ApplicationConfiguration(
            options.config_paths, options.app_id)
        app_id = app_config.app_id
        app_root = app_config.modules[0].application_root
    else:
        app_id = (options.app_id_prefix + options.app_id
                  if options.app_id else DEFAULT_API_SERVER_APP_ID)
        app_root = tempfile.mkdtemp()
    util.setup_environ(app_id)

    # pylint: disable=protected-access
    if options.java_app_base_url:
        # If the user specified a java_app_base_url, then the actual app is running
        # via the classic Java SDK, so we use the appropriate dispatcher that will
        # send requests to the Java app rather than forward them internally to a
        # devappserver2 module.
        dispatcher = _LocalJavaAppDispatcher(
            java_app_base_url=options.java_app_base_url)
    else:
        # TODO: Rename LocalFakeDispatcher or re-implement for
        # api_server.py.
        dispatcher = request_info_lib._LocalFakeDispatcher()
    request_info = wsgi_request_info.WSGIRequestInfo(dispatcher)
    # pylint: enable=protected-access

    metrics_logger = metrics.GetMetricsLogger()
    metrics_logger.Start(
        options.google_analytics_client_id,
        user_agent=options.google_analytics_user_agent,
        support_datastore_emulator=options.support_datastore_emulator,
        category=metrics.API_SERVER_CATEGORY)

    # When Cloud Datastore Emulator is invoked from api_server, it should be in
    # test mode, which stores in memory.
    options.datastore_emulator_is_test_mode = True
    server = create_api_server(request_info=request_info,
                               storage_path=get_storage_path(
                                   options.storage_path, app_id),
                               options=options,
                               app_id=app_id,
                               app_root=app_root)

    try:
        server.start()
        shutdown.wait_until_shutdown()
    finally:
        metrics.GetMetricsLogger().Stop()
        server.quit()
Example #4
0
def main():
    """Parses command line options and launches the API server."""
    shutdown.install_signal_handlers()

    # Initialize logging early -- otherwise some library packages may
    # pre-empt our log formatting.  NOTE: the level is provisional; it may
    # be changed based on the --debug flag.
    logging.basicConfig(
        level=logging.INFO,
        format=
        '%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s] %(message)s')

    options = cli_parser.create_command_line_parser(
        cli_parser.API_SERVER_CONFIGURATION).parse_args()
    logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
        options.dev_appserver_log_level])

    # Parse the application configuration if config_paths are provided, else
    # provide sensible defaults.
    if options.config_paths:
        app_config = application_configuration.ApplicationConfiguration(
            options.config_paths, options.app_id)
        app_id = app_config.app_id
        app_root = app_config.modules[0].application_root
    else:
        app_id = (options.app_id
                  if options.app_id else DEFAULT_API_SERVER_APP_ID)
        app_root = tempfile.mkdtemp()

    # pylint: disable=protected-access
    # TODO: Rename LocalFakeDispatcher or re-implement for api_server.py.
    request_info = wsgi_request_info.WSGIRequestInfo(
        request_info_lib._LocalFakeDispatcher())
    # pylint: enable=protected-access

    os.environ['APPLICATION_ID'] = app_id
    os.environ['APPNAME'] = app_id
    os.environ['NGINX_HOST'] = options.nginx_host

    def request_context(environ):
        return request_info.request(environ, None)

    server = create_api_server(request_info=request_info,
                               storage_path=get_storage_path(
                                   options.storage_path, app_id),
                               options=options,
                               app_id=app_id,
                               app_root=app_root,
                               request_context=request_context)

    if options.pidfile:
        with open(options.pidfile, 'w') as pidfile:
            pidfile.write(str(os.getpid()))

    try:
        server.start()
        shutdown.wait_until_shutdown()
    finally:
        server.quit()
Example #5
0
def main():
  """Parses command line options and launches the API server."""
  shutdown.install_signal_handlers()

  options = PARSER.parse_args()
  logging.getLogger().setLevel(
      constants.LOG_LEVEL_TO_PYTHON_CONSTANT[options.dev_appserver_log_level])

  # Parse the application configuration if config_paths are provided, else
  # provide sensible defaults.
  if options.config_paths:
    app_config = application_configuration.ApplicationConfiguration(
        options.config_paths, options.app_id)
    app_id = app_config.app_id
    app_root = app_config.modules[0].application_root
  else:
    app_id = (options.app_id_prefix + options.app_id if
              options.app_id else DEFAULT_API_SERVER_APP_ID)
    app_root = tempfile.mkdtemp()
  util.setup_environ(app_id)

  # pylint: disable=protected-access
  if options.java_app_base_url:
    # If the user specified a java_app_base_url, then the actual app is running
    # via the classic Java SDK, so we use the appropriate dispatcher that will
    # send requests to the Java app rather than forward them internally to a
    # devappserver2 module.
    dispatcher = _LocalJavaAppDispatcher(
        java_app_base_url=options.java_app_base_url)
  else:
    # TODO: Rename LocalFakeDispatcher or re-implement for
    # api_server.py.
    dispatcher = request_info_lib._LocalFakeDispatcher()
  request_info = wsgi_request_info.WSGIRequestInfo(dispatcher)
  # pylint: enable=protected-access

  metrics_logger = metrics.GetMetricsLogger()
  metrics_logger.Start(
      options.google_analytics_client_id,
      user_agent=options.google_analytics_user_agent,
      support_datastore_emulator=options.support_datastore_emulator,
      category=metrics.API_SERVER_CATEGORY)

  # When Cloud Datastore Emulator is invoked from api_server, it should be in
  # test mode, which stores in memory.
  options.datastore_emulator_is_test_mode = True
  server = create_api_server(
      request_info=request_info,
      storage_path=get_storage_path(options.storage_path, app_id),
      options=options, app_id=app_id, app_root=app_root)

  try:
    server.start()
    shutdown.wait_until_shutdown()
  finally:
    metrics.GetMetricsLogger().Stop()
    server.quit()
Example #6
0
    def test_wait_until_shutdown_raise_interrupted_io(self):
        def quit_and_raise(*_):
            shutdown.async_quit()
            raise IOError

        self.mox.StubOutWithMock(time, 'sleep')
        time.sleep(1).WithSideEffects(quit_and_raise)
        self.mox.ReplayAll()
        shutdown.wait_until_shutdown()
        self.mox.VerifyAll()
  def test_wait_until_shutdown_raise_interrupted_io(self):

    def quit_and_raise(*_):
      shutdown.async_quit()
      raise IOError

    self.mox.StubOutWithMock(time, 'sleep')
    time.sleep(1).WithSideEffects(quit_and_raise)
    self.mox.ReplayAll()
    shutdown.wait_until_shutdown()
    self.mox.VerifyAll()
Example #8
0
def main():
    """Parses command line options and launches the API server."""
    shutdown.install_signal_handlers()

    options = PARSER.parse_args()
    logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
        options.dev_appserver_log_level])

    # Parse the application configuration if config_paths are provided, else
    # provide sensible defaults.
    if options.config_paths:
        app_config = application_configuration.ApplicationConfiguration(
            options.config_paths, options.app_id)
        app_id = app_config.app_id
        app_root = app_config.modules[0].application_root
    else:
        app_id = (options.app_id_prefix + options.app_id
                  if options.app_id else DEFAULT_API_SERVER_APP_ID)
        app_root = tempfile.mkdtemp()
    util.setup_environ(app_id)

    # pylint: disable=protected-access
    if options.java_app_base_url:
        # If the user specified a java_app_base_url, then the actual app is running
        # via the classic Java SDK, so we use the appropriate dispatcher that will
        # send requests to the Java app rather than forward them internally to a
        # devappserver2 module.
        dispatcher = _LocalJavaAppDispatcher(
            java_app_base_url=options.java_app_base_url)
    else:
        # TODO: Rename LocalFakeDispatcher or re-implement for
        # api_server.py.
        dispatcher = request_info_lib._LocalFakeDispatcher()
    request_info = wsgi_request_info.WSGIRequestInfo(dispatcher)
    # pylint: enable=protected-access

    server = create_api_server(request_info=request_info,
                               storage_path=get_storage_path(
                                   options.storage_path, app_id),
                               options=options,
                               app_id=app_id,
                               app_root=app_root)

    try:
        server.start()
        shutdown.wait_until_shutdown()
    finally:
        server.quit()
def main():
  shutdown.install_signal_handlers()
  # The timezone must be set in the devappserver2 process rather than just in
  # the runtime so printed log timestamps are consistent and the taskqueue stub
  # expects the timezone to be UTC. The runtime inherits the environment.
  os.environ['TZ'] = 'UTC'
  if hasattr(time, 'tzset'):
    # time.tzet() should be called on Unix, but doesn't exist on Windows.
    time.tzset()
  options = PARSER.parse_args()
  dev_server = DevelopmentServer()
  try:
    dev_server.start(options)
    shutdown.wait_until_shutdown()
  finally:
    dev_server.stop()
Example #10
0
def main():
    shutdown.install_signal_handlers()
    # The timezone must be set in the devappserver2 process rather than just in
    # the runtime so printed log timestamps are consistent and the taskqueue stub
    # expects the timezone to be UTC. The runtime inherits the environment.
    os.environ['TZ'] = 'UTC'
    if hasattr(time, 'tzset'):
        # time.tzet() should be called on Unix, but doesn't exist on Windows.
        time.tzset()
    options = PARSER.parse_args()
    dev_server = DevelopmentServer()
    try:
        dev_server.start(options)
        shutdown.wait_until_shutdown()
    finally:
        dev_server.stop()
Example #11
0
def main():
    """Parses command line options and launches the API server."""
    shutdown.install_signal_handlers()

    options = cli_parser.create_command_line_parser(
        cli_parser.API_SERVER_CONFIGURATION).parse_args()
    logging.getLogger().setLevel(constants.LOG_LEVEL_TO_PYTHON_CONSTANT[
        options.dev_appserver_log_level])

    # Parse the application configuration if config_paths are provided, else
    # provide sensible defaults.
    if options.config_paths:
        app_config = application_configuration.ApplicationConfiguration(
            options.config_paths, options.app_id)
        app_id = app_config.app_id
        app_root = app_config.modules[0].application_root
    else:
        app_id = (options.app_id
                  if options.app_id else DEFAULT_API_SERVER_APP_ID)
        app_root = tempfile.mkdtemp()

    # pylint: disable=protected-access
    # TODO: Rename LocalFakeDispatcher or re-implement for api_server.py.
    request_info = wsgi_request_info.WSGIRequestInfo(
        request_info_lib._LocalFakeDispatcher())
    # pylint: enable=protected-access

    os.environ['APPNAME'] = app_id
    os.environ['NGINX_HOST'] = options.nginx_host

    server = create_api_server(request_info=request_info,
                               storage_path=get_storage_path(
                                   options.storage_path, app_id),
                               options=options,
                               app_id=app_id,
                               app_root=app_root)

    try:
        server.start()
        shutdown.wait_until_shutdown()
    finally:
        server.quit()
Example #12
0
def main():
    shutdown.install_signal_handlers()
    # The timezone must be set in the devappserver2 process rather than just in
    # the runtime so printed log timestamps are consistent and the taskqueue stub
    # expects the timezone to be UTC. The runtime inherits the environment.
    os.environ["TZ"] = "UTC"
    if hasattr(time, "tzset"):
        # time.tzet() should be called on Unix, but doesn't exist on Windows.
        time.tzset()
    options = PARSER.parse_args()
    os.environ["MY_IP_ADDRESS"] = options.host
    os.environ["MY_PORT"] = str(options.port)
    os.environ["COOKIE_SECRET"] = appscale_info.get_secret()
    os.environ["NGINX_HOST"] = options.nginx_host
    dev_server = DevelopmentServer()
    try:
        dev_server.start(options)
        shutdown.wait_until_shutdown()
    finally:
        dev_server.stop()
def main():
  shutdown.install_signal_handlers()
  # The timezone must be set in the devappserver2 process rather than just in
  # the runtime so printed log timestamps are consistent and the taskqueue stub
  # expects the timezone to be UTC. The runtime inherits the environment.
  os.environ['TZ'] = 'UTC'
  if hasattr(time, 'tzset'):
    # time.tzet() should be called on Unix, but doesn't exist on Windows.
    time.tzset()
  options = PARSER.parse_args()
  dev_server = DevelopmentServer()
  try:
    dev_server.start(options)
    shutdown.wait_until_shutdown()
  except:  # pylint: disable=bare-except
    metrics.GetMetricsLogger().LogOnceOnStop(
        metrics.DEVAPPSERVER_CATEGORY, metrics.ERROR_ACTION,
        label=metrics.GetErrorDetails())
    raise
  finally:
    dev_server.stop()
def main():
  shutdown.install_signal_handlers()
  # The timezone must be set in the devappserver2 process rather than just in
  # the runtime so printed log timestamps are consistent and the taskqueue stub
  # expects the timezone to be UTC. The runtime inherits the environment.
  os.environ['TZ'] = 'UTC'
  if hasattr(time, 'tzset'):
    # time.tzet() should be called on Unix, but doesn't exist on Windows.
    time.tzset()
  options = PARSER.parse_args()
  dev_server = DevelopmentServer()
  try:
    dev_server.start(options)
    shutdown.wait_until_shutdown()
  except:  # pylint: disable=bare-except
    metrics.GetMetricsLogger().LogOnceOnStop(
        metrics.DEVAPPSERVER_CATEGORY, metrics.ERROR_ACTION,
        label=metrics.GetErrorDetails())
    raise
  finally:
    dev_server.stop()
Example #15
0
def main():
  """Parses command line options and launches the API server."""
  shutdown.install_signal_handlers()

  options = cli_parser.create_command_line_parser(
      cli_parser.API_SERVER_CONFIGURATION).parse_args()
  logging.getLogger().setLevel(
      constants.LOG_LEVEL_TO_PYTHON_CONSTANT[options.dev_appserver_log_level])

  # Parse the application configuration if config_paths are provided, else
  # provide sensible defaults.
  if options.config_paths:
    app_config = application_configuration.ApplicationConfiguration(
        options.config_paths, options.app_id)
    app_id = app_config.app_id
    app_root = app_config.modules[0].application_root
  else:
    app_id = (options.app_id if
              options.app_id else DEFAULT_API_SERVER_APP_ID)
    app_root = tempfile.mkdtemp()

  # pylint: disable=protected-access
  # TODO: Rename LocalFakeDispatcher or re-implement for api_server.py.
  request_info = wsgi_request_info.WSGIRequestInfo(
      request_info_lib._LocalFakeDispatcher())
  # pylint: enable=protected-access

  os.environ['APPNAME'] = app_id
  os.environ['NGINX_HOST'] = options.nginx_host

  server = create_api_server(
      request_info=request_info,
      storage_path=get_storage_path(options.storage_path, app_id),
      options=options, app_id=app_id, app_root=app_root)

  try:
    server.start()
    shutdown.wait_until_shutdown()
  finally:
    server.quit()
Example #16
0
    def inner_run(self, *args, **options):
        import sys

        shutdown_message = options.get('shutdown_message', '')

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        from djangae.utils import find_project_root
        from djangae.sandbox import _find_sdk_from_python_path
        from djangae.blobstore_service import stop_blobstore_service

        from django.conf import settings
        from django.utils import translation

        stop_blobstore_service()

        # Check for app.yaml
        expected_path = os.path.join(find_project_root(), "app.yaml")
        if not os.path.exists(expected_path):
            sys.stderr.write("Unable to find app.yaml at '%s'\n" %
                             expected_path)
            sys.exit(1)

        self.stdout.write("Validating models...\n\n")
        self.validate(display_num_errors=True)
        self.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": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })
        sys.stdout.write("\n")
        sys.stdout.flush()

        # 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)

        # Will have been set by setup_paths
        sdk_path = _find_sdk_from_python_path()

        from google.appengine.tools.devappserver2 import devappserver2
        from google.appengine.tools.devappserver2 import python_runtime

        from djangae import sandbox

        if int(self.port) != sandbox._OPTIONS.port:
            # Override the port numbers
            sandbox._OPTIONS.port = int(self.port)
            sandbox._OPTIONS.admin_port = int(self.port) + 1
            sandbox._OPTIONS.api_port = int(self.port) + 2

        if self.addr != sandbox._OPTIONS.host:
            sandbox._OPTIONS.host = sandbox._OPTIONS.admin_host = sandbox._OPTIONS.api_host = self.addr

        # Extra options for `dev_appserver.py`
        for param, value in self.gae_options.items():
            setattr(sandbox._OPTIONS, param, value)

        # External port is a new flag introduced in 1.9.19
        current_version = _VersionList(GetVersionObject()['release'])
        if current_version >= _VersionList('1.9.19'):
            sandbox._OPTIONS.external_port = None

        sandbox._OPTIONS.automatic_restart = self.use_reloader

        if sandbox._OPTIONS.host == "127.0.0.1" and os.environ[
                "HTTP_HOST"].startswith("localhost"):
            hostname = "localhost"
        else:
            hostname = sandbox._OPTIONS.host

        os.environ["HTTP_HOST"] = "%s:%s" % (hostname, sandbox._OPTIONS.port)
        os.environ['SERVER_NAME'] = os.environ['HTTP_HOST'].split(':', 1)[0]
        os.environ['SERVER_PORT'] = os.environ['HTTP_HOST'].split(':', 1)[1]
        os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % (
            os.environ['SERVER_NAME'], os.environ['SERVER_PORT'])

        class NoConfigDevServer(devappserver2.DevelopmentServer):
            def _create_api_server(self, request_data, storage_path, options,
                                   configuration):
                self._dispatcher = sandbox._create_dispatcher(
                    configuration, options)
                self._dispatcher._port = options.port
                self._dispatcher._host = options.host

                self._dispatcher.request_data = request_data
                request_data._dispatcher = self._dispatcher

                sandbox._API_SERVER._host = options.api_host
                sandbox._API_SERVER.bind_addr = (options.api_host,
                                                 options.api_port)

                from google.appengine.api import apiproxy_stub_map
                task_queue = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
                # Make sure task running is enabled (it's disabled in the sandbox by default)
                if not task_queue._auto_task_running:
                    task_queue._auto_task_running = True
                    task_queue.StartBackgroundExecution()

                return sandbox._API_SERVER

        python_runtime._RUNTIME_PATH = os.path.join(sdk_path,
                                                    '_python_runtime.py')
        python_runtime._RUNTIME_ARGS = [
            sys.executable, python_runtime._RUNTIME_PATH
        ]

        dev_server = NoConfigDevServer()

        try:
            dev_server.start(sandbox._OPTIONS)
            try:
                shutdown.wait_until_shutdown()
            except KeyboardInterrupt:
                pass
        finally:
            dev_server.stop()

        if shutdown_message:
            sys.stdout.write(shutdown_message)

        return
Example #17
0
    def inner_run(self, *args, **options):
        import sys

        shutdown_message = options.get('shutdown_message', '')

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        from djangae.utils import find_project_root
        from djangae.sandbox import _find_sdk_from_python_path
        from djangae.blobstore_service import stop_blobstore_service

        from django.conf import settings
        from django.utils import translation

        stop_blobstore_service()

        # Check for app.yaml
        expected_path = os.path.join(find_project_root(), "app.yaml")
        if not os.path.exists(expected_path):
            sys.stderr.write("Unable to find app.yaml at '%s'\n" %
                             expected_path)
            sys.exit(1)

        self.stdout.write("Validating models...\n\n")
        self.check(display_num_errors=True)
        self.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": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })
        sys.stdout.write("\n")
        sys.stdout.flush()

        # 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)

        # Will have been set by setup_paths
        sdk_path = _find_sdk_from_python_path()

        from google.appengine.tools.devappserver2 import devappserver2
        from google.appengine.tools.devappserver2 import python_runtime

        from djangae import sandbox

        # Add any additional modules specified in the settings
        additional_modules = getattr(settings, "DJANGAE_ADDITIONAL_MODULES",
                                     [])
        if additional_modules:
            sandbox._OPTIONS.config_paths.extend(additional_modules)

        if int(self.port) != sandbox._OPTIONS.port or additional_modules:
            # Override the port numbers
            sandbox._OPTIONS.port = int(self.port)
            sandbox._OPTIONS.admin_port = int(
                self.port) + len(additional_modules) + 1
            sandbox._OPTIONS.api_port = int(
                self.port) + len(additional_modules) + 2

        if self.addr != sandbox._OPTIONS.host:
            sandbox._OPTIONS.host = sandbox._OPTIONS.admin_host = sandbox._OPTIONS.api_host = self.addr

        # Extra options for `dev_appserver.py`
        for param, value in self.gae_options.items():
            setattr(sandbox._OPTIONS, param, value)

        # External port is a new flag introduced in 1.9.19
        current_version = _VersionList(GetVersionObject()['release'])
        if current_version >= _VersionList('1.9.19'):
            sandbox._OPTIONS.external_port = None

        sandbox._OPTIONS.automatic_restart = self.use_reloader

        if sandbox._OPTIONS.host == "127.0.0.1" and os.environ[
                "HTTP_HOST"].startswith("localhost"):
            hostname = "localhost"
            sandbox._OPTIONS.host = "localhost"
        else:
            hostname = sandbox._OPTIONS.host

        os.environ["HTTP_HOST"] = "%s:%s" % (hostname, sandbox._OPTIONS.port)
        os.environ['SERVER_NAME'] = os.environ['HTTP_HOST'].split(':', 1)[0]
        os.environ['SERVER_PORT'] = os.environ['HTTP_HOST'].split(':', 1)[1]
        os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % (
            os.environ['SERVER_NAME'], os.environ['SERVER_PORT'])

        from google.appengine.api.appinfo import EnvironmentVariables

        class NoConfigDevServer(devappserver2.DevelopmentServer):
            def _create_api_server(self, request_data, storage_path, options,
                                   configuration):
                # sandbox._create_dispatcher returns a singleton dispatcher instance made in sandbox
                self._dispatcher = sandbox._create_dispatcher(
                    configuration, options)
                # the dispatcher may have passed environment variables, it should be propagated
                env_vars = self._dispatcher._configuration.modules[
                    0]._app_info_external.env_variables or EnvironmentVariables(
                    )
                for module in configuration.modules:
                    module_name = module._module_name
                    if module_name == 'default':
                        module_settings = 'DJANGO_SETTINGS_MODULE'
                    else:
                        module_settings = '%s_DJANGO_SETTINGS_MODULE' % module_name
                    if module_settings in env_vars:
                        module_env_vars = module.env_variables or EnvironmentVariables(
                        )
                        module_env_vars['DJANGO_SETTINGS_MODULE'] = env_vars[
                            module_settings]

                        old_env_vars = module._app_info_external.env_variables
                        new_env_vars = EnvironmentVariables.Merge(
                            module_env_vars, old_env_vars)
                        module._app_info_external.env_variables = new_env_vars
                self._dispatcher._configuration = configuration
                self._dispatcher._port = options.port
                self._dispatcher._host = options.host

                self._dispatcher.request_data = request_data
                request_data._dispatcher = self._dispatcher

                sandbox._API_SERVER._host = options.api_host
                sandbox._API_SERVER.bind_addr = (options.api_host,
                                                 options.api_port)

                from google.appengine.api import apiproxy_stub_map
                task_queue = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
                # Make sure task running is enabled (it's disabled in the sandbox by default)
                if not task_queue._auto_task_running:
                    task_queue._auto_task_running = True
                    task_queue.StartBackgroundExecution()

                return sandbox._API_SERVER

        from google.appengine.tools.devappserver2 import module

        def fix_watcher_files(regex):
            from google.appengine.tools.devappserver2 import watcher_common
            watcher_common._IGNORED_REGEX = regex
            watcher_common.ignore_file = ignore_file

        regex = sandbox._CONFIG.modules[0].skip_files
        if regex:
            fix_watcher_files(regex)

        def logging_wrapper(func):
            """
                Changes logging to use the DJANGO_COLORS settings
            """
            def _wrapper(level, format, *args, **kwargs):
                if args and len(args) == 1 and isinstance(args[0], dict):
                    args = args[0]
                    status = str(args.get("status", 200))
                else:
                    status = "200"

                try:
                    msg = format % args
                except UnicodeDecodeError:
                    msg += "\n"  # This is what Django does in WSGIRequestHandler.log_message

                # Utilize terminal colors, if available
                if status[0] == '2':
                    # Put 2XX first, since it should be the common case
                    msg = self.style.HTTP_SUCCESS(msg)
                elif status[0] == '1':
                    msg = self.style.HTTP_INFO(msg)
                elif status == '304':
                    msg = self.style.HTTP_NOT_MODIFIED(msg)
                elif status[0] == '3':
                    msg = self.style.HTTP_REDIRECT(msg)
                elif status == '404':
                    msg = self.style.HTTP_NOT_FOUND(msg)
                elif status[0] == '4':
                    # 0x16 = Handshake, 0x03 = SSL 3.0 or TLS 1.x
                    if status.startswith(str('\x16\x03')):
                        msg = (
                            "You're accessing the development server over HTTPS, "
                            "but it only supports HTTP.\n")
                    msg = self.style.HTTP_BAD_REQUEST(msg)
                else:
                    # Any 5XX, or any other response
                    msg = self.style.HTTP_SERVER_ERROR(msg)

                return func(level, msg)

            return _wrapper

        module.logging.log = logging_wrapper(module.logging.log)

        python_runtime._RUNTIME_PATH = os.path.join(sdk_path,
                                                    '_python_runtime.py')
        python_runtime._RUNTIME_ARGS = [
            sys.executable, python_runtime._RUNTIME_PATH
        ]

        dev_server = NoConfigDevServer()

        try:
            dev_server.start(sandbox._OPTIONS)
            try:
                shutdown.wait_until_shutdown()
            except KeyboardInterrupt:
                pass
        finally:
            dev_server.stop()

        if shutdown_message:
            sys.stdout.write(shutdown_message)

        return
Example #18
0
    def inner_run(self, *args, **options):
        import sys

        shutdown_message = options.get('shutdown_message', '')

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        from djangae.utils import find_project_root
        from djangae.sandbox import _find_sdk_from_python_path
        from djangae.blobstore_service import stop_blobstore_service

        from django.conf import settings
        from django.utils import translation

        stop_blobstore_service()

        # Check for app.yaml
        expected_path = os.path.join(find_project_root(), "app.yaml")
        if not os.path.exists(expected_path):
            sys.stderr.write("Unable to find app.yaml at '%s'\n" % expected_path)
            sys.exit(1)

        self.stdout.write("Validating models...\n\n")
        self.validate(display_num_errors=True)
        self.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": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
            "port": self.port,
            "quit_command": quit_command,
        })
        sys.stdout.write("\n")
        sys.stdout.flush()

        # 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)

        # Will have been set by setup_paths
        sdk_path = _find_sdk_from_python_path()

        from google.appengine.tools.devappserver2 import devappserver2
        from google.appengine.tools.devappserver2 import python_runtime

        from djangae import sandbox

        # Add any additional modules specified in the settings
        additional_modules = getattr(settings, "DJANGAE_ADDITIONAL_MODULES", [])
        if additional_modules:
            sandbox._OPTIONS.config_paths.extend(additional_modules)

        if int(self.port) != sandbox._OPTIONS.port or additional_modules:
            # Override the port numbers
            sandbox._OPTIONS.port = int(self.port)
            sandbox._OPTIONS.admin_port = int(self.port) + len(additional_modules) + 1
            sandbox._OPTIONS.api_port = int(self.port) + len(additional_modules) + 2

        if self.addr != sandbox._OPTIONS.host:
            sandbox._OPTIONS.host = sandbox._OPTIONS.admin_host = sandbox._OPTIONS.api_host = self.addr

        # Extra options for `dev_appserver.py`
        for param, value in self.gae_options.items():
            setattr(sandbox._OPTIONS, param, value)

        # External port is a new flag introduced in 1.9.19
        current_version = _VersionList(GetVersionObject()['release'])
        if current_version >= _VersionList('1.9.19'):
            sandbox._OPTIONS.external_port = None

        sandbox._OPTIONS.automatic_restart = self.use_reloader

        if sandbox._OPTIONS.host == "127.0.0.1" and os.environ["HTTP_HOST"].startswith("localhost"):
            hostname = "localhost"
        else:
            hostname = sandbox._OPTIONS.host

        os.environ["HTTP_HOST"] = "%s:%s" % (hostname, sandbox._OPTIONS.port)
        os.environ['SERVER_NAME'] = os.environ['HTTP_HOST'].split(':', 1)[0]
        os.environ['SERVER_PORT'] = os.environ['HTTP_HOST'].split(':', 1)[1]
        os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % (os.environ['SERVER_NAME'], os.environ['SERVER_PORT'])

        class NoConfigDevServer(devappserver2.DevelopmentServer):
            def _create_api_server(self, request_data, storage_path, options, configuration):
                self._dispatcher = sandbox._create_dispatcher(configuration, options)
                self._dispatcher._configuration = configuration
                self._dispatcher._port = options.port
                self._dispatcher._host = options.host

                self._dispatcher.request_data = request_data
                request_data._dispatcher = self._dispatcher

                sandbox._API_SERVER._host = options.api_host
                sandbox._API_SERVER.bind_addr = (options.api_host, options.api_port)

                from google.appengine.api import apiproxy_stub_map
                task_queue = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
                # Make sure task running is enabled (it's disabled in the sandbox by default)
                if not task_queue._auto_task_running:
                    task_queue._auto_task_running = True
                    task_queue.StartBackgroundExecution()

                return sandbox._API_SERVER

        from google.appengine.tools.devappserver2 import module

        def logging_wrapper(func):
            """
                Changes logging to use the DJANGO_COLORS settings
            """
            def _wrapper(level, format, *args, **kwargs):
                if args and len(args) == 1 and isinstance(args[0], dict):
                    args = args[0]
                    status = str(args.get("status", 200))
                else:
                    status = "200"

                try:
                    msg = format % args
                except UnicodeDecodeError:
                    msg += "\n" # This is what Django does in WSGIRequestHandler.log_message

                # Utilize terminal colors, if available
                if status[0] == '2':
                    # Put 2XX first, since it should be the common case
                    msg = self.style.HTTP_SUCCESS(msg)
                elif status[0] == '1':
                    msg = self.style.HTTP_INFO(msg)
                elif status == '304':
                    msg = self.style.HTTP_NOT_MODIFIED(msg)
                elif status[0] == '3':
                    msg = self.style.HTTP_REDIRECT(msg)
                elif status == '404':
                    msg = self.style.HTTP_NOT_FOUND(msg)
                elif status[0] == '4':
                    # 0x16 = Handshake, 0x03 = SSL 3.0 or TLS 1.x
                    if status.startswith(str('\x16\x03')):
                        msg = ("You're accessing the development server over HTTPS, "
                            "but it only supports HTTP.\n")
                    msg = self.style.HTTP_BAD_REQUEST(msg)
                else:
                    # Any 5XX, or any other response
                    msg = self.style.HTTP_SERVER_ERROR(msg)

                return func(level, msg)
            return _wrapper

        module.logging.log = logging_wrapper(module.logging.log)

        python_runtime._RUNTIME_PATH = os.path.join(sdk_path, '_python_runtime.py')
        python_runtime._RUNTIME_ARGS = [sys.executable, python_runtime._RUNTIME_PATH]

        dev_server = NoConfigDevServer()

        try:
            dev_server.start(sandbox._OPTIONS)
            try:
                shutdown.wait_until_shutdown()
            except KeyboardInterrupt:
                pass
        finally:
            dev_server.stop()


        if shutdown_message:
            sys.stdout.write(shutdown_message)

        return
Example #19
0
    def inner_run(self, *args, **options):
        import sys

        shutdown_message = options.get('shutdown_message', '')

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        from djangae.environment import get_application_root
        from djangae.sandbox import _find_sdk_from_python_path
        from djangae.blobstore_service import stop_blobstore_service

        from django.conf import settings
        from django.utils import translation

        stop_blobstore_service()

        # Check for app.yaml
        expected_path = os.path.join(get_application_root(), "app.yaml")
        if not os.path.exists(expected_path):
            sys.stderr.write("Unable to find app.yaml at '%s'\n" % expected_path)
            sys.exit(1)

        self.stdout.write("Validating models...\n\n")
        self.check(display_num_errors=True)
        self.stdout.write((
            "%(started_at)s\n"
            "Djangae version %(djangae_version)s\n"
            "Django version %(django_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'),
            "django_version": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
            "port": self.port,
            "quit_command": quit_command,
            "djangae_version": DJANGAE_VERSION
        })
        sys.stdout.write("\n")
        sys.stdout.flush()

        # 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)

        # Will have been set by setup_paths
        sdk_path = _find_sdk_from_python_path()

        from google.appengine.tools.devappserver2 import devappserver2
        from google.appengine.tools.devappserver2 import python_runtime

        from djangae import sandbox

        # Add any additional modules specified in the settings
        additional_modules = getattr(settings, "DJANGAE_ADDITIONAL_MODULES", [])
        if additional_modules:
            sandbox._OPTIONS.config_paths.extend(additional_modules)

        if self.addr != sandbox._OPTIONS.host:
            sandbox._OPTIONS.host = sandbox._OPTIONS.admin_host = sandbox._OPTIONS.api_host = self.addr

        # Extra options for `dev_appserver.py`
        for param, value in self.gae_options.items():
            setattr(sandbox._OPTIONS, param, value)

        # External port is a new flag introduced in 1.9.19
        current_version = _VersionList(GetVersionObject()['release'])
        if current_version >= _VersionList('1.9.19') or \
                current_version == sandbox.TEMP_1_9_49_VERSION_NO:
            sandbox._OPTIONS.external_port = None

        # Apply equivalent options for Django args
        sandbox._OPTIONS.automatic_restart = self.use_reloader
        sandbox._OPTIONS.threadsafe_override = self.use_threading

        if sandbox._OPTIONS.host == "127.0.0.1" and os.environ["HTTP_HOST"].startswith("localhost"):
            hostname = "localhost"
            sandbox._OPTIONS.host = "localhost"
        else:
            hostname = sandbox._OPTIONS.host

        os.environ["HTTP_HOST"] = "%s:%s" % (hostname, sandbox._OPTIONS.port)
        os.environ['SERVER_NAME'] = os.environ['HTTP_HOST'].split(':', 1)[0]
        os.environ['SERVER_PORT'] = os.environ['HTTP_HOST'].split(':', 1)[1]
        os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % (os.environ['SERVER_NAME'], os.environ['SERVER_PORT'])

        from google.appengine.api.appinfo import EnvironmentVariables

        class NoConfigDevServer(devappserver2.DevelopmentServer):
            """
                This is horrible, but unfortunately necessary.

                Because we want to enable a sandbox outside of runserver (both when
                running different management commands, but also before/after dev_appserver)
                we have to make sure the following are true:

                1. There is only ever one api server
                2. There is only ever one dispatcher

                Unfortunately, most of the setup is done inside .start() of the DevelopmentServer
                class, there is not really an easy way to hook into part of this without overriding the
                whole .start() method which makes things even more brittle.

                What we do here is hook in at the point that self._dispatcher is set. We ignore whatever
                dispatcher is passed in, but user our own one. We patch api server creation in sandbox.py
                so only ever one api server exists.
            """
            def __init__(self, *args, **kwargs):
                self._patched_dispatcher = None
                super(NoConfigDevServer, self).__init__(*args, **kwargs)

            def start(self, options):
                self.options = options
                return super(NoConfigDevServer, self).start(options)

            def _get_dispatcher(self):
                return self._patched_dispatcher

            def _create_api_server(self, *args, **kwargs):
                """
                    For SDK around 1.9.40 - just return the existing API server
                """
                return sandbox._API_SERVER

            def _set_dispatcher(self, dispatcher):
                """
                    Ignore explicit setting of _dispatcher, use our own
                """

                if dispatcher is None:
                    # Allow wiping the patched dispatcher
                    self._patched_dispatcher = None
                    return

                if self._patched_dispatcher:
                    # We already created the dispatcher, ignore further sets
                    logging.warning("Attempted to set _dispatcher twice")
                    return


                # When the dispatcher is created this property is set so we use it
                # to construct *our* dispatcher
                configuration = dispatcher._configuration

                # We store options in .start() so it's available here
                options = self.options

                # sandbox._create_dispatcher returns a singleton dispatcher instance made in sandbox
                self._patched_dispatcher = sandbox._create_dispatcher(
                    configuration,
                    options
                )

                # the dispatcher may have passed environment variables, it should be propagated
                env_vars = self._dispatcher._configuration.modules[0]._app_info_external.env_variables or EnvironmentVariables()
                for module in configuration.modules:
                    module_name = module._module_name
                    if module_name == 'default' or module_name is None:
                        module_settings = 'DJANGO_SETTINGS_MODULE'
                    else:
                        module_settings = '%s_DJANGO_SETTINGS_MODULE' % module_name
                    if module_settings in env_vars:
                        module_env_vars = module.env_variables or EnvironmentVariables()
                        module_env_vars['DJANGO_SETTINGS_MODULE'] = env_vars[module_settings]

                        old_env_vars = module._app_info_external.env_variables
                        new_env_vars = EnvironmentVariables.Merge(module_env_vars, old_env_vars)
                        module._app_info_external.env_variables = new_env_vars
                self._dispatcher._configuration = configuration
                self._dispatcher._port = options.port
                self._dispatcher._host = options.host

                # Because the dispatcher is a singleton, we need to set the threadsafe override here
                # depending on what was passed to the runserver command. This entire file really needs rebuilding
                # we have way too many hacks in here!
                self._dispatcher._module_to_threadsafe_override[
                    configuration.modules[0].module_name
                ] = options.threadsafe_override

#                self._dispatcher.request_data = request_data
#                request_data._dispatcher = self._dispatcher

                sandbox._API_SERVER._host = options.api_host
                sandbox._API_SERVER.bind_addr = (options.api_host, options.api_port)

                from google.appengine.api import apiproxy_stub_map
                task_queue = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
                # Make sure task running is enabled (it's disabled in the sandbox by default)
                if not task_queue._auto_task_running:
                    task_queue._auto_task_running = True
                    task_queue.StartBackgroundExecution()

            _dispatcher = property(fget=_get_dispatcher, fset=_set_dispatcher)

        from google.appengine.tools.devappserver2 import module

        def fix_watcher_files(regex):
            """ Monkeypatch dev_appserver's file watcher to ignore any unwanted dirs or files. """
            from google.appengine.tools.devappserver2 import watcher_common
            watcher_common._IGNORED_REGEX = regex
            watcher_common.ignore_file = ignore_file
            watcher_common.skip_ignored_dirs = skip_ignored_dirs

        regex = sandbox._CONFIG.modules[0].skip_files
        if regex:
            fix_watcher_files(regex)

        def logging_wrapper(func):
            """
                Changes logging to use the DJANGO_COLORS settings
            """
            def _wrapper(level, format, *args, **kwargs):
                if args and len(args) == 1 and isinstance(args[0], dict):
                    args = args[0]
                    status = str(args.get("status", 200))
                else:
                    status = "200"

                try:
                    msg = format % args
                except UnicodeDecodeError:
                    msg += "\n" # This is what Django does in WSGIRequestHandler.log_message

                # Utilize terminal colors, if available
                if status[0] == '2':
                    # Put 2XX first, since it should be the common case
                    msg = self.style.HTTP_SUCCESS(msg)
                elif status[0] == '1':
                    msg = self.style.HTTP_INFO(msg)
                elif status == '304':
                    msg = self.style.HTTP_NOT_MODIFIED(msg)
                elif status[0] == '3':
                    msg = self.style.HTTP_REDIRECT(msg)
                elif status == '404':
                    msg = self.style.HTTP_NOT_FOUND(msg)
                elif status[0] == '4':
                    # 0x16 = Handshake, 0x03 = SSL 3.0 or TLS 1.x
                    if status.startswith(str('\x16\x03')):
                        msg = ("You're accessing the development server over HTTPS, "
                            "but it only supports HTTP.\n")
                    msg = self.style.HTTP_BAD_REQUEST(msg)
                else:
                    # Any 5XX, or any other response
                    msg = self.style.HTTP_SERVER_ERROR(msg)

                return func(level, msg)
            return _wrapper

        module.logging.log = logging_wrapper(module.logging.log)

        python_runtime._RUNTIME_PATH = os.path.join(sdk_path, '_python_runtime.py')
        python_runtime._RUNTIME_ARGS = [sys.executable, python_runtime._RUNTIME_PATH]

        dev_server = NoConfigDevServer()

        try:
            dev_server.start(sandbox._OPTIONS)
            try:
                shutdown.wait_until_shutdown()
            except KeyboardInterrupt:
                pass
        finally:
            dev_server.stop()


        if shutdown_message:
            sys.stdout.write(shutdown_message)

        return
Example #20
0
    def inner_run(self, *args, **options):
        import sys

        shutdown_message = options.get('shutdown_message', '')

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        from djangae.environment import get_application_root
        from djangae.sandbox import _find_sdk_from_python_path
        from djangae.blobstore_service import stop_blobstore_service

        from django.conf import settings
        from django.utils import translation

        stop_blobstore_service()

        # Check for app.yaml
        expected_path = os.path.join(get_application_root(), "app.yaml")
        if not os.path.exists(expected_path):
            sys.stderr.write("Unable to find app.yaml at '%s'\n" % expected_path)
            sys.exit(1)

        self.stdout.write("Validating models...\n\n")
        self.check(display_num_errors=True)
        self.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": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
            "port": self.port,
            "quit_command": quit_command,
        })
        sys.stdout.write("\n")
        sys.stdout.flush()

        # 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)

        # Will have been set by setup_paths
        sdk_path = _find_sdk_from_python_path()

        from google.appengine.tools.devappserver2 import devappserver2
        from google.appengine.tools.devappserver2 import python_runtime

        from djangae import sandbox

        # Add any additional modules specified in the settings
        additional_modules = getattr(settings, "DJANGAE_ADDITIONAL_MODULES", [])
        if additional_modules:
            sandbox._OPTIONS.config_paths.extend(additional_modules)

        if self.addr != sandbox._OPTIONS.host:
            sandbox._OPTIONS.host = sandbox._OPTIONS.admin_host = sandbox._OPTIONS.api_host = self.addr

        # Extra options for `dev_appserver.py`
        for param, value in self.gae_options.items():
            setattr(sandbox._OPTIONS, param, value)

        # External port is a new flag introduced in 1.9.19
        current_version = _VersionList(GetVersionObject()['release'])
        if current_version >= _VersionList('1.9.19') or \
                current_version == sandbox.TEMP_1_9_49_VERSION_NO:
            sandbox._OPTIONS.external_port = None

        # Apply equivalent options for Django args
        sandbox._OPTIONS.automatic_restart = self.use_reloader
        sandbox._OPTIONS.threadsafe_override = self.use_threading

        if sandbox._OPTIONS.host == "127.0.0.1" and os.environ["HTTP_HOST"].startswith("localhost"):
            hostname = "localhost"
            sandbox._OPTIONS.host = "localhost"
        else:
            hostname = sandbox._OPTIONS.host

        os.environ["HTTP_HOST"] = "%s:%s" % (hostname, sandbox._OPTIONS.port)
        os.environ['SERVER_NAME'] = os.environ['HTTP_HOST'].split(':', 1)[0]
        os.environ['SERVER_PORT'] = os.environ['HTTP_HOST'].split(':', 1)[1]
        os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % (os.environ['SERVER_NAME'], os.environ['SERVER_PORT'])

        from google.appengine.api.appinfo import EnvironmentVariables

        class NoConfigDevServer(devappserver2.DevelopmentServer):
            """
                This is horrible, but unfortunately necessary.

                Because we want to enable a sandbox outside of runserver (both when
                running different management commands, but also before/after dev_appserver)
                we have to make sure the following are true:

                1. There is only ever one api server
                2. There is only ever one dispatcher

                Unfortunately, most of the setup is done inside .start() of the DevelopmentServer
                class, there is not really an easy way to hook into part of this without overriding the
                whole .start() method which makes things even more brittle.

                What we do here is hook in at the point that self._dispatcher is set. We ignore whatever
                dispatcher is passed in, but user our own one. We patch api server creation in sandbox.py
                so only ever one api server exists.
            """
            def __init__(self, *args, **kwargs):
                self._patched_dispatcher = None
                super(NoConfigDevServer, self).__init__(*args, **kwargs)

            def start(self, options):
                self.options = options
                return super(NoConfigDevServer, self).start(options)

            def _get_dispatcher(self):
                return self._patched_dispatcher

            def _create_api_server(self, *args, **kwargs):
                """
                    For SDK around 1.9.40 - just return the existing API server
                """
                return sandbox._API_SERVER

            def _set_dispatcher(self, dispatcher):
                """
                    Ignore explicit setting of _dispatcher, use our own
                """

                if dispatcher is None:
                    # Allow wiping the patched dispatcher
                    self._patched_dispatcher = None
                    return

                if self._patched_dispatcher:
                    # We already created the dispatcher, ignore further sets
                    logging.warning("Attempted to set _dispatcher twice")
                    return


                # When the dispatcher is created this property is set so we use it
                # to construct *our* dispatcher
                configuration = dispatcher._configuration

                # We store options in .start() so it's available here
                options = self.options

                # sandbox._create_dispatcher returns a singleton dispatcher instance made in sandbox
                self._patched_dispatcher = sandbox._create_dispatcher(
                    configuration,
                    options
                )

                # the dispatcher may have passed environment variables, it should be propagated
                env_vars = self._dispatcher._configuration.modules[0]._app_info_external.env_variables or EnvironmentVariables()
                for module in configuration.modules:
                    module_name = module._module_name
                    if module_name == 'default' or module_name is None:
                        module_settings = 'DJANGO_SETTINGS_MODULE'
                    else:
                        module_settings = '%s_DJANGO_SETTINGS_MODULE' % module_name
                    if module_settings in env_vars:
                        module_env_vars = module.env_variables or EnvironmentVariables()
                        module_env_vars['DJANGO_SETTINGS_MODULE'] = env_vars[module_settings]

                        old_env_vars = module._app_info_external.env_variables
                        new_env_vars = EnvironmentVariables.Merge(module_env_vars, old_env_vars)
                        module._app_info_external.env_variables = new_env_vars
                self._dispatcher._configuration = configuration
                self._dispatcher._port = options.port
                self._dispatcher._host = options.host

                # Because the dispatcher is a singleton, we need to set the threadsafe override here
                # depending on what was passed to the runserver command. This entire file really needs rebuilding
                # we have way too many hacks in here!
                self._dispatcher._module_to_threadsafe_override[
                    configuration.modules[0].module_name
                ] = options.threadsafe_override

#                self._dispatcher.request_data = request_data
#                request_data._dispatcher = self._dispatcher

                sandbox._API_SERVER._host = options.api_host
                sandbox._API_SERVER.bind_addr = (options.api_host, options.api_port)

                from google.appengine.api import apiproxy_stub_map
                task_queue = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
                # Make sure task running is enabled (it's disabled in the sandbox by default)
                if not task_queue._auto_task_running:
                    task_queue._auto_task_running = True
                    task_queue.StartBackgroundExecution()

            _dispatcher = property(fget=_get_dispatcher, fset=_set_dispatcher)

        from google.appengine.tools.devappserver2 import module

        def fix_watcher_files(regex):
            """ Monkeypatch dev_appserver's file watcher to ignore any unwanted dirs or files. """
            from google.appengine.tools.devappserver2 import watcher_common
            watcher_common._IGNORED_REGEX = regex
            watcher_common.ignore_file = ignore_file
            watcher_common.skip_ignored_dirs = skip_ignored_dirs

        regex = sandbox._CONFIG.modules[0].skip_files
        if regex:
            fix_watcher_files(regex)

        def logging_wrapper(func):
            """
                Changes logging to use the DJANGO_COLORS settings
            """
            def _wrapper(level, format, *args, **kwargs):
                if args and len(args) == 1 and isinstance(args[0], dict):
                    args = args[0]
                    status = str(args.get("status", 200))
                else:
                    status = "200"

                try:
                    msg = format % args
                except UnicodeDecodeError:
                    msg += "\n" # This is what Django does in WSGIRequestHandler.log_message

                # Utilize terminal colors, if available
                if status[0] == '2':
                    # Put 2XX first, since it should be the common case
                    msg = self.style.HTTP_SUCCESS(msg)
                elif status[0] == '1':
                    msg = self.style.HTTP_INFO(msg)
                elif status == '304':
                    msg = self.style.HTTP_NOT_MODIFIED(msg)
                elif status[0] == '3':
                    msg = self.style.HTTP_REDIRECT(msg)
                elif status == '404':
                    msg = self.style.HTTP_NOT_FOUND(msg)
                elif status[0] == '4':
                    # 0x16 = Handshake, 0x03 = SSL 3.0 or TLS 1.x
                    if status.startswith(str('\x16\x03')):
                        msg = ("You're accessing the development server over HTTPS, "
                            "but it only supports HTTP.\n")
                    msg = self.style.HTTP_BAD_REQUEST(msg)
                else:
                    # Any 5XX, or any other response
                    msg = self.style.HTTP_SERVER_ERROR(msg)

                return func(level, msg)
            return _wrapper

        module.logging.log = logging_wrapper(module.logging.log)

        python_runtime._RUNTIME_PATH = os.path.join(sdk_path, '_python_runtime.py')
        python_runtime._RUNTIME_ARGS = [sys.executable, python_runtime._RUNTIME_PATH]

        dev_server = NoConfigDevServer()

        try:
            dev_server.start(sandbox._OPTIONS)
            try:
                shutdown.wait_until_shutdown()
            except KeyboardInterrupt:
                pass
        finally:
            dev_server.stop()


        if shutdown_message:
            sys.stdout.write(shutdown_message)

        return
Example #21
0
    def inner_run(self, *args, **options):
        import sys

        shutdown_message = options.get('shutdown_message', '')

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        from djangae.utils import find_project_root
        from djangae.sandbox import _find_sdk_from_python_path

        from django.conf import settings
        from django.utils import translation

        # Check for app.yaml
        expected_path = os.path.join(find_project_root(), "app.yaml")
        if not os.path.exists(expected_path):
            sys.stderr.write("Unable to find app.yaml at '%s'\n" % expected_path)
            sys.exit(1)

        self.stdout.write("Validating models...\n\n")
        self.validate(display_num_errors=True)
        self.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": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
            "port": self.port,
            "quit_command": quit_command,
        })
        sys.stdout.write("\n")
        sys.stdout.flush()

        # 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)

        # Will have been set by setup_paths
        sdk_path = _find_sdk_from_python_path()

        from google.appengine.tools.devappserver2 import devappserver2
        from google.appengine.tools.devappserver2 import python_runtime

        from djangae import sandbox

        sandbox._OPTIONS.port = int(self.port) if self.port else sandbox._OPTIONS.port
        sandbox._OPTIONS.host = self.addr if self.addr else sandbox._OPTIONS.host
        sandbox._OPTIONS.automatic_restart = self.use_reloader

        class NoConfigDevServer(devappserver2.DevelopmentServer):
            def _create_api_server(self, request_data, storage_path, options, configuration):
                self._dispatcher = sandbox._create_dispatcher(configuration, options)
                request_data._dispatcher = self._dispatcher
                return sandbox._API_SERVER

        python_runtime._RUNTIME_PATH = os.path.join(sdk_path, '_python_runtime.py')
        python_runtime._RUNTIME_ARGS = [sys.executable, python_runtime._RUNTIME_PATH]

        dev_server = NoConfigDevServer()

        try:
            dev_server.start(sandbox._OPTIONS)
            try:
                shutdown.wait_until_shutdown()
            except KeyboardInterrupt:
                pass
        finally:
            dev_server.stop()


        if shutdown_message:
            sys.stdout.write(shutdown_message)

        return
Example #22
0
 def test_wait_until_shutdown(self):
     self.mox.StubOutWithMock(time, 'sleep')
     time.sleep(1).WithSideEffects(lambda _: shutdown.async_quit())
     self.mox.ReplayAll()
     shutdown.wait_until_shutdown()
     self.mox.VerifyAll()
Example #23
0
    def inner_run(self, *args, **options):
        import sys

        shutdown_message = options.get('shutdown_message', '')

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        from djangae.utils import find_project_root
        from djangae.sandbox import _find_sdk_from_python_path
        from djangae.blobstore_service import stop_blobstore_service

        from django.conf import settings
        from django.utils import translation

        stop_blobstore_service()

        # Check for app.yaml
        expected_path = os.path.join(find_project_root(), "app.yaml")
        if not os.path.exists(expected_path):
            sys.stderr.write("Unable to find app.yaml at '%s'\n" % expected_path)
            sys.exit(1)

        self.stdout.write("Validating models...\n\n")
        self.validate(display_num_errors=True)
        self.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": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
            "port": self.port,
            "quit_command": quit_command,
        })
        sys.stdout.write("\n")
        sys.stdout.flush()

        # 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)

        # Will have been set by setup_paths
        sdk_path = _find_sdk_from_python_path()

        from google.appengine.tools.devappserver2 import devappserver2
        from google.appengine.tools.devappserver2 import python_runtime

        from djangae import sandbox

        if int(self.port) != sandbox._OPTIONS.port:
            # Override the port numbers
            sandbox._OPTIONS.port = int(self.port)
            sandbox._OPTIONS.admin_port = int(self.port) + 1
            sandbox._OPTIONS.api_port = int(self.port) + 2

        if self.addr != sandbox._OPTIONS.host:
            sandbox._OPTIONS.host = sandbox._OPTIONS.admin_host = sandbox._OPTIONS.api_host = self.addr

        # External port is a new flag introduced in 1.9.19
        current_version = _VersionList(GetVersionObject()['release'])
        if current_version >= _VersionList('1.9.19'):
            sandbox._OPTIONS.external_port = None

        sandbox._OPTIONS.automatic_restart = self.use_reloader

        if sandbox._OPTIONS.host == "127.0.0.1" and os.environ["HTTP_HOST"].startswith("localhost"):
            hostname = "localhost"
        else:
            hostname = sandbox._OPTIONS.host

        os.environ["HTTP_HOST"] = "%s:%s" % (hostname, sandbox._OPTIONS.port)
        os.environ['SERVER_NAME'] = os.environ['HTTP_HOST'].split(':', 1)[0]
        os.environ['SERVER_PORT'] = os.environ['HTTP_HOST'].split(':', 1)[1]
        os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % (os.environ['SERVER_NAME'], os.environ['SERVER_PORT'])

        class NoConfigDevServer(devappserver2.DevelopmentServer):
            def _create_api_server(self, request_data, storage_path, options, configuration):
                self._dispatcher = sandbox._create_dispatcher(configuration, options)
                self._dispatcher._port = options.port
                self._dispatcher._host = options.host

                self._dispatcher.request_data = request_data
                request_data._dispatcher = self._dispatcher

                sandbox._API_SERVER._host = options.api_host
                sandbox._API_SERVER.bind_addr = (options.api_host, options.api_port)

                from google.appengine.api import apiproxy_stub_map
                task_queue = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
                # Make sure task running is enabled (it's disabled in the sandbox by default)
                if not task_queue._auto_task_running:
                    task_queue._auto_task_running = True
                    task_queue.StartBackgroundExecution()

                return sandbox._API_SERVER

        python_runtime._RUNTIME_PATH = os.path.join(sdk_path, '_python_runtime.py')
        python_runtime._RUNTIME_ARGS = [sys.executable, python_runtime._RUNTIME_PATH]

        dev_server = NoConfigDevServer()

        try:
            dev_server.start(sandbox._OPTIONS)
            try:
                shutdown.wait_until_shutdown()
            except KeyboardInterrupt:
                pass
        finally:
            dev_server.stop()


        if shutdown_message:
            sys.stdout.write(shutdown_message)

        return
 def test_wait_until_shutdown(self):
   self.mox.StubOutWithMock(time, 'sleep')
   time.sleep(1).WithSideEffects(lambda _: shutdown.async_quit())
   self.mox.ReplayAll()
   shutdown.wait_until_shutdown()
   self.mox.VerifyAll()
Example #25
0
    def inner_run(self, *args, **options):
        import sys

        shutdown_message = options.get('shutdown_message', '')

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        from djangae.utils import find_project_root
        from djangae.sandbox import _find_sdk_from_python_path

        from django.conf import settings
        from django.utils import translation

        # Check for app.yaml
        expected_path = os.path.join(find_project_root(), "app.yaml")
        if not os.path.exists(expected_path):
            sys.stderr.write("Unable to find app.yaml at '%s'\n" %
                             expected_path)
            sys.exit(1)

        self.stdout.write("Validating models...\n\n")
        self.validate(display_num_errors=True)
        self.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": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })
        sys.stdout.write("\n")
        sys.stdout.flush()

        # 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)

        # Will have been set by setup_paths
        sdk_path = _find_sdk_from_python_path()

        from google.appengine.tools.devappserver2 import devappserver2
        from google.appengine.tools.devappserver2 import python_runtime

        from djangae import sandbox

        if int(self.port) != sandbox._OPTIONS.port:
            # Override the port numbers
            sandbox._OPTIONS.port = int(self.port)
            sandbox._OPTIONS.admin_port = int(self.port) + 1
            sandbox._OPTIONS.api_port = int(self.port) + 2

        if self.addr != sandbox._OPTIONS.host:
            sandbox._OPTIONS.host = sandbox._OPTIONS.admin_host = sandbox._OPTIONS.api_host = self.addr

        # External port is a new flag introduced in 1.9.19
        current_version = _VersionList(GetVersionObject()['release'])
        if current_version >= _VersionList('1.9.19'):
            sandbox._OPTIONS.external_port = None

        sandbox._OPTIONS.automatic_restart = self.use_reloader

        class NoConfigDevServer(devappserver2.DevelopmentServer):
            def _create_api_server(self, request_data, storage_path, options,
                                   configuration):
                self._dispatcher = sandbox._create_dispatcher(
                    configuration, options)
                self._dispatcher._port = options.port
                self._dispatcher._host = options.host

                # Make sure the dispatcher uses the WSGIRequestInfo object already instantiated in local sandbox.
                # Without this, there will be references to two different request info objects, causing errors when trying
                # to access a request in one that was started in the other.
                def request_data_override(func, _request_data):
                    def _wrapped(api_host, api_port, request_data):
                        return func(api_host, api_port, _request_data)

                    return _wrapped

                self._dispatcher.start = request_data_override(
                    self._dispatcher.start, self._dispatcher._request_data)

                sandbox._API_SERVER._host = options.api_host
                sandbox._API_SERVER.bind_addr = (options.api_host,
                                                 options.api_port)

                request_data._dispatcher = self._dispatcher

                from google.appengine.api import apiproxy_stub_map
                task_queue = apiproxy_stub_map.apiproxy.GetStub('taskqueue')
                # Make sure task running is enabled (it's disabled in the sandbox by default)
                if not task_queue._auto_task_running:
                    task_queue._auto_task_running = True
                    task_queue.StartBackgroundExecution()

                return sandbox._API_SERVER

        python_runtime._RUNTIME_PATH = os.path.join(sdk_path,
                                                    '_python_runtime.py')
        python_runtime._RUNTIME_ARGS = [
            sys.executable, python_runtime._RUNTIME_PATH
        ]

        dev_server = NoConfigDevServer()

        try:
            dev_server.start(sandbox._OPTIONS)
            try:
                shutdown.wait_until_shutdown()
            except KeyboardInterrupt:
                pass
        finally:
            dev_server.stop()

        if shutdown_message:
            sys.stdout.write(shutdown_message)

        return