1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-09-19 19:40:17 +02:00

Simplify field/flag checking and bool flag usage

This commit is contained in:
Daniel Martí 2014-05-30 18:51:59 +02:00
parent 5933880a90
commit e93017569f

View File

@ -87,7 +87,8 @@ ordered_flags = [
# 'fields' - Metadata fields (Field:Value) of this type # 'fields' - Metadata fields (Field:Value) of this type
# 'attrs' - Build attributes (attr=value) of this type # 'attrs' - Build attributes (attr=value) of this type
# #
class FieldType(): class FieldValidator():
def __init__(self, name, matching, sep, fields, attrs): def __init__(self, name, matching, sep, fields, attrs):
self.name = name self.name = name
self.matching = matching self.matching = matching
@ -126,62 +127,62 @@ class FieldType():
# Generic value types # Generic value types
valuetypes = { valuetypes = {
'int': FieldType("Integer", FieldValidator("Integer",
r'^[1-9][0-9]*$', None, r'^[1-9][0-9]*$', None,
['FlattrID'], ['FlattrID'],
['vercode']), ['vercode']),
'http': FieldType("HTTP link", FieldValidator("HTTP link",
r'^http[s]?://', None, r'^http[s]?://', None,
["Web Site", "Source Code", "Issue Tracker", "Donate"], []), ["Web Site", "Source Code", "Issue Tracker", "Donate"], []),
'bitcoin': FieldType("Bitcoin address", FieldValidator("Bitcoin address",
r'^[a-zA-Z0-9]{27,34}$', None, r'^[a-zA-Z0-9]{27,34}$', None,
["Bitcoin"], ["Bitcoin"],
[]), []),
'litecoin': FieldType("Litecoin address", FieldValidator("Litecoin address",
r'^L[a-zA-Z0-9]{33}$', None, r'^L[a-zA-Z0-9]{33}$', None,
["Litecoin"], ["Litecoin"],
[]), []),
'dogecoin': FieldType("Dogecoin address", FieldValidator("Dogecoin address",
r'^D[a-zA-Z0-9]{33}$', None, r'^D[a-zA-Z0-9]{33}$', None,
["Dogecoin"], ["Dogecoin"],
[]), []),
'Bool': FieldType("Boolean", FieldValidator("Boolean",
['Yes', 'No'], None, ['Yes', 'No'], None,
["Requires Root"], ["Requires Root"],
[]), []),
'bool': FieldType("Boolean", FieldValidator("bool",
['yes', 'no'], None, ['yes', 'no'], None,
[], [],
['submodules', 'oldsdkloc', 'forceversion', 'forcevercode', ['submodules', 'oldsdkloc', 'forceversion', 'forcevercode',
'novcheck']), 'novcheck']),
'Repo Type': FieldType("Repo Type", FieldValidator("Repo Type",
['git', 'git-svn', 'svn', 'hg', 'bzr', 'srclib'], None, ['git', 'git-svn', 'svn', 'hg', 'bzr', 'srclib'], None,
["Repo Type"], ["Repo Type"],
[]), []),
'archive': FieldType("Archive Policy", FieldValidator("Archive Policy",
r'^[0-9]+ versions$', None, r'^[0-9]+ versions$', None,
["Archive Policy"], ["Archive Policy"],
[]), []),
'antifeatures': FieldType("Anti-Feature", FieldValidator("Anti-Feature",
["Ads", "Tracking", "NonFreeNet", "NonFreeDep", "NonFreeAdd", "UpstreamNonFree"], ',', ["Ads", "Tracking", "NonFreeNet", "NonFreeDep", "NonFreeAdd", "UpstreamNonFree"], ',',
["AntiFeatures"], ["AntiFeatures"],
[]), []),
'autoupdatemodes': FieldType("Auto Update Mode", FieldValidator("Auto Update Mode",
r"^(Version .+|None)$", None, r"^(Version .+|None)$", None,
["Auto Update Mode"], ["Auto Update Mode"],
[]), []),
'updatecheckmodes': FieldType("Update Check Mode", FieldValidator("Update Check Mode",
r"^(Tags|Tags .+|RepoManifest|RepoManifest/.+|RepoTrunk|HTTP|Static|None)$", None, r"^(Tags|Tags .+|RepoManifest|RepoManifest/.+|RepoTrunk|HTTP|Static|None)$", None,
["Update Check Mode"], ["Update Check Mode"],
[]) [])
@ -190,20 +191,14 @@ valuetypes = {
# Check an app's metadata information for integrity errors # Check an app's metadata information for integrity errors
def check_metadata(info): def check_metadata(info):
for k, t in valuetypes.iteritems(): for v in valuetypes:
for field in t.fields: for field in v.fields:
if field in info: if field in info:
t.check(info[field], info['id']) v.check(info[field], info['id'])
if k == 'Bool':
info[field] = info[field] == "Yes"
for build in info['builds']: for build in info['builds']:
for attr in t.attrs: for attr in v.attrs:
if attr in build: if attr in build:
t.check(build[attr], info['id']) v.check(build[attr], info['id'])
if k == 'bool':
build[attr] = build[attr] == "yes"
elif k == 'bool':
build[attr] = False
# Formatter for descriptions. Create an instance, and call parseline() with # Formatter for descriptions. Create an instance, and call parseline() with
@ -499,6 +494,9 @@ def flagtype(name):
return 'list' return 'list'
if name in ['init', 'prebuild', 'build']: if name in ['init', 'prebuild', 'build']:
return 'script' return 'script'
if name in ['submodules', 'oldsdkloc', 'forceversion', 'forcevercode',
'novcheck']:
return 'bool'
return 'string' return 'string'
@ -549,10 +547,15 @@ def parse_metadata(metafile):
if t == 'list': if t == 'list':
# Port legacy ';' separators # Port legacy ';' separators
thisbuild[pk] = [v.strip() for v in pv.replace(';', ',').split(',')] thisbuild[pk] = [v.strip() for v in pv.replace(';', ',').split(',')]
elif t == 'string': elif t == 'string' or t == 'script':
thisbuild[pk] = pv
elif t == 'script':
thisbuild[pk] = pv thisbuild[pk] = pv
elif t == 'bool':
value = pv == 'yes'
if value:
thisbuild[pk] = True
else:
logging.debug("...ignoring bool flag %s" % p)
else: else:
raise MetaDataException("Unrecognised build flag type '%s' at %s in %s" raise MetaDataException("Unrecognised build flag type '%s' at %s in %s"
% (t, p, linedesc)) % (t, p, linedesc))
@ -622,6 +625,16 @@ def parse_metadata(metafile):
curcomments = [] curcomments = []
curbuild = None curbuild = None
def fill_bool_defaults(build):
# TODO: quick fix to make bool flags default to False
# Should provide defaults for all flags instead of using
# build.get(flagname, default) each time
for f in ordered_flags:
if f in build:
continue
if flagtype(f) == 'bool':
build[f] = False
c = 0 c = 0
for line in metafile: for line in metafile:
c += 1 c += 1
@ -632,6 +645,8 @@ def parse_metadata(metafile):
if 'commit' not in curbuild and 'disable' not in curbuild: if 'commit' not in curbuild and 'disable' not in curbuild:
raise MetaDataException("No commit specified for {0} in {1}" raise MetaDataException("No commit specified for {0} in {1}"
.format(curbuild['version'], linedesc)) .format(curbuild['version'], linedesc))
fill_bool_defaults(curbuild)
thisinfo['builds'].append(curbuild) thisinfo['builds'].append(curbuild)
add_comments('build:' + curbuild['version']) add_comments('build:' + curbuild['version'])
mode = 0 mode = 0
@ -706,8 +721,9 @@ def parse_metadata(metafile):
buildlines.append(line[:-1]) buildlines.append(line[:-1])
else: else:
buildlines.append(line) buildlines.append(line)
thisinfo['builds'].append( curbuild = parse_buildline(buildlines)
parse_buildline(buildlines)) fill_bool_defaults(curbuild)
thisinfo['builds'].append(curbuild)
add_comments('build:' + thisinfo['builds'][-1]['version']) add_comments('build:' + thisinfo['builds'][-1]['version'])
mode = 0 mode = 0
add_comments(None) add_comments(None)
@ -798,21 +814,26 @@ def write_metadata(dest, app):
mf.write("Build:%s,%s\n" % (build['version'], build['vercode'])) mf.write("Build:%s,%s\n" % (build['version'], build['vercode']))
def write_builditem(key, value): def write_builditem(key, value):
if key in ['version', 'vercode', 'origlines', 'type']: if key in ['version', 'vercode', 'origlines', 'type']:
return return
if key in valuetypes['bool'].attrs:
if not value:
return
value = 'yes'
t = flagtype(key) t = flagtype(key)
if t == 'bool' and value == False:
return
logging.debug("...writing {0} : {1}".format(key, value)) logging.debug("...writing {0} : {1}".format(key, value))
outline = ' %s=' % key outline = ' %s=' % key
if t == 'string': if t == 'string':
outline += value outline += value
if t == 'bool':
outline += 'yes'
elif t == 'script': elif t == 'script':
outline += '&& \\\n '.join([s.lstrip() for s in value.split('&& ')]) outline += '&& \\\n '.join([s.lstrip() for s in value.split('&& ')])
elif t == 'list': elif t == 'list':
outline += ','.join(value) if type(value) == list else value outline += ','.join(value) if type(value) == list else value
outline += '\n' outline += '\n'
mf.write(outline) mf.write(outline)