support both config.py and config.yml in common.write_to_config()

This commit is contained in:
Hans-Christoph Steiner 2020-10-22 23:00:58 +02:00
parent 2d115135f7
commit 4bbbf35511
2 changed files with 50 additions and 7 deletions

View File

@ -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.

View File

@ -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__":