コード例 #1
0
def skip_ignored_dirs(*args, **kwargs):
    """ Replacement for devappserver2.watchter_common.skip_ignored_dirs
    - to be monkeypatched into place.
    """
    # Note that this function modifies the `dirs` list in place, it doesn't return anything.
    # Also note that `dirs` is a list of dir *names* not dir *paths*, which means that we can't
    # differentiate between /foo/bar and /moo/bar because we just get 'bar'. But allowing that
    # would require a whole load more monkey patching.
    from djangae import sandbox
    from google.appengine.tools.devappserver2 import watcher_common

    # since version 1.9.49 (which is incorrectly marked as [0, 0, 0] for now, until
    # https://code.google.com/p/googleappengine/issues/detail?id=13439 will be fixed,
    # skip_ignored_dirs have three arguments instead of one. To preserve
    # backwards compatibilty we check version here and use args to fetch one
    # or three arguments depending on version. We do not do any further error handling
    # here to make sure that this explicitly fail if there is another change in
    # the number of arguments with new SDK versions.
    current_version = _VersionList(GetVersionObject()['release'])
    if current_version == sandbox.TEMP_1_9_49_VERSION_NO:
        current_version = _VersionList('1.9.49')

    if current_version >= _VersionList('1.9.49'):
        dirpath, dirs, skip_files_re = args
    else:
        dirs = args[0]
    watcher_common._remove_pred(dirs, lambda d: d.startswith(watcher_common._IGNORED_PREFIX))
    watcher_common._remove_pred(
        dirs,
        lambda d: any(regex.search(d) for regex in DJANGAE_RUNSERVER_IGNORED_DIR_REGEXES)
    )
コード例 #2
0
ファイル: runserver.py プロジェクト: scottg1989/djangae
def skip_ignored_dirs(*args, **kwargs):
    """ Replacement for devappserver2.watchter_common.skip_ignored_dirs
    - to be monkeypatched into place.
    """
    # Note that this function modifies the `dirs` list in place, it doesn't return anything.
    # Also note that `dirs` is a list of dir *names* not dir *paths*, which means that we can't
    # differentiate between /foo/bar and /moo/bar because we just get 'bar'. But allowing that
    # would require a whole load more monkey patching.
    from djangae import sandbox
    from google.appengine.tools.devappserver2 import watcher_common

    # since version 1.9.49 (which is incorrectly marked as [0, 0, 0] for now, until
    # https://code.google.com/p/googleappengine/issues/detail?id=13439 will be fixed,
    # skip_ignored_dirs have three arguments instead of one. To preserve
    # backwards compatibilty we check version here and use args to fetch one
    # or three arguments depending on version. We do not do any further error handling
    # here to make sure that this explicitly fail if there is another change in
    # the number of arguments with new SDK versions.
    current_version = _VersionList(GetVersionObject()['release'])
    if current_version == sandbox.TEMP_1_9_49_VERSION_NO:
        current_version = _VersionList('1.9.49')

    if current_version >= _VersionList('1.9.49'):
        dirpath, dirs, skip_files_re = args
    else:
        dirs = args[0]
    watcher_common._remove_pred(dirs, lambda d: d.startswith(watcher_common._IGNORED_PREFIX))
    watcher_common._remove_pred(
        dirs,
        lambda d: any(regex.search(d) for regex in DJANGAE_RUNSERVER_IGNORED_DIR_REGEXES)
    )
コード例 #3
0
def _create_dispatcher(configuration, options):
    from google.appengine.tools.devappserver2 import dispatcher
    from google.appengine.tools.devappserver2.devappserver2 import DevelopmentServer

    try:
        from google.appengine.tools.devappserver2.devappserver2 import _LOG_LEVEL_TO_RUNTIME_CONSTANT
    except ImportError:
        from google.appengine.tools.devappserver2.constants import LOG_LEVEL_TO_RUNTIME_CONSTANT
        _LOG_LEVEL_TO_RUNTIME_CONSTANT = LOG_LEVEL_TO_RUNTIME_CONSTANT

    from google.appengine.tools.sdk_update_checker import GetVersionObject, \
                                                          _VersionList

    if hasattr(_create_dispatcher, "singleton"):
        return _create_dispatcher.singleton

    class UnsupportedOption(object):
        pass

    current_version = _VersionList(GetVersionObject()['release'])
    supports_go_config = current_version >= _VersionList('1.9.50')
    supports_custom_config = current_version >= _VersionList(
        '1.9.22') or current_version == TEMP_1_9_49_VERSION_NO
    supports_external_port = current_version >= _VersionList(
        '1.9.19') or current_version == TEMP_1_9_49_VERSION_NO
    supports_watcher_ignore_re = current_version >= _VersionList('1.9.54')

    dispatcher_args = [
        configuration, options.host, options.port, options.auth_domain,
        _LOG_LEVEL_TO_RUNTIME_CONSTANT[options.log_level],
        DevelopmentServer._create_php_config(options),
        DevelopmentServer._create_python_config(options),
        DevelopmentServer._create_java_config(options),
        DevelopmentServer._create_go_config(options)
        if supports_go_config else UnsupportedOption,
        None if supports_custom_config else UnsupportedOption,
        DevelopmentServer._create_cloud_sql_config(options),
        DevelopmentServer._create_vm_config(options),
        DevelopmentServer._create_module_to_setting(
            options.max_module_instances, configuration,
            '--max_module_instances'), options.use_mtime_file_watcher,
        None if supports_watcher_ignore_re else UnsupportedOption,
        options.automatic_restart, options.allow_skipped_files,
        DevelopmentServer._create_module_to_setting(
            options.threadsafe_override, configuration,
            '--threadsafe_override'),
        options.external_port if supports_external_port else UnsupportedOption
    ]

    dispatcher_args = [
        x for x in dispatcher_args if not x is UnsupportedOption
    ]

    _create_dispatcher.singleton = dispatcher.Dispatcher(*dispatcher_args)

    return _create_dispatcher.singleton
コード例 #4
0
ファイル: sandbox.py プロジェクト: rehabstudio/djangae
def _create_dispatcher(configuration, options):
    from google.appengine.tools.devappserver2 import dispatcher
    from google.appengine.tools.devappserver2.devappserver2 import DevelopmentServer

    try:
        from google.appengine.tools.devappserver2.devappserver2 import _LOG_LEVEL_TO_RUNTIME_CONSTANT
    except ImportError:
        from google.appengine.tools.devappserver2.constants import LOG_LEVEL_TO_RUNTIME_CONSTANT
        _LOG_LEVEL_TO_RUNTIME_CONSTANT = LOG_LEVEL_TO_RUNTIME_CONSTANT

    from google.appengine.tools.sdk_update_checker import GetVersionObject, \
                                                          _VersionList

    if hasattr(_create_dispatcher, "singleton"):
        return _create_dispatcher.singleton

    class UnsupportedOption(object):
        pass

    current_version = _VersionList(GetVersionObject()['release'])
    supports_go_config = current_version >= _VersionList('1.9.50')
    supports_custom_config = current_version >= _VersionList('1.9.22') or current_version == TEMP_1_9_49_VERSION_NO
    supports_external_port = current_version >= _VersionList('1.9.19') or current_version == TEMP_1_9_49_VERSION_NO
    supports_watcher_ignore_re = current_version >= _VersionList('1.9.54')

    dispatcher_args = [
        configuration,
        options.host,
        options.port,
        options.auth_domain,
        _LOG_LEVEL_TO_RUNTIME_CONSTANT[options.log_level],
        DevelopmentServer._create_php_config(options),
        DevelopmentServer._create_python_config(options),
        DevelopmentServer._create_java_config(options),
        DevelopmentServer._create_go_config(options) if supports_go_config else UnsupportedOption,
        None if supports_custom_config else UnsupportedOption,
        DevelopmentServer._create_cloud_sql_config(options),
        DevelopmentServer._create_vm_config(options),
        DevelopmentServer._create_module_to_setting(options.max_module_instances,
                                       configuration, '--max_module_instances'),
        options.use_mtime_file_watcher,
        None if supports_watcher_ignore_re else UnsupportedOption,
        options.automatic_restart,
        options.allow_skipped_files,
        DevelopmentServer._create_module_to_setting(options.threadsafe_override,
                                       configuration, '--threadsafe_override'),
        options.external_port if supports_external_port else UnsupportedOption
    ]

    dispatcher_args = [x for x in dispatcher_args if not x is UnsupportedOption]

    _create_dispatcher.singleton = dispatcher.Dispatcher(*dispatcher_args)

    return _create_dispatcher.singleton
コード例 #5
0
def _create_dispatcher(configuration, options):
    from google.appengine.tools.devappserver2 import dispatcher
    from google.appengine.tools.devappserver2.devappserver2 import (
        DevelopmentServer, _LOG_LEVEL_TO_RUNTIME_CONSTANT
    )
    from google.appengine.tools.sdk_update_checker import GetVersionObject, \
                                                          _VersionList

    if hasattr(_create_dispatcher, "singleton"):
        return _create_dispatcher.singleton

    dispatcher_args = [
        configuration,
        options.host,
        options.port,
        options.auth_domain,
        _LOG_LEVEL_TO_RUNTIME_CONSTANT[options.log_level],
        DevelopmentServer._create_php_config(options),
        DevelopmentServer._create_python_config(options),
        DevelopmentServer._create_java_config(options),
        DevelopmentServer._create_cloud_sql_config(options),
        DevelopmentServer._create_vm_config(options),
        DevelopmentServer._create_module_to_setting(options.max_module_instances,
                                       configuration, '--max_module_instances'),
        options.use_mtime_file_watcher,
        options.automatic_restart,
        options.allow_skipped_files,
        DevelopmentServer._create_module_to_setting(options.threadsafe_override,
                                       configuration, '--threadsafe_override')
    ]

    # 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 == TEMP_1_9_49_VERSION_NO:
        dispatcher_args.append(options.external_port)

    if current_version >= _VersionList('1.9.50'):
        dispatcher_args.insert(7, DevelopmentServer._create_go_config(options))

    if current_version >= _VersionList('1.9.22') or \
            current_version == TEMP_1_9_49_VERSION_NO:
        dispatcher_args.insert(8, None) # Custom config setting

    _create_dispatcher.singleton = dispatcher.Dispatcher(*dispatcher_args)

    return _create_dispatcher.singleton
コード例 #6
0
ファイル: sandbox.py プロジェクト: owad/djangae
def _create_dispatcher(configuration, options):
    from google.appengine.tools.devappserver2 import dispatcher
    from google.appengine.tools.devappserver2.devappserver2 import (
        DevelopmentServer, _LOG_LEVEL_TO_RUNTIME_CONSTANT
    )
    from google.appengine.tools.sdk_update_checker import GetVersionObject, \
                                                          _VersionList

    if hasattr(_create_dispatcher, "singleton"):
        return _create_dispatcher.singleton

    dispatcher_args = [
        configuration,
        options.host,
        options.port,
        options.auth_domain,
        _LOG_LEVEL_TO_RUNTIME_CONSTANT[options.log_level],
        DevelopmentServer._create_php_config(options),
        DevelopmentServer._create_python_config(options),
        DevelopmentServer._create_java_config(options),
        DevelopmentServer._create_cloud_sql_config(options),
        DevelopmentServer._create_vm_config(options),
        DevelopmentServer._create_module_to_setting(options.max_module_instances,
                                       configuration, '--max_module_instances'),
        options.use_mtime_file_watcher,
        options.automatic_restart,
        options.allow_skipped_files,
        DevelopmentServer._create_module_to_setting(options.threadsafe_override,
                                       configuration, '--threadsafe_override')
    ]

    # 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 == TEMP_1_9_49_VERSION_NO:
        dispatcher_args.append(options.external_port)

    if current_version >= _VersionList('1.9.50'):
        dispatcher_args.insert(7, DevelopmentServer._create_go_config(options))

    if current_version >= _VersionList('1.9.22') or \
            current_version == TEMP_1_9_49_VERSION_NO:
        dispatcher_args.insert(8, None) # Custom config setting

    _create_dispatcher.singleton = dispatcher.Dispatcher(*dispatcher_args)

    return _create_dispatcher.singleton
コード例 #7
0
ファイル: checks.py プロジェクト: andreipetre/djangae
def check_app_engine_sdk_version(app_configs=None, **kwargs):
    errors = []
    if GetVersionObject:
        sdk_version = tuple(_VersionList(GetVersionObject()['release']))
        if sdk_version > MAX_APP_ENGINE_SDK_VERSION:
            errors.append(Warning(
                "MAX_APP_ENGINE_SDK_VERSION",
                hint="You are using a version of the App Engine SDK that is not yet supported",
                id='djangae.W002',
            ))
    return errors
コード例 #8
0
ファイル: checks.py プロジェクト: vzts/djangae
def check_app_engine_sdk_version(app_configs=None, **kwargs):
    errors = []
    if GetVersionObject:
        sdk_version = tuple(_VersionList(GetVersionObject()['release']))
        if sdk_version > MAX_APP_ENGINE_SDK_VERSION:
            errors.append(
                Warning(
                    "MAX_APP_ENGINE_SDK_VERSION",
                    hint=
                    "You are using a version of the App Engine SDK that is not yet supported",
                    id='djangae.W002',
                ))
    return errors
コード例 #9
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
コード例 #10
0
ファイル: runserver.py プロジェクト: stucox/djangae
    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
コード例 #11
0
ファイル: runserver.py プロジェクト: VincentStephens/djangae
    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
コード例 #12
0
ファイル: runserver.py プロジェクト: rehabstudio/djangae
    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
コード例 #13
0
def _local(devappserver2=None,
           configuration=None,
           options=None,
           wsgi_request_info=None,
           **kwargs):

    # If we use `_LocalRequestInfo`, deferred tasks don't seem to work,
    # but with the default `WSGIRequestInfo`, building the request url for
    # blobstore uploads fails. So we inherit from `WSGIRequestInfo` and copy
    # the `get_request_url` from `_LocalRequestInfo`
    class CustomWSGIRequestInfo(wsgi_request_info.WSGIRequestInfo):
        def get_request_url(self, request_id):
            """Returns the URL the request e.g. 'http://localhost:8080/foo?bar=baz'.

            Args:
              request_id: The string id of the request making the API call.

            Returns:
              The URL of the request as a string.
            """
            try:
                host = os.environ['HTTP_HOST']
            except KeyError:
                host = os.environ['SERVER_NAME']
                port = os.environ['SERVER_PORT']
                if port != '80':
                    host += ':' + port
            url = 'http://' + host
            url += urllib.quote(os.environ.get('PATH_INFO', '/'))
            if os.environ.get('QUERY_STRING'):
                url += '?' + os.environ['QUERY_STRING']
            return url

    global _API_SERVER

    _disable_sqlite_stub_logging()

    original_environ = os.environ.copy()

    # Silence warnings about this being unset, localhost:8080 is the dev_appserver default.
    # Note that we're setting things for the *Blobstore* handler in os.environ here, which seems
    # kind of crazy, and probably is, but it seems to be necessary to make stuff work.
    url = "localhost"
    port = get_next_available_port(url, DEFAULT_BLOBSTORE_SERVICE_PORT)
    os.environ.setdefault("HTTP_HOST", "{}:{}".format(url, port))
    os.environ['SERVER_NAME'] = url
    os.environ['SERVER_PORT'] = str(port)
    os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % (
        os.environ['SERVER_NAME'], os.environ['SERVER_PORT'])

    devappserver2._setup_environ(configuration.app_id)

    from google.appengine.tools.devappserver2 import api_server
    from google.appengine.tools.sdk_update_checker import GetVersionObject, _VersionList

    if hasattr(api_server, "get_storage_path"):
        storage_path = api_server.get_storage_path(options.storage_path,
                                                   configuration.app_id)
    else:
        # SDK < 1.9.51
        storage_path = devappserver2._get_storage_path(options.storage_path,
                                                       configuration.app_id)

    dispatcher = _create_dispatcher(configuration, options)
    request_data = CustomWSGIRequestInfo(dispatcher)
    # Remember the wsgi request info object so it can be reused to avoid duplication.
    dispatcher._request_data = request_data

    # We set the API and Admin ports so that they are beyond any modules (if you
    # have 10 modules then these values will shift, but it's better that they are predictable
    # in the common case)
    options.api_port = get_next_available_port(url, DEFAULT_API_PORT)
    options.admin_port = get_next_available_port(
        url, max(DEFAULT_ADMIN_PORT, options.api_port))

    if hasattr(api_server, "create_api_server"):
        current_version = _VersionList(GetVersionObject()['release'])
        app_rather_than_config = current_version >= _VersionList('1.9.54')

        # Google changed the argument structure in version 1.9.54 so we have to
        # conditionally supply the args here
        if app_rather_than_config:
            _API_SERVER = api_server.create_api_server(
                request_data, storage_path, options, configuration.app_id,
                environment.get_application_root())
        else:
            _API_SERVER = api_server.create_api_server(request_data,
                                                       storage_path, options,
                                                       configuration)

        # We have to patch api_server.create_api_server to return _API_SERVER
        # every time it's called, without this we end up with all kinds of
        # problems. Basically we need one api server for the lifetime of the
        # sandbox (including in `runserver`)
        def create_api_server_patch(*args, **kwargs):
            return _API_SERVER

        api_server.create_api_server = create_api_server_patch

    else:

        _API_SERVER = devappserver2.DevelopmentServer._create_api_server(
            request_data, storage_path, options, configuration)

    from .blobstore_service import start_blobstore_service, stop_blobstore_service

    start_blobstore_service()
    try:
        yield
    finally:
        api_server.cleanup_stubs()
        os.environ = original_environ
        stop_blobstore_service()
コード例 #14
0
ファイル: runserver.py プロジェクト: jscissr/djangae
    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
コード例 #15
0
ファイル: sandbox.py プロジェクト: andreipetre/djangae
def _local(devappserver2=None, configuration=None, options=None, wsgi_request_info=None, **kwargs):

    # If we use `_LocalRequestInfo`, deferred tasks don't seem to work,
    # but with the default `WSGIRequestInfo`, building the request url for
    # blobstore uploads fails. So we inherit from `WSGIRequestInfo` and copy
    # the `get_request_url` from `_LocalRequestInfo`
    class CustomWSGIRequestInfo(wsgi_request_info.WSGIRequestInfo):
        def get_request_url(self, request_id):
            """Returns the URL the request e.g. 'http://localhost:8080/foo?bar=baz'.

            Args:
              request_id: The string id of the request making the API call.

            Returns:
              The URL of the request as a string.
            """
            try:
                host = os.environ['HTTP_HOST']
            except KeyError:
                host = os.environ['SERVER_NAME']
                port = os.environ['SERVER_PORT']
                if port != '80':
                    host += ':' + port
            url = 'http://' + host
            url += quote(os.environ.get('PATH_INFO', '/'))
            if os.environ.get('QUERY_STRING'):
                url += '?' + os.environ['QUERY_STRING']
            return url

    global _API_SERVER

    _disable_sqlite_stub_logging()

    original_environ = os.environ.copy()

    # Silence warnings about this being unset, localhost:8080 is the dev_appserver default.
    # Note that we're setting things for the *Blobstore* handler in os.environ here, which seems
    # kind of crazy, and probably is, but it seems to be necessary to make stuff work.
    url = "localhost"
    port = get_next_available_port(url, DEFAULT_BLOBSTORE_SERVICE_PORT)
    os.environ.setdefault("HTTP_HOST", "{}:{}".format(url, port))
    os.environ['SERVER_NAME'] = url
    os.environ['SERVER_PORT'] = str(port)
    os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % (os.environ['SERVER_NAME'], os.environ['SERVER_PORT'])

    devappserver2._setup_environ(configuration.app_id)

    from google.appengine.tools.devappserver2 import api_server
    from google.appengine.tools.sdk_update_checker import GetVersionObject, _VersionList

    if hasattr(api_server, "get_storage_path"):
        storage_path = api_server.get_storage_path(options.storage_path, configuration.app_id)
    else:
        # SDK < 1.9.51
        storage_path = devappserver2._get_storage_path(options.storage_path, configuration.app_id)

    dispatcher = _create_dispatcher(configuration, options)
    request_data = CustomWSGIRequestInfo(dispatcher)
    # Remember the wsgi request info object so it can be reused to avoid duplication.
    dispatcher._request_data = request_data

    # We set the API and Admin ports so that they are beyond any modules (if you
    # have 10 modules then these values will shift, but it's better that they are predictable
    # in the common case)
    options.api_port = get_next_available_port(url, max(DEFAULT_API_PORT, port + 1))
    options.admin_port = get_next_available_port(url, max(DEFAULT_ADMIN_PORT, options.api_port + 1))

    if hasattr(api_server, "create_api_server"):
        current_version = _VersionList(GetVersionObject()['release'])
        app_rather_than_config = current_version >= _VersionList('1.9.54')

        # Google changed the argument structure in version 1.9.54 so we have to
        # conditionally supply the args here
        if app_rather_than_config:
            _API_SERVER = api_server.create_api_server(
                request_data,
                storage_path,
                options,
                configuration.app_id,
                environment.get_application_root()
            )
        else:
            _API_SERVER = api_server.create_api_server(
                request_data, storage_path, options, configuration
            )

        # We have to patch api_server.create_api_server to return _API_SERVER
        # every time it's called, without this we end up with all kinds of
        # problems. Basically we need one api server for the lifetime of the
        # sandbox (including in `runserver`)
        def create_api_server_patch(*args, **kwargs):
            return _API_SERVER

        api_server.create_api_server = create_api_server_patch

    else:

        _API_SERVER = devappserver2.DevelopmentServer._create_api_server(
            request_data, storage_path, options, configuration
        )

    from .blobstore_service import start_blobstore_service, stop_blobstore_service

    start_blobstore_service()
    try:
        yield
    finally:
        api_server.cleanup_stubs()
        os.environ = original_environ
        stop_blobstore_service()
コード例 #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.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
コード例 #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.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