1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-11-18 20:50:10 +01:00

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
This commit is contained in:
Hans-Christoph Steiner 2023-04-24 17:34:47 +02:00
parent f9864dc3a2
commit 49362b5fd1
3 changed files with 54 additions and 46 deletions

View File

@ -487,6 +487,42 @@ def read_config(opts=None):
return config 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): def parse_human_readable_size(size):
units = { units = {
'b': 1, 'b': 1,

View File

@ -29,7 +29,6 @@ import re
import shutil import shutil
import tempfile import tempfile
import urllib.parse import urllib.parse
import yaml
import zipfile import zipfile
import calendar import calendar
import qrcode import qrcode
@ -472,37 +471,6 @@ def dict_diff(source, target):
return result 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): def convert_datetime(obj):
if isinstance(obj, datetime): if isinstance(obj, datetime):
# Java prefers milliseconds # Java prefers milliseconds
@ -568,7 +536,9 @@ def package_metadata(app, repodir):
# TODO handle different resolutions # TODO handle different resolutions
if app.get("icon"): 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: if "iconv2" in app:
meta["icon"] = app["iconv2"] meta["icon"] = app["iconv2"]
@ -594,16 +564,16 @@ def convert_version(version, app, repodir):
ver["file"]["ipfsCIDv1"] = ipfsCIDv1 ver["file"]["ipfsCIDv1"] = ipfsCIDv1
if "srcname" in version: 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: if "obbMainFile" in version:
ver["obbMainFile"] = file_entry( ver["obbMainFile"] = common.file_entry(
os.path.join(repodir, version["obbMainFile"]), os.path.join(repodir, version["obbMainFile"]),
version["obbMainFileSha256"], version["obbMainFileSha256"],
) )
if "obbPatchFile" in version: if "obbPatchFile" in version:
ver["obbPatchFile"] = file_entry( ver["obbPatchFile"] = common.file_entry(
os.path.join(repodir, version["obbPatchFile"]), os.path.join(repodir, version["obbPatchFile"]),
version["obbPatchFileSha256"], version["obbPatchFileSha256"],
) )
@ -686,9 +656,11 @@ def v2_repo(repodict, repodir, archive):
repo["name"] = {"en-US": repodict["name"]} repo["name"] = {"en-US": repodict["name"]}
repo["description"] = {"en-US": repodict["description"]} 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: if config:
repo["name"] = config["archive" if archive else "repo"]["name"] repo["name"] = config["archive" if archive else "repo"]["name"]
repo["description"] = config["archive" if archive else "repo"]["description"] repo["description"] = config["archive" if archive else "repo"]["description"]
@ -702,15 +674,15 @@ def v2_repo(repodict, repodir, archive):
repo["timestamp"] = repodict["timestamp"] repo["timestamp"] = repodict["timestamp"]
antiFeatures = load_locale("antiFeatures", repodir) antiFeatures = common.load_localized_config("antiFeatures", repodir)
if antiFeatures: if antiFeatures:
repo["antiFeatures"] = antiFeatures repo["antiFeatures"] = antiFeatures
categories = load_locale("categories", repodir) categories = common.load_localized_config("categories", repodir)
if categories: if categories:
repo["categories"] = categories repo["categories"] = categories
channels = load_locale("channels", repodir) channels = common.load_localized_config("channels", repodir)
if channels: if channels:
repo["releaseChannels"] = channels repo["releaseChannels"] = channels
@ -792,7 +764,7 @@ def make_v2(apps, packages, repodir, repodict, requestsdict, fdroid_signing_key_
else: else:
json.dump(output, fp, default=_index_encoder_default, ensure_ascii=False) 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", [])) entry["index"]["numPackages"] = len(output.get("packages", []))
indexes = sorted(Path().glob("tmp/{}*.json".format(repodir)), key=lambda x: x.name) 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: else:
json.dump(diff, fp, default=_index_encoder_default, ensure_ascii=False) 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", [])) entry["diffs"][old["repo"]["timestamp"]]["numPackages"] = len(diff.get("packages", []))
json_name = "entry.json" json_name = "entry.json"

View File

@ -1037,7 +1037,7 @@ def insert_localized_app_metadata(apps):
base = "iconv2" base = "iconv2"
if base not in apps[packageName] or not isinstance(apps[packageName][base], collections.OrderedDict): if base not in apps[packageName] or not isinstance(apps[packageName][base], collections.OrderedDict):
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: for d in dirs:
if d in SCREENSHOT_DIRS: if d in SCREENSHOT_DIRS:
if locale == 'images': if locale == 'images':
@ -1090,7 +1090,7 @@ def insert_localized_app_metadata(apps):
base = "iconv2" base = "iconv2"
if base not in apps[packageName] or not isinstance(apps[packageName][base], collections.OrderedDict): if base not in apps[packageName] or not isinstance(apps[packageName][base], collections.OrderedDict):
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: elif screenshotdir in SCREENSHOT_DIRS:
# there can any number of these per locale # there can any number of these per locale
logging.debug(_('adding to {name}: {path}').format(name=screenshotdir, path=f)) 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() apps[packageName]["screenshots"][newKey] = collections.OrderedDict()
if locale not in apps[packageName]["screenshots"][newKey]: if locale not in apps[packageName]["screenshots"][newKey]:
apps[packageName]["screenshots"][newKey][locale] = [] 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: else:
logging.warning(_('Unsupported graphics file found: {path}').format(path=f)) logging.warning(_('Unsupported graphics file found: {path}').format(path=f))