Ejemplo n.º 1
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
    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, DEFAULT_ADMIN_PORT)

    if hasattr(api_server, "create_api_server"):
        _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()
Ejemplo n.º 2
0
 def tearDown(self):
   api_server.cleanup_stubs()
Ejemplo n.º 3
0
 def tearDown(self):
     api_server.cleanup_stubs()
Ejemplo n.º 4
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 += 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()
Ejemplo n.º 5
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
    url = "localhost"
    port = get_next_available_port(url, 8080)
    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)
    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

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

    from .blobstore_service import start_blobstore_service, stop_blobstore_service
    from google.appengine.tools.devappserver2 import api_server
    start_blobstore_service()
    try:
        yield
    finally:
        api_server.cleanup_stubs()
        os.environ = original_environ
        stop_blobstore_service()
Ejemplo n.º 6
0
def tear_down_local():
    from google.appengine.tools.devappserver2.api_server import cleanup_stubs
    cleanup_stubs()