From ab80dc34d6374e99528a52b372d2910ae7ecc6ff Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 19 Jun 2014 17:50:41 +0200 Subject: [PATCH] Add option to run as non-daemon mode - Pass on debug option with Django - Cleanup option parsing --- cfg.py | 2 ++ logger.py | 10 +++++-- plinth.py | 88 +++++++++++++++++++++++-------------------------------- 3 files changed, 46 insertions(+), 54 deletions(-) diff --git a/cfg.py b/cfg.py index 124bb0ebc..deb3f943a 100644 --- a/cfg.py +++ b/cfg.py @@ -31,6 +31,8 @@ access_log_file = get_item(parser, 'Path', 'access_log_file') pidfile = get_item(parser, 'Path', 'pidfile') host = get_item(parser, 'Network', 'host') port = int(get_item(parser, 'Network', 'port')) +debug = False +no_daemon = False main_menu = Menu() diff --git a/logger.py b/logger.py index 9a753948e..581255ebe 100644 --- a/logger.py +++ b/logger.py @@ -2,9 +2,13 @@ import cherrypy import inspect import cfg -cherrypy.log.error_file = cfg.status_log_file -cherrypy.log.access_file = cfg.access_log_file -cherrypy.log.screen = False + +def init(): + """Initialize logging""" + cherrypy.log.error_file = cfg.status_log_file + cherrypy.log.access_file = cfg.access_log_file + if not cfg.no_daemon: + cherrypy.log.screen = False class Logger(object): diff --git a/plinth.py b/plinth.py index ddcce62f0..35234e8b5 100755 --- a/plinth.py +++ b/plinth.py @@ -10,12 +10,12 @@ if not os.path.join(cfg.file_root, "vendor") in sys.path: import cherrypy from cherrypy import _cpserver from cherrypy.process.plugins import Daemonizer -Daemonizer(cherrypy.engine).subscribe() import module_loader import plugin_mount import service +import logger from logger import Logger __version__ = "0.2.14" @@ -28,42 +28,27 @@ __status__ = "Development" def parse_arguments(): - parser = argparse.ArgumentParser(description='Plinth web interface for the FreedomBox.') - parser.add_argument('--pidfile', - help='specify a file in which the server may write its pid') - # FIXME make this work with basehref for static files. - parser.add_argument('--server_dir', - help='specify where to host the server.') - parser.add_argument("--debug", action="store_true", - help="Debug flag. Don't use.") + """Parse command line arguments""" + parser = argparse.ArgumentParser( + description='Plinth web interface for FreedomBox') + parser.add_argument( + '--pidfile', default='plinth.pid', + help='specify a file in which the server may write its pid') + parser.add_argument( + '--server_dir', default='/', + help='web server path under which to serve') + parser.add_argument( + '--debug', action='store_true', default=False, + help='enable debugging and run server *insecurely*') + parser.add_argument( + '--no-daemon', action='store_true', default=False, + help='do not start as a daemon') - args=parser.parse_args() - set_config(args, "pidfile", "plinth.pid") - set_config(args, "server_dir", "/") - set_config(args, "debug", False) - - return cfg - -def set_config(args, element, default): - """Sets *cfg* elements based on *args* values, or uses a reasonable default. - - - If values are passed in from *args*, use those. - - If values are read from the config file, use those. - - If no values have been given, use default. - - """ - try: - # cfg.(element) = args.(element) - setattr(cfg, element, getattr(args, element)) - except AttributeError: - # if it fails, we didn't receive that argument. - try: - # if not cfg.(element): cfg.(element) = default - if not getattr(cfg, element): - setattr(cfg, element, default) - except AttributeError: - # it wasn't in the config file, but set the default anyway. - setattr(cfg, element, default) + args = parser.parse_args() + cfg.pidfile = args.pidfile + cfg.server_dir = args.server_dir + cfg.debug = args.debug + cfg.no_daemon = args.no_daemon def setup_logging(): @@ -71,21 +56,16 @@ def setup_logging(): cfg.log = Logger() -def setup_configuration(): - cfg = parse_arguments() - - try: - if cfg.pidfile: - from cherrypy.process.plugins import PIDFile - PIDFile(cherrypy.engine, cfg.pidfile).subscribe() - except AttributeError: - pass - - os.chdir(cfg.python_root) - - def setup_server(): """Setup CherryPy server""" + # Set the PID file path + try: + if cfg.pidfile: + from cherrypy.process.plugins import PIDFile + PIDFile(cherrypy.engine, cfg.pidfile).subscribe() + except AttributeError: + pass + # Add an extra server server = _cpserver.Server() server.socket_host = '127.0.0.1' @@ -107,6 +87,9 @@ def setup_server(): 'tools.staticdir.dir': '.'}} cherrypy.tree.mount(None, cfg.server_dir + '/static', config) + if not cfg.no_daemon: + Daemonizer(cherrypy.engine).subscribe() + cherrypy.engine.signal_handler.subscribe() @@ -141,7 +124,7 @@ def configure_django(): template_directories = module_loader.get_template_directories() sessions_directory = os.path.join(cfg.data_dir, 'sessions') django.conf.settings.configure( - DEBUG=False, + DEBUG=cfg.debug, ALLOWED_HOSTS=['127.0.0.1', 'localhost'], TEMPLATE_DIRS=template_directories, INSTALLED_APPS=['bootstrapform'], @@ -154,11 +137,14 @@ def configure_django(): def main(): """Intialize and start the application""" + parse_arguments() + setup_logging() + logger.init() service.init() - setup_configuration() + os.chdir(cfg.python_root) configure_django()