From 0d00705aed5c77cda8d807266675d0737a0d38eb Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 25 Nov 2016 15:20:19 +0100 Subject: [PATCH] use pyyaml for rewriting .yml metadata files This replaces the broken, custom code with the standard YAML lib. In rewritemeta, do not call app.metadatapath since it will be deleted when the dict is cleaned up for outputing. metadatapath is only used internally and should not be written out. closes #169 refs #290 --- fdroidserver/metadata.py | 88 ++++++++++--------------------------- fdroidserver/rewritemeta.py | 20 +++++++-- 2 files changed, 38 insertions(+), 70 deletions(-) diff --git a/fdroidserver/metadata.py b/fdroidserver/metadata.py index fe7e07f1..360ffc82 100644 --- a/fdroidserver/metadata.py +++ b/fdroidserver/metadata.py @@ -239,8 +239,9 @@ build_flags_order = [ 'novcheck', ] - -build_flags = set(build_flags_order + ['versionName', 'versionCode']) +# old .txt format has version name/code inline in the 'Build:' line +# but YAML and JSON have a explicit key for them +build_flags = ['versionName', 'versionCode'] + build_flags_order class Build(dict): @@ -959,6 +960,25 @@ def parse_yaml_metadata(mf, app): return app +def write_yaml(mf, app): + + def _class_as_dict_representer(dumper, data): + '''Creates a YAML representation of a App/Build instance''' + return dumper.represent_dict(data) + + empty_keys = [k for k, v in app.items() if not v] + for k in empty_keys: + del app[k] + + for k in ['added', 'lastUpdated', 'id', 'metadatapath']: + if k in app: + del app[k] + + yaml.add_representer(fdroidserver.metadata.App, _class_as_dict_representer) + yaml.add_representer(fdroidserver.metadata.Build, _class_as_dict_representer) + yaml.dump(app, mf, default_flow_style=False) + + build_line_sep = re.compile(r'(? 0: - cmds[-1] = cmds[-1][:-len('&& \\')] - w_field(f, cmds, prefix, 'multiline') - return - else: - v = ' ' + escape(v) + '\n' - - mf.write(prefix) - mf.write(f) - mf.write(":") - mf.write(v) - - global first_build - first_build = True - - def w_build(build): - global first_build - if first_build: - mf.write("builds:\n") - first_build = False - - w_field('versionName', build.versionName, ' - ', TYPE_STRING) - w_field('versionCode', build.versionCode, ' ', TYPE_STRING) - for f in build_flags_order: - v = build.get(f) - if not v: - continue - - w_field(f, v, ' ', flagtype(f)) - - write_plaintext_metadata(mf, app, w_comment, w_field, w_build) - - def write_metadata(metadatapath, app): _, ext = fdroidserver.common.get_extension(metadatapath) accepted = fdroidserver.common.config['accepted_formats'] diff --git a/fdroidserver/rewritemeta.py b/fdroidserver/rewritemeta.py index f6e2910f..2d2d1803 100644 --- a/fdroidserver/rewritemeta.py +++ b/fdroidserver/rewritemeta.py @@ -72,9 +72,10 @@ def main(): parser.error("Unsupported metadata format, use: --to [" + ' '.join(supported) + "]") for appid, app in apps.items(): - base, ext = common.get_extension(app.metadatapath) + path = app.metadatapath + base, ext = common.get_extension(path) if not options.to and ext not in supported: - logging.info("Ignoring %s file at '%s'" % (ext, app.metadatapath)) + logging.info("Ignoring %s file at '%s'" % (ext, path)) continue to_ext = ext @@ -83,13 +84,24 @@ def main(): if options.list: if not proper_format(app): - print(app.metadatapath) + print(path) continue + newbuilds = [] + for build in app.builds: + new = metadata.Build() + for k in metadata.build_flags: + v = build[k] + if v is None or v is False or v == [] or v == '': + continue + new[k] = v + newbuilds.append(new) + app.builds = newbuilds + metadata.write_metadata(base + '.' + to_ext, app) if ext != to_ext: - os.remove(app.metadatapath) + os.remove(path) logging.debug("Finished.")