Add ./run --develop option to use relative config/file paths

Signed-off-by: Michael Pimmer <info@fonfon.at>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Michael Pimmer 2018-06-12 14:18:44 +00:00 committed by James Valleroy
parent 7a63504c62
commit cab5b694cf
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 41 additions and 26 deletions

View File

@ -34,11 +34,10 @@ import cherrypy
from plinth import cfg, menu, module_loader, service, setup
axes.default_app_config = "plinth.axes_app_config.AppConfig"
precedence_commandline_arguments = ["server_dir", "debug", "develop"]
logger = logging.getLogger(__name__)
arguments = None
def parse_arguments():
"""Parse command line arguments"""
@ -46,10 +45,13 @@ def parse_arguments():
description='Core functionality and web interface for FreedomBox',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# TODO: server_dir is actually a url prefix; use a better variable name
parser.add_argument('--server_dir', default=cfg.server_dir,
parser.add_argument('--server_dir', default=None,
help='web server path under which to serve')
parser.add_argument('--debug', action='store_true', default=cfg.debug,
parser.add_argument('--debug', action='store_true', default=None,
help='enable debugging and run server *insecurely*')
parser.add_argument('--develop', action='store_true', default=None,
help=('use files from current source code folder '
'instead of system-installed plinth files'))
parser.add_argument(
'--setup', default=False, nargs='*',
help='run setup tasks on all essential modules and exit')
@ -63,10 +65,7 @@ def parse_arguments():
parser.add_argument('--list-modules', default=False, nargs='*',
help='list modules')
global arguments
arguments = parser.parse_args()
cfg.server_dir = arguments.server_dir
cfg.debug = arguments.debug
return parser.parse_args()
def setup_logging():
@ -377,11 +376,35 @@ def run_diagnostics_and_exit():
sys.exit(error_code)
def adapt_config(arguments):
"""Give commandline arguments precedence over plinth.config entries"""
for argument_name in precedence_commandline_arguments:
argument_value = getattr(arguments, argument_name)
if argument_value is not None:
setattr(cfg, argument_name, argument_value)
def get_relative_config_paths():
"""Get config paths of the current source code folder"""
root_directory = os.path.dirname(os.path.realpath(__file__))
root_directory = os.path.join(root_directory, '..')
root_directory = os.path.realpath(root_directory)
file_path = os.path.join(root_directory, 'plinth.config')
return {
'file_path': file_path,
'root_directory': root_directory,
}
def main():
"""Intialize and start the application"""
cfg.read()
arguments = parse_arguments()
config_paths = {}
if arguments.develop:
config_paths = get_relative_config_paths()
parse_arguments()
cfg.read(**config_paths)
adapt_config(arguments)
setup_logging()

View File

@ -45,24 +45,16 @@ DEFAULT_CONFIG_FILE = '/etc/plinth/plinth.config'
DEFAULT_ROOT = '/'
def get_config_file():
"""Return the configuration file to read."""
if os.path.isfile(DEFAULT_CONFIG_FILE):
def read(file_path=None, root_directory=None):
"""
Read configuration.
- file_path: path of plinth.config file
- root_directory: path of plinth root folder
"""
if not file_path and not root_directory:
root_directory = DEFAULT_ROOT
file_path = DEFAULT_CONFIG_FILE
else:
root_directory = os.path.dirname(os.path.realpath(__file__))
root_directory = os.path.join(root_directory, '..')
root_directory = os.path.realpath(root_directory)
file_path = os.path.join(root_directory, 'plinth.config')
return file_path, root_directory
def read(file_path=None, root_directory=None):
"""Read configuration."""
if not file_path and not root_directory:
file_path, root_directory = get_config_file()
if not os.path.isfile(file_path):
raise FileNotFoundError('No plinth.config file could be found.')