From 49362b5fd1eb2c91a6e12a744ff3fa2dbd1c55fc Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 24 Apr 2023 17:34:47 +0200 Subject: [PATCH] move load_locale() and file_entry() to be accessible by all modules * load_locale -> common.load_localized_config() since common handles config * file_entry -> metadata.file_entry() since metadata handles data format --- fdroidserver/common.py | 36 ++++++++++++++++++++++++++ fdroidserver/index.py | 58 +++++++++++------------------------------- fdroidserver/update.py | 6 ++--- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 550e7b5a..d704212e 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -487,6 +487,42 @@ def read_config(opts=None): return config +def file_entry(filename, hash_value=None): + meta = {} + meta["name"] = "/" + filename.split("/", 1)[1] + meta["sha256"] = hash_value or common.sha256sum(filename) + meta["size"] = os.stat(filename).st_size + return meta + + +def load_localized_config(name, repodir): + lst = {} + for f in Path().glob("config/**/{name}.yml".format(name=name)): + locale = f.parts[1] + if len(f.parts) == 2: + locale = "en-US" + with open(f, encoding="utf-8") as fp: + elem = yaml.safe_load(fp) + for akey, avalue in elem.items(): + if akey not in lst: + lst[akey] = {} + for key, value in avalue.items(): + if key not in lst[akey]: + lst[akey][key] = {} + if key == "icon": + shutil.copy( + os.path.join("config", value), + os.path.join(repodir, "icons") + ) + lst[akey][key][locale] = file_entry( + os.path.join(repodir, "icons", value) + ) + else: + lst[akey][key][locale] = value + + return lst + + def parse_human_readable_size(size): units = { 'b': 1, diff --git a/fdroidserver/index.py b/fdroidserver/index.py index 162472ed..fb080550 100644 --- a/fdroidserver/index.py +++ b/fdroidserver/index.py @@ -29,7 +29,6 @@ import re import shutil import tempfile import urllib.parse -import yaml import zipfile import calendar import qrcode @@ -472,37 +471,6 @@ def dict_diff(source, target): return result -def file_entry(filename, hash_value=None): - meta = {} - meta["name"] = "/" + filename.split("/", 1)[1] - meta["sha256"] = hash_value or common.sha256sum(filename) - meta["size"] = os.stat(filename).st_size - return meta - - -def load_locale(name, repodir): - lst = {} - for yml in Path().glob("config/**/{name}.yml".format(name=name)): - locale = yml.parts[1] - if len(yml.parts) == 2: - locale = "en-US" - with open(yml, encoding="utf-8") as fp: - elem = yaml.safe_load(fp) - for akey, avalue in elem.items(): - if akey not in lst: - lst[akey] = {} - for key, value in avalue.items(): - if key not in lst[akey]: - lst[akey][key] = {} - if key == "icon": - shutil.copy(os.path.join("config", value), os.path.join(repodir, "icons")) - lst[akey][key][locale] = file_entry(os.path.join(repodir, "icons", value)) - else: - lst[akey][key][locale] = value - - return lst - - def convert_datetime(obj): if isinstance(obj, datetime): # Java prefers milliseconds @@ -568,7 +536,9 @@ def package_metadata(app, repodir): # TODO handle different resolutions if app.get("icon"): - meta["icon"] = {"en-US": file_entry(os.path.join(repodir, "icons", app["icon"]))} + meta["icon"] = { + "en-US": common.file_entry(os.path.join(repodir, "icons", app["icon"])) + } if "iconv2" in app: meta["icon"] = app["iconv2"] @@ -594,16 +564,16 @@ def convert_version(version, app, repodir): ver["file"]["ipfsCIDv1"] = ipfsCIDv1 if "srcname" in version: - ver["src"] = file_entry(os.path.join(repodir, version["srcname"])) + ver["src"] = common.file_entry(os.path.join(repodir, version["srcname"])) if "obbMainFile" in version: - ver["obbMainFile"] = file_entry( + ver["obbMainFile"] = common.file_entry( os.path.join(repodir, version["obbMainFile"]), version["obbMainFileSha256"], ) if "obbPatchFile" in version: - ver["obbPatchFile"] = file_entry( + ver["obbPatchFile"] = common.file_entry( os.path.join(repodir, version["obbPatchFile"]), version["obbPatchFileSha256"], ) @@ -686,9 +656,11 @@ def v2_repo(repodict, repodir, archive): repo["name"] = {"en-US": repodict["name"]} repo["description"] = {"en-US": repodict["description"]} - repo["icon"] = {"en-US": file_entry("{}/icons/{}".format(repodir, repodict["icon"]))} + repo["icon"] = { + "en-US": common.file_entry("{}/icons/{}".format(repodir, repodict["icon"])) + } - config = load_locale("config", repodir) + config = common.load_localized_config("config", repodir) if config: repo["name"] = config["archive" if archive else "repo"]["name"] repo["description"] = config["archive" if archive else "repo"]["description"] @@ -702,15 +674,15 @@ def v2_repo(repodict, repodir, archive): repo["timestamp"] = repodict["timestamp"] - antiFeatures = load_locale("antiFeatures", repodir) + antiFeatures = common.load_localized_config("antiFeatures", repodir) if antiFeatures: repo["antiFeatures"] = antiFeatures - categories = load_locale("categories", repodir) + categories = common.load_localized_config("categories", repodir) if categories: repo["categories"] = categories - channels = load_locale("channels", repodir) + channels = common.load_localized_config("channels", repodir) if channels: repo["releaseChannels"] = channels @@ -792,7 +764,7 @@ def make_v2(apps, packages, repodir, repodict, requestsdict, fdroid_signing_key_ else: json.dump(output, fp, default=_index_encoder_default, ensure_ascii=False) - entry["index"] = file_entry(index_file) + entry["index"] = common.file_entry(index_file) entry["index"]["numPackages"] = len(output.get("packages", [])) indexes = sorted(Path().glob("tmp/{}*.json".format(repodir)), key=lambda x: x.name) @@ -819,7 +791,7 @@ def make_v2(apps, packages, repodir, repodict, requestsdict, fdroid_signing_key_ else: json.dump(diff, fp, default=_index_encoder_default, ensure_ascii=False) - entry["diffs"][old["repo"]["timestamp"]] = file_entry(diff_file) + entry["diffs"][old["repo"]["timestamp"]] = common.file_entry(diff_file) entry["diffs"][old["repo"]["timestamp"]]["numPackages"] = len(diff.get("packages", [])) json_name = "entry.json" diff --git a/fdroidserver/update.py b/fdroidserver/update.py index b9490a53..aec69237 100644 --- a/fdroidserver/update.py +++ b/fdroidserver/update.py @@ -1037,7 +1037,7 @@ def insert_localized_app_metadata(apps): base = "iconv2" if base not in apps[packageName] or not isinstance(apps[packageName][base], collections.OrderedDict): apps[packageName][base] = collections.OrderedDict() - apps[packageName][base][locale] = index.file_entry(dst) + apps[packageName][base][locale] = common.file_entry(dst) for d in dirs: if d in SCREENSHOT_DIRS: if locale == 'images': @@ -1090,7 +1090,7 @@ def insert_localized_app_metadata(apps): base = "iconv2" if base not in apps[packageName] or not isinstance(apps[packageName][base], collections.OrderedDict): apps[packageName][base] = collections.OrderedDict() - apps[packageName][base][locale] = index.file_entry(index_file) + apps[packageName][base][locale] = common.file_entry(index_file) elif screenshotdir in SCREENSHOT_DIRS: # there can any number of these per locale logging.debug(_('adding to {name}: {path}').format(name=screenshotdir, path=f)) @@ -1105,7 +1105,7 @@ def insert_localized_app_metadata(apps): apps[packageName]["screenshots"][newKey] = collections.OrderedDict() if locale not in apps[packageName]["screenshots"][newKey]: apps[packageName]["screenshots"][newKey][locale] = [] - apps[packageName]["screenshots"][newKey][locale].append(index.file_entry(f)) + apps[packageName]["screenshots"][newKey][locale].append(common.file_entry(f)) else: logging.warning(_('Unsupported graphics file found: {path}').format(path=f))