2014-12-16 15:54:29 +00:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2017-01-04 13:33:32 +00:00
|
|
|
# Copyright (C) 2013 - 2017, The pgAdmin Development Team
|
2014-12-16 15:54:29 +00:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
2015-06-29 06:58:41 +00:00
|
|
|
"""This is the main application entry point for pgAdmin 4. If running on
|
2015-01-21 12:00:13 +00:00
|
|
|
a webserver, this will provide the WSGI interface, otherwise, we're going
|
|
|
|
to start a web server."""
|
|
|
|
|
2015-06-30 05:51:55 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2014-12-16 15:54:29 +00:00
|
|
|
|
|
|
|
# We need to include the root directory in sys.path to ensure that we can
|
|
|
|
# find everything we need when running in the standalone runtime.
|
2015-03-10 13:09:11 +00:00
|
|
|
root = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
if sys.path[0] != root:
|
|
|
|
sys.path.insert(0, root)
|
2014-12-16 15:54:29 +00:00
|
|
|
|
2014-12-16 17:14:48 +00:00
|
|
|
import config
|
2014-12-18 17:49:09 +00:00
|
|
|
from pgadmin import create_app
|
2013-10-04 16:12:10 +00:00
|
|
|
|
2016-06-24 11:50:52 +00:00
|
|
|
# Get the config database schema version. We store this in pgadmin.model
|
|
|
|
# as it turns out that putting it in the config files isn't a great idea
|
|
|
|
from pgadmin.model import SCHEMA_VERSION
|
|
|
|
config.SETTINGS_SCHEMA_VERSION = SCHEMA_VERSION
|
|
|
|
|
2015-01-26 15:20:28 +00:00
|
|
|
##########################################################################
|
|
|
|
# Sanity checks
|
|
|
|
##########################################################################
|
|
|
|
|
2016-05-26 12:00:27 +00:00
|
|
|
# Check if the database exists. If it does not, create it.
|
2015-01-26 15:20:28 +00:00
|
|
|
if not os.path.isfile(config.SQLITE_PATH):
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 10:00:57 +00:00
|
|
|
from pgadmin.utils import u, fs_encoding, file_quote
|
|
|
|
setupfile = os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(u(__file__, fs_encoding))), u'setup.py'
|
|
|
|
)
|
|
|
|
exec(open(file_quote(setupfile), 'r').read())
|
2015-01-26 15:20:28 +00:00
|
|
|
|
2014-12-17 15:27:54 +00:00
|
|
|
##########################################################################
|
|
|
|
# Server starup
|
|
|
|
##########################################################################
|
|
|
|
|
2014-12-18 17:49:09 +00:00
|
|
|
# Create the app!
|
|
|
|
app = create_app()
|
|
|
|
|
2015-06-29 08:11:56 +00:00
|
|
|
if config.DEBUG:
|
|
|
|
app.debug = True
|
2015-10-20 07:03:18 +00:00
|
|
|
else:
|
|
|
|
app.debug = False
|
2015-06-29 06:58:41 +00:00
|
|
|
|
2013-10-04 16:12:10 +00:00
|
|
|
# Start the web server. The port number should have already been set by the
|
2015-06-29 06:58:41 +00:00
|
|
|
# runtime if we're running in desktop mode, otherwise we'll just use the
|
2014-12-16 12:53:09 +00:00
|
|
|
# Flask default.
|
2016-06-17 09:03:32 +00:00
|
|
|
PGADMIN_RUNTIME = False
|
2014-12-16 12:53:09 +00:00
|
|
|
if 'PGADMIN_PORT' in globals():
|
2016-06-17 09:03:32 +00:00
|
|
|
app.logger.debug('Running under the desktop runtime, port: %s',
|
2015-06-30 05:51:55 +00:00
|
|
|
globals()['PGADMIN_PORT'])
|
|
|
|
server_port = int(globals()['PGADMIN_PORT'])
|
2016-06-17 09:03:32 +00:00
|
|
|
PGADMIN_RUNTIME = True
|
2017-02-22 12:41:28 +00:00
|
|
|
elif 'PGADMIN_PORT' in os.environ:
|
|
|
|
port = os.environ['PGADMIN_PORT']
|
|
|
|
app.logger.debug(
|
|
|
|
'Not running under the desktop runtime, port: %s',
|
|
|
|
port)
|
|
|
|
server_port = int(port)
|
2014-12-16 12:53:09 +00:00
|
|
|
else:
|
2015-06-30 05:51:55 +00:00
|
|
|
app.logger.debug(
|
2016-06-17 09:03:32 +00:00
|
|
|
'Not running under the desktop runtime, port: %s',
|
2015-06-30 05:51:55 +00:00
|
|
|
config.DEFAULT_SERVER_PORT)
|
2014-12-16 17:14:48 +00:00
|
|
|
server_port = config.DEFAULT_SERVER_PORT
|
2014-12-16 12:53:09 +00:00
|
|
|
|
2016-06-21 09:42:20 +00:00
|
|
|
# Let the application save the status about the runtime for using it later.
|
|
|
|
app.PGADMIN_RUNTIME = PGADMIN_RUNTIME
|
|
|
|
|
2017-03-06 14:53:49 +00:00
|
|
|
# Set the key if appropriate
|
|
|
|
if 'PGADMIN_KEY' in globals():
|
|
|
|
app.PGADMIN_KEY = globals()['PGADMIN_KEY']
|
|
|
|
app.logger.debug("Desktop security key: %s" % app.PGADMIN_KEY)
|
|
|
|
else:
|
|
|
|
app.PGADMIN_KEY = ''
|
|
|
|
|
2016-08-18 12:43:00 +00:00
|
|
|
# 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 __name__ == '__main__':
|
|
|
|
if not PGADMIN_RUNTIME:
|
2016-08-23 11:41:41 +00:00
|
|
|
print("Starting %s. Please navigate to http://%s:%d in your browser." %
|
|
|
|
(config.APP_NAME, config.DEFAULT_SERVER, server_port))
|
2016-08-18 12:43:00 +00:00
|
|
|
sys.stdout.flush()
|
2017-02-10 22:42:44 +00:00
|
|
|
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.
|
2017-03-09 09:54:55 +00:00
|
|
|
from pgadmin.utils import IS_WIN
|
|
|
|
if IS_WIN:
|
|
|
|
os.environ['PYTHONHOME'] = sys.prefix
|
2016-06-14 11:00:06 +00:00
|
|
|
|
2016-08-18 12:43:00 +00:00
|
|
|
try:
|
|
|
|
app.run(
|
|
|
|
host=config.DEFAULT_SERVER,
|
|
|
|
port=server_port,
|
|
|
|
use_reloader=((not PGADMIN_RUNTIME) and app.debug),
|
|
|
|
threaded=config.THREADED_MODE
|
|
|
|
)
|
|
|
|
except IOError:
|
|
|
|
app.logger.error("Error starting the app server: %s", sys.exc_info())
|