mirror of
https://gitlab.com/fdroid/fdroidserver.git
synced 2024-10-06 11:00:13 +02:00
support both config.py and config.yml in common.write_to_config()
This commit is contained in:
parent
2d115135f7
commit
4bbbf35511
@ -3552,19 +3552,24 @@ def load_stats_fdroid_signing_key_fingerprints():
|
|||||||
|
|
||||||
|
|
||||||
def write_to_config(thisconfig, key, value=None, config_file=None):
|
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.
|
NOTE: only supports writing string variables.
|
||||||
|
|
||||||
:param thisconfig: config dictionary
|
: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
|
:param value: optional value to be written, instead of fetched
|
||||||
from 'thisconfig' dictionary.
|
from 'thisconfig' dictionary.
|
||||||
'''
|
'''
|
||||||
if value is None:
|
if value is None:
|
||||||
origkey = key + '_orig'
|
origkey = key + '_orig'
|
||||||
value = thisconfig[origkey] if origkey in thisconfig else thisconfig[key]
|
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
|
# load config file, create one if it doesn't exist
|
||||||
if not os.path.exists(cfg):
|
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
|
# regex for finding and replacing python string variable
|
||||||
# definitions/initializations
|
# definitions/initializations
|
||||||
|
if cfg.endswith('.py'):
|
||||||
pattern = re.compile(r'^[\s#]*' + key + r'\s*=\s*"[^"]*"')
|
pattern = re.compile(r'^[\s#]*' + key + r'\s*=\s*"[^"]*"')
|
||||||
repl = key + ' = "' + value + '"'
|
repl = key + ' = "' + value + '"'
|
||||||
pattern2 = re.compile(r'^[\s#]*' + key + r"\s*=\s*'[^']*'")
|
pattern2 = re.compile(r'^[\s#]*' + key + r"\s*=\s*'[^']*'")
|
||||||
repl2 = key + " = '" + value + "'"
|
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
|
# If we replaced this line once, we make sure won't be a
|
||||||
# second instance of this line for this key in the document.
|
# second instance of this line for this key in the document.
|
||||||
|
@ -1485,6 +1485,37 @@ class CommonTest(unittest.TestCase):
|
|||||||
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
||||||
self.assertEqual('yml', config.get('apksigner'))
|
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__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user