From 4bbbf3551153ba8a7d6a36ab24390c0d946b5734 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 22 Oct 2020 23:00:58 +0200 Subject: [PATCH] support both config.py and config.yml in common.write_to_config() --- fdroidserver/common.py | 26 +++++++++++++++++++------- tests/common.TestCase | 31 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 9736edf2..74d0b987 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -3552,19 +3552,24 @@ def load_stats_fdroid_signing_key_fingerprints(): def write_to_config(thisconfig, key, value=None, config_file=None): - '''write a key/value to the local config.py + '''write a key/value to the local config.yml or config.py NOTE: only supports writing string variables. :param thisconfig: config dictionary - :param key: variable name in config.py to be overwritten/added + :param key: variable name in config to be overwritten/added :param value: optional value to be written, instead of fetched from 'thisconfig' dictionary. ''' if value is None: origkey = key + '_orig' value = thisconfig[origkey] if origkey in thisconfig else thisconfig[key] - cfg = config_file if config_file else 'config.py' + if config_file: + cfg = config_file + elif os.path.exists('config.py') and not os.path.exists('config.yml'): + cfg = 'config.py' + else: + cfg = 'config.yml' # load config file, create one if it doesn't exist if not os.path.exists(cfg): @@ -3580,10 +3585,17 @@ def write_to_config(thisconfig, key, value=None, config_file=None): # regex for finding and replacing python string variable # definitions/initializations - pattern = re.compile(r'^[\s#]*' + key + r'\s*=\s*"[^"]*"') - repl = key + ' = "' + value + '"' - pattern2 = re.compile(r'^[\s#]*' + key + r"\s*=\s*'[^']*'") - repl2 = key + " = '" + value + "'" + if cfg.endswith('.py'): + pattern = re.compile(r'^[\s#]*' + key + r'\s*=\s*"[^"]*"') + repl = key + ' = "' + value + '"' + pattern2 = re.compile(r'^[\s#]*' + key + r"\s*=\s*'[^']*'") + repl2 = key + " = '" + value + "'" + else: + # assume .yml as default + pattern = re.compile(r'^[\s#]*' + key + r':.*') + repl = yaml.dump({key: value}, default_flow_style=False) + pattern2 = pattern + repl2 = repl # If we replaced this line once, we make sure won't be a # second instance of this line for this key in the document. diff --git a/tests/common.TestCase b/tests/common.TestCase index a12ec6a6..d8bc6c6b 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -1485,6 +1485,37 @@ class CommonTest(unittest.TestCase): config = fdroidserver.common.read_config(fdroidserver.common.options) self.assertEqual('yml', config.get('apksigner')) + def test_write_to_config_yml(self): + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) + os.chdir(testdir) + with open('config.yml', 'w') as fp: + fp.write('apksigner: yml') + self.assertTrue(os.path.exists(fp.name)) + self.assertFalse(os.path.exists('config.py')) + config = fdroidserver.common.read_config(fdroidserver.common.options) + self.assertFalse('keypass' in config) + self.assertEqual('yml', config.get('apksigner')) + fdroidserver.common.write_to_config(config, 'keypass', 'mysecretpassword') + with open(fp.name) as fp: + print(fp.read()) + fdroidserver.common.config = None + config = fdroidserver.common.read_config(fdroidserver.common.options) + self.assertEqual('mysecretpassword', config['keypass']) + + def test_write_to_config_py(self): + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) + os.chdir(testdir) + with open('config.py', 'w') as fp: + fp.write('apksigner = "py"') + self.assertTrue(os.path.exists(fp.name)) + self.assertFalse(os.path.exists('config.yml')) + config = fdroidserver.common.read_config(fdroidserver.common.options) + self.assertFalse('keypass' in config) + self.assertEqual('py', config.get('apksigner')) + fdroidserver.common.write_to_config(config, 'keypass', 'mysecretpassword') + fdroidserver.common.config = None + config = fdroidserver.common.read_config(fdroidserver.common.options) + self.assertEqual('mysecretpassword', config['keypass']) if __name__ == "__main__":