Beispiel #1
0
def main():
    # Set null device file path to stdout, stdin, stderr if they are None
    for _name in ('stdin', 'stdout', 'stderr'):
        if getattr(sys, _name) is None:
            setattr(sys, _name,
                    open(os.devnull, 'r' if _name == 'stdin' else 'w'))

    # Build Javascript files when DEBUG
    if config.DEBUG:
        from pgadmin.utils.javascript.javascript_bundler import \
            JavascriptBundler, JsState
        app.debug = True

        javascriptBundler = JavascriptBundler()
        javascriptBundler.bundle()
        if javascriptBundler.report() == JsState.NONE:
            app.logger.error(
                "Unable to generate javascript.\n"
                "To run the app ensure that yarn install command runs "
                "successfully")
            raise Exception("No generated javascript, aborting")

    # Output a startup message if we're not under the runtime and startup.
    # If we're under WSGI, we don't need to worry about this
    if not app.PGADMIN_RUNTIME:
        print("Starting %s. Please navigate to http://%s:%d in your browser." %
              (config.APP_NAME, config.DEFAULT_SERVER,
               config.EFFECTIVE_SERVER_PORT))
        sys.stdout.flush()
    else:
        # For unknown reason the Qt runtime does not pass the environment
        # variables (i.e. PYTHONHOME, and PYTHONPATH), to the Python
        # sub-processes, leading to failures executing background processes.
        #
        # This has been observed only on windows. On *nix systems, it is likely
        # picking the system python environment, which is good enough to run
        # the process-executor.
        #
        # Setting PYTHONHOME launch them properly.
        from pgadmin.utils import IS_WIN

        if IS_WIN:
            os.environ['PYTHONHOME'] = sys.prefix

    # Initialize Flask service only once
    # If `WERKZEUG_RUN_MAIN` is None, i.e: app is initializing for first time
    # so set `use_reloader` = False, thus reload won't call.
    # Reference:
    # https://github.com/pallets/werkzeug/issues/220#issuecomment-11176538
    try:
        app.run(
            host=config.DEFAULT_SERVER,
            port=config.EFFECTIVE_SERVER_PORT,
            use_reloader=((not app.PGADMIN_RUNTIME) and app.debug
                          and os.environ.get("WERKZEUG_RUN_MAIN") is not None),
            threaded=config.THREADED_MODE)

    except IOError:
        app.logger.error("Error starting the app server: %s", sys.exc_info())
    def _bundling_fails_when_bundling_returns_nonzero(self):
        javascript_bundler = JavascriptBundler()
        self.assertEqual(len(self.mockSubprocessCall.method_calls), 0)
        self.mockOs.listdir.return_value = []
        self.mockSubprocessCall.return_value = 99

        javascript_bundler.bundle()

        self.__assertState(javascript_bundler, JsState.NONE)
    def _bundling_fails_when_bundling_returns_nonzero(self):
        javascript_bundler = JavascriptBundler()
        self.assertEqual(len(self.mockSubprocessCall.method_calls), 0)
        self.mockOs.listdir.return_value = []
        self.mockSubprocessCall.return_value = 99

        javascript_bundler.bundle()

        self.__assertState(javascript_bundler, JsState.NONE)
    def _bundling_fails_and_there_is_no_existing_bundle_directory(self):
        javascript_bundler = JavascriptBundler()
        self.mockSubprocessCall.side_effect = OSError("mock exception behavior")
        self.mockOs.path.exists.return_value = False
        self.mockOs.listdir.side_effect = OSError("mock exception behavior")

        javascript_bundler.bundle()

        self.__assertState(javascript_bundler, JsState.NONE)
    def _bundling_fails_but_there_was_existing_bundle(self):
        javascript_bundler = JavascriptBundler()
        self.mockSubprocessCall.side_effect = OSError("mock exception behavior")
        self.mockOs.path.exists.return_value = True
        self.mockOs.listdir.return_value = [u'history.js', u'reactComponents.js']

        javascript_bundler.bundle()
        self.mockSubprocessCall.assert_called_once_with(['yarn', 'run', 'bundle:dev'])

        self.__assertState(javascript_bundler, JsState.OLD)
    def _bundling_fails_and_there_is_no_existing_bundle_directory(self):
        javascript_bundler = JavascriptBundler()
        self.mockSubprocessCall.side_effect = OSError(
            "mock exception behavior")
        self.mockOs.path.exists.return_value = False
        self.mockOs.listdir.side_effect = OSError("mock exception behavior")

        javascript_bundler.bundle()

        self.__assertState(javascript_bundler, JsState.NONE)
    def _bundling_succeeds(self):
        javascript_bundler = JavascriptBundler()
        self.assertEqual(len(self.mockSubprocessCall.method_calls), 0)
        self.mockSubprocessCall.return_value = 0

        self.mockOs.listdir.return_value = [u'history.js', u'reactComponents.js']

        javascript_bundler.bundle()
        self.mockSubprocessCall.assert_called_once_with(['yarn', 'run', 'bundle:dev'])

        self.__assertState(javascript_bundler, JsState.NEW)
    def _bundling_succeeds(self):
        javascript_bundler = JavascriptBundler()
        self.assertEqual(len(self.mockSubprocessCall.method_calls), 0)
        self.mockSubprocessCall.return_value = 0

        self.mockOs.listdir.return_value = [
            u'history.js', u'reactComponents.js']

        javascript_bundler.bundle()
        self.mockSubprocessCall.assert_called_once_with(
            ['yarn', 'run', 'bundle:dev'])

        self.__assertState(javascript_bundler, JsState.NEW)
    def _bundling_fails_but_there_was_existing_bundle(self):
        javascript_bundler = JavascriptBundler()
        self.mockSubprocessCall.side_effect = OSError(
            "mock exception behavior")
        self.mockOs.path.exists.return_value = True
        self.mockOs.listdir.return_value = [
            u'history.js', u'reactComponents.js']

        javascript_bundler.bundle()
        self.mockSubprocessCall.assert_called_once_with(
            ['yarn', 'run', 'bundle:dev'])

        self.__assertState(javascript_bundler, JsState.OLD)
Beispiel #10
0
            path_info = environ["PATH_INFO"]
            if path_info.startswith(script_name):
                environ["PATH_INFO"] = path_info[len(script_name):]
        scheme = environ.get("HTTP_X_SCHEME", "")
        if scheme:
            environ["wsgi.url_scheme"] = scheme
        return self.app(environ, start_response)


##########################################################################
# Server startup
##########################################################################

# Build Javascript files
if config.DEBUG:
    javascriptBundler = JavascriptBundler()
    javascriptBundler.bundle()

# Create the app!
app = create_app()

if config.SERVER_MODE:
    app.wsgi_app = ReverseProxied(app.wsgi_app)

if config.DEBUG:
    app.debug = True
else:
    app.debug = False

# respond to JS
if config.DEBUG:
            path_info = environ["PATH_INFO"]
            if path_info.startswith(script_name):
                environ["PATH_INFO"] = path_info[len(script_name):]
        scheme = environ.get("HTTP_X_SCHEME", "")
        if scheme:
            environ["wsgi.url_scheme"] = scheme
        return self.app(environ, start_response)


##########################################################################
# Server startup
##########################################################################

# Build Javascript files
if config.DEBUG:
    javascriptBundler = JavascriptBundler()
    javascriptBundler.bundle()

# Create the app!
app = create_app()

if config.SERVER_MODE:
    app.wsgi_app = ReverseProxied(app.wsgi_app)

if config.DEBUG:
    app.debug = True
else:
    app.debug = False

# respond to JS
if config.DEBUG and javascriptBundler.report() == JsState.NONE: