From 34d3a0841606b1b1e0bae95bdbd3074f9435f2d0 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 17 Jan 2020 14:01:52 -0800 Subject: [PATCH] Move config dir init logic to config.init --- amanuensis/cli/server.py | 52 ++---------------------------- amanuensis/config/init.py | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 50 deletions(-) diff --git a/amanuensis/cli/server.py b/amanuensis/cli/server.py index 4f9b80c..3dae84e 100644 --- a/amanuensis/cli/server.py +++ b/amanuensis/cli/server.py @@ -9,57 +9,9 @@ def command_init(args): pidfile, a lexicon config directory, and a user config directory. """ - from collections import OrderedDict - import fcntl - import json - import os + import config.init - import resources - - cfd = args.config_dir - # Create the directory if it doesn't exist. - if not os.path.isdir(cfd): - os.mkdir(cfd) - # The directory should be empty if we're not updating an existing one. - if len(os.listdir(cfd)) > 0 and not args.update: - print("Directory {} is not empty".format(cfd)) - return -1 - - # Update or create global config. - def_cfg = resources.get_stream("global.json") - if args.update and os.path.isfile(os.path.join(cfd, "config.json")): - with open(os.path.join(cfd, "config.json"), 'r+', encoding='utf8') as cfg_file: - fcntl.lockf(cfg_file, fcntl.LOCK_EX) - old_cfg = json.load(cfg_file, object_pairs_hook=OrderedDict) - new_cfg = json.load(def_cfg, object_pairs_hook=OrderedDict) - merged = {} - for key in new_cfg: - merged[key] = old_cfg[key] if key in old_cfg else new_cfg[key] - if key not in old_cfg: - print("Added key '{}' to config".format(key)) - for key in old_cfg: - if key not in new_cfg: - print("Config contains unknown key '{}'".format(key)) - merged[key] = old_cfg[key] - cfg_file.seek(0) - json.dump(merged, cfg_file, allow_nan=False, indent='\t') - cfg_file.truncate() - fcntl.lockf(cfg_file, fcntl.LOCK_UN) - else: - with open(os.path.join(cfd, "config.json"), 'wb') as f: - f.write(def_cfg.read()) - # Ensure pidfile exists. - if not os.path.isfile(os.path.join(cfd, "pid")): - with open(os.path.join(cfd, "pid"), 'w') as f: - f.write(str(os.getpid())) - # Ensure subdirs exist. - if not os.path.isdir(os.path.join(cfd, "lexicon")): - os.mkdir(os.path.join(cfd, "lexicon")) - if not os.path.isdir(os.path.join(cfd, "user")): - os.mkdir(os.path.join(cfd, "user")) - if not os.path.isfile(os.path.join(cfd, 'user', 'index.json')): - with open(os.path.join(cfd, 'user', 'index.json'), 'w') as f: - json.dump({}, f) + config.init.create_config_dir(args.config_dir, args.update) @no_argument def command_generate_secret(args): diff --git a/amanuensis/config/init.py b/amanuensis/config/init.py index 83fcf4f..bb2f9ed 100644 --- a/amanuensis/config/init.py +++ b/amanuensis/config/init.py @@ -6,9 +6,76 @@ import os # Module imports from errors import MissingConfigError, MalformedConfigError +import config from config.loader import json_ro import resources +def create_config_dir(config_dir, update=False): + """ + Create or update a config directory + """ + from collections import OrderedDict + import fcntl + + path = config.prepend + + # Create the directory if it doesn't exist. + if not os.path.isdir(config_dir): + os.mkdir(config_dir) + + # Initialize the config dir without verification + config.CONFIG_DIR = config_dir + + # The directory should be empty if we're not updating an existing one. + if len(os.listdir(config_dir)) > 0 and not update: + print("Directory {} is not empty".format(config_dir)) + return -1 + + # Update or create global config. + def_cfg = resources.get_stream("global.json") + global_config_path = path("config.json") + if update and os.path.isfile(global_config_path): + # We need to write an entirely different ordereddict to the config + # file, so we mimic the config.loader functionality manually. + with open(global_config_path, 'r+', encoding='utf8') as cfg_file: + fcntl.lockf(cfg_file, fcntl.LOCK_EX) + old_cfg = json.load(cfg_file, object_pairs_hook=OrderedDict) + new_cfg = json.load(def_cfg, object_pairs_hook=OrderedDict) + merged = {} + for key in new_cfg: + merged[key] = old_cfg[key] if key in old_cfg else new_cfg[key] + if key not in old_cfg: + print("Added key '{}' to config".format(key)) + for key in old_cfg: + if key not in new_cfg: + print("Config contains unknown key '{}'".format(key)) + merged[key] = old_cfg[key] + cfg_file.seek(0) + json.dump(merged, cfg_file, allow_nan=False, indent='\t') + cfg_file.truncate() + fcntl.lockf(cfg_file, fcntl.LOCK_UN) + else: + with open(path("config.json"), 'wb') as f: + f.write(def_cfg.read()) + + # Ensure pidfile exists. + if not os.path.isfile(path("pid")): + with open(path("pid"), 'w') as f: + f.write(str(os.getpid())) + + # Ensure lexicon subdir exists. + if not os.path.isdir(path("lexicon")): + os.mkdir(path("lexicon")) + if not os.path.isfile(path("lexicon", "index.json")): + with open(path("lexicon", "index.json"), 'w') as f: + json.dump({}, f) + + # Ensure user subdir exists. + if not os.path.isdir(path("user")): + os.mkdir(path("user")) + if not os.path.isfile(path('user', 'index.json')): + with open(path('user', 'index.json'), 'w') as f: + json.dump({}, f) def verify_config_dir(config_dir): """