From 325db909187e75966ea70388c0621409e4ae93e6 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 27 Aug 2015 11:43:17 +0200 Subject: [PATCH] set "accepted formats" for metadata in config.py For a bit repo like f-droid.org, it makes sense to standardize on a single format for metadata files. This adds support for enforcing a single data format, or a reduced set of data formats. So f-droid.org would run like this if it changed to YAML: accepted_formats = ['txt', 'yaml'] Then once everything was converted to YAML, it could look like this: accepted_formats = ['yaml'] --- examples/config.py | 5 +++++ fdroidserver/common.py | 1 + fdroidserver/metadata.py | 45 +++++++++++++++++++++++++--------------- tests/metadata.TestCase | 5 +++-- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/examples/config.py b/examples/config.py index 6026e5b4..427342be 100644 --- a/examples/config.py +++ b/examples/config.py @@ -213,6 +213,11 @@ carbon_port = 2003 # --server option on dedicated secure build server hosts. build_server_always = False +# By default, fdroid will use YAML and the custom .txt metadata formats. It +# is also possible to have metadata in JSON and XML. You can enable your +# preferred formats by setting them in a list: +# accepted_formats = ['json', 'txt', 'xml', 'yaml'] + # Limit in number of characters that fields can take up # Only the fields listed here are supported, defaults shown char_limits = { diff --git a/fdroidserver/common.py b/fdroidserver/common.py index d3c7f176..0bc1eb5c 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -59,6 +59,7 @@ default_config = { 'ant': "ant", 'mvn3': "mvn", 'gradle': 'gradle', + 'accepted_formats': ['txt', 'yaml'], 'sync_from_local_copy_dir': False, 'per_app_repos': False, 'make_current_version_link': True, diff --git a/fdroidserver/metadata.py b/fdroidserver/metadata.py index a333a512..0ef93ef7 100644 --- a/fdroidserver/metadata.py +++ b/fdroidserver/metadata.py @@ -499,23 +499,11 @@ def read_metadata(xref=True): # exception. So the original .txt format is parsed first, at least until # newer formats stabilize. - for metadatapath in sorted(glob.glob(os.path.join('metadata', '*.txt'))): - appid, appinfo = parse_txt_metadata(apps, metadatapath) - check_metadata(appinfo) - apps[appid] = appinfo - - for metadatapath in sorted(glob.glob(os.path.join('metadata', '*.json'))): - appid, appinfo = parse_json_metadata(apps, metadatapath) - check_metadata(appinfo) - apps[appid] = appinfo - - for metadatapath in sorted(glob.glob(os.path.join('metadata', '*.xml'))): - appid, appinfo = parse_xml_metadata(apps, metadatapath) - check_metadata(appinfo) - apps[appid] = appinfo - - for metadatapath in sorted(glob.glob(os.path.join('metadata', '*.yaml'))): - appid, appinfo = parse_yaml_metadata(apps, metadatapath) + for metadatapath in sorted(glob.glob(os.path.join('metadata', '*.txt')) + + glob.glob(os.path.join('metadata', '*.json')) + + glob.glob(os.path.join('metadata', '*.xml')) + + glob.glob(os.path.join('metadata', '*.yaml'))): + appid, appinfo = parse_metadata(apps, metadatapath) check_metadata(appinfo) apps[appid] = appinfo @@ -731,6 +719,29 @@ def _decode_dict(data): return rv +def parse_metadata(apps, metadatapath): + root, ext = os.path.splitext(metadatapath) + metadataformat = ext[1:] + accepted = common.config['accepted_formats'] + if metadataformat not in accepted: + logging.critical('"' + metadatapath + + '" is not in an accepted format, ' + + 'convert to: ' + ', '.join(accepted)) + sys.exit(1) + + if metadataformat == 'txt': + return parse_txt_metadata(apps, metadatapath) + elif metadataformat == 'json': + return parse_json_metadata(apps, metadatapath) + elif metadataformat == 'xml': + return parse_xml_metadata(apps, metadatapath) + elif metadataformat == 'yaml': + return parse_yaml_metadata(apps, metadatapath) + else: + logging.critical('Unknown metadata format: ' + metadatapath) + sys.exit(1) + + def parse_json_metadata(apps, metadatapath): appid, thisinfo = get_default_app_info_list(apps, metadatapath) diff --git a/tests/metadata.TestCase b/tests/metadata.TestCase index 8d20ffef..69f4c6ed 100755 --- a/tests/metadata.TestCase +++ b/tests/metadata.TestCase @@ -29,11 +29,12 @@ class MetadataTest(unittest.TestCase): self.maxDiff = None - # these only need to be set to prevent code running on None. The - # values are not used in metadata.py + # these need to be set to prevent code running on None, only + # 'accepted_formats' is actually used in metadata.py config = dict() config['sdk_path'] = '/opt/android-sdk' config['ndk_paths'] = dict() + config['accepted_formats'] = ['json', 'txt', 'xml', 'yaml'] fdroidserver.common.config = config apps = fdroidserver.metadata.read_metadata(xref=True) -- 2.30.2