use common var for 'config.yml', standarize on UTF-8

This makes it easy to track all the places that use config.yml, and
hopefully makes things feel cleaner.  This also standardizes all places
where config.yml is written out to use UTF-8 as the file encoding.

This also includes a lot of black code format fixes.
This commit is contained in:
Hans-Christoph Steiner 2025-02-26 18:16:06 +01:00
parent 1f96a84f9a
commit 3cc6c09ffc
14 changed files with 189 additions and 173 deletions

View file

@ -126,6 +126,7 @@ CONFIG_NAMES = (
RELEASECHANNELS_CONFIG_NAME,
)
CONFIG_FILE = 'config.yml'
config = None
options = None
@ -525,7 +526,7 @@ def config_type_check(path, data):
def read_config():
"""Read the repository config.
The config is read from config_file, which is in the current
The config is read from config.yml, which is in the current
directory when any of the repo management commands are used. If
there is a local metadata file in the git repo, then the config is
not required, just use defaults.
@ -546,21 +547,20 @@ def read_config():
return config
config = {}
config_file = 'config.yml'
old_config_file = 'config.py'
if os.path.exists(config_file):
logging.debug(_("Reading '{config_file}'").format(config_file=config_file))
with open(config_file, encoding='utf-8') as fp:
if os.path.exists(CONFIG_FILE):
logging.debug(_("Reading '{config_file}'").format(config_file=CONFIG_FILE))
with open(CONFIG_FILE, encoding='utf-8') as fp:
config = yaml.safe_load(fp)
if not config:
config = {}
config_type_check(config_file, config)
config_type_check(CONFIG_FILE, config)
old_config_file = 'config.py'
if os.path.exists(old_config_file):
logging.warning(
_("""Ignoring deprecated {oldfile}, use {newfile}!""").format(
oldfile=old_config_file, newfile=config_file
oldfile=old_config_file, newfile=CONFIG_FILE
)
)
@ -577,14 +577,13 @@ def read_config():
'-providerArg', 'opensc-fdroid.cfg']
if any(k in config for k in ["keystore", "keystorepass", "keypass"]):
if os.path.exists(config_file):
f = config_file
elif os.path.exists(old_config_file):
f = old_config_file
st = os.stat(f)
st = os.stat(CONFIG_FILE)
if st.st_mode & stat.S_IRWXG or st.st_mode & stat.S_IRWXO:
logging.warning(_("unsafe permissions on '{config_file}' (should be 0600)!")
.format(config_file=f))
logging.warning(
_("unsafe permissions on '{config_file}' (should be 0600)!").format(
config_file=CONFIG_FILE
)
)
fill_config_defaults(config)
@ -4191,9 +4190,17 @@ def load_stats_fdroid_signing_key_fingerprints():
return json.loads(str(f.read('publishsigkeys.json'), 'utf-8'))
def write_to_config(thisconfig, key, value=None, config_file=None):
def write_config_file(config):
"""Write the provided string to config.yml with the right path and encoding."""
Path(CONFIG_FILE).write_text(config, encoding='utf-8')
def write_to_config(thisconfig, key, value=None):
"""Write a key/value to the local config.yml.
The config.yml is defined as YAML 1.2 in UTF-8 encoding on all
platforms.
NOTE: only supports writing string variables.
Parameters
@ -4205,21 +4212,18 @@ def write_to_config(thisconfig, key, value=None, config_file=None):
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]
if config_file:
cfg = config_file
else:
cfg = 'config.yml'
# load config file, create one if it doesn't exist
if not os.path.exists(cfg):
open(cfg, 'a').close()
logging.info("Creating empty " + cfg)
with open(cfg, 'r') as f:
lines = f.readlines()
if not os.path.exists(CONFIG_FILE):
write_config_file('')
logging.info(_("Creating empty {config_file}").format(config_file=CONFIG_FILE))
with open(CONFIG_FILE) as fp:
lines = fp.readlines()
# make sure the file ends with a carraige return
if len(lines) > 0:
@ -4233,7 +4237,7 @@ def write_to_config(thisconfig, key, value=None, config_file=None):
# second instance of this line for this key in the document.
didRepl = False
# edit config file
with open(cfg, 'w') as f:
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
for line in lines:
if pattern.match(line):
if not didRepl:

View file

@ -38,13 +38,13 @@ def disable_in_config(key, value):
"""Write a key/value to the local config.yml, then comment it out."""
import yaml
with open('config.yml') as f:
data = f.read()
with open(common.CONFIG_FILE) as fp:
data = fp.read()
pattern = r'\n[\s#]*' + key + r':.*'
repl = '\n#' + yaml.dump({key: value}, default_flow_style=False)
data = re.sub(pattern, repl, data)
with open('config.yml', 'w') as f:
f.writelines(data)
with open(common.CONFIG_FILE, 'w') as fp:
fp.writelines(data)
def main():
@ -138,24 +138,24 @@ def main():
_("Android SDK not found at {path}!").format(path=test_config['sdk_path'])
)
if not os.path.exists('config.yml'):
if not os.path.exists(common.CONFIG_FILE):
# 'metadata' and 'tmp' are created in fdroid
if not os.path.exists('repo'):
os.mkdir('repo')
example_config_yml = os.path.join(examplesdir, 'config.yml')
example_config_yml = os.path.join(examplesdir, common.CONFIG_FILE)
if os.path.exists(example_config_yml):
shutil.copyfile(example_config_yml, 'config.yml')
shutil.copyfile(example_config_yml, common.CONFIG_FILE)
else:
from pkg_resources import get_distribution
versionstr = get_distribution('fdroidserver').version
if not versionstr:
versionstr = 'master'
with open('config.yml', 'w') as fp:
with open(common.CONFIG_FILE, 'w') as fp:
fp.write('# see https://gitlab.com/fdroid/fdroidserver/blob/')
fp.write(versionstr)
fp.write('/examples/config.yml\n')
os.chmod('config.yml', 0o0600)
fp.write(f'/examples/{common.CONFIG_FILE}\n')
os.chmod(common.CONFIG_FILE, 0o0600)
# If android_home is None, test_config['sdk_path'] will be used and
# "$ANDROID_HOME" may be used if the env var is set up correctly.
# If android_home is not None, the path given from the command line

View file

@ -976,7 +976,7 @@ def main():
paths = list()
for arg in options.appid:
if (
arg == 'config.yml'
arg == common.CONFIG_FILE
or Path(arg).parent.name == 'config'
or Path(arg).parent.parent.name == 'config' # localized
):

View file

@ -423,9 +423,9 @@ Last updated: {date}'''.format(repo_git_base=repo_git_base,
'keydname': DISTINGUISHED_NAME,
'make_current_version_link': False,
}
with open('config.yml', 'w') as fp:
with open(common.CONFIG_FILE, 'w', encoding='utf-8') as fp:
yaml.dump(config, fp, default_flow_style=False)
os.chmod('config.yml', 0o600)
os.chmod(common.CONFIG_FILE, 0o600)
config = common.read_config()
common.assert_config_keystore(config)