3 # metadata.py - part of the FDroid server tools
4 # Copyright (C) 2013, Ciaran Gultnieks, ciaran@ciarang.com
5 # Copyright (C) 2013-2014 Daniel Martà <mvdan@mvdan.cc>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
30 # use libyaml if it is available
32 from yaml import CLoader
35 from yaml import Loader
38 import fdroidserver.common
39 from fdroidserver.exception import MetaDataException
42 warnings_action = None
45 def warn_or_exception(value):
46 '''output warning or Exception depending on -W'''
47 if warnings_action == 'ignore':
49 elif warnings_action == 'error':
50 raise MetaDataException(value)
52 logging.warning(value)
55 # To filter which ones should be written to the metadata files if
86 'Update Check Ignore',
91 'Current Version Code',
95 'comments', # For formats that don't do inline comments
96 'builds', # For formats that do builds as a list
102 def __init__(self, copydict=None):
104 super().__init__(copydict)
109 self.AntiFeatures = []
111 self.Categories = ['None']
112 self.License = 'Unknown'
113 self.AuthorName = None
114 self.AuthorEmail = None
115 self.AuthorWebSite = None
118 self.IssueTracker = ''
127 self.Description = ''
128 self.RequiresRoot = False
132 self.MaintainerNotes = ''
133 self.ArchivePolicy = None
134 self.AutoUpdateMode = 'None'
135 self.UpdateCheckMode = 'None'
136 self.UpdateCheckIgnore = None
137 self.VercodeOperation = None
138 self.UpdateCheckName = None
139 self.UpdateCheckData = None
140 self.CurrentVersion = ''
141 self.CurrentVersionCode = None
142 self.NoSourceSince = ''
145 self.metadatapath = None
149 self.lastUpdated = None
151 def __getattr__(self, name):
155 raise AttributeError("No such attribute: " + name)
157 def __setattr__(self, name, value):
160 def __delattr__(self, name):
164 raise AttributeError("No such attribute: " + name)
166 def get_last_build(self):
167 if len(self.builds) > 0:
168 return self.builds[-1]
185 'Description': TYPE_MULTILINE,
186 'MaintainerNotes': TYPE_MULTILINE,
187 'Categories': TYPE_LIST,
188 'AntiFeatures': TYPE_LIST,
189 'BuildVersion': TYPE_BUILD,
190 'Build': TYPE_BUILD_V2,
191 'UseBuilt': TYPE_OBSOLETE,
196 name = name.replace(' ', '')
197 if name in fieldtypes:
198 return fieldtypes[name]
202 # In the order in which they are laid out on files
203 build_flags_order = [
236 # old .txt format has version name/code inline in the 'Build:' line
237 # but YAML and JSON have a explicit key for them
238 build_flags = ['versionName', 'versionCode'] + build_flags_order
243 def __init__(self, copydict=None):
248 self.submodules = False
254 self.buildozer = False
257 self.oldsdkloc = False
259 self.forceversion = False
260 self.forcevercode = False
264 self.androidupdate = []
271 self.preassemble = []
272 self.gradleprops = []
273 self.antcommands = []
274 self.novcheck = False
276 super().__init__(copydict)
279 def __getattr__(self, name):
283 raise AttributeError("No such attribute: " + name)
285 def __setattr__(self, name, value):
288 def __delattr__(self, name):
292 raise AttributeError("No such attribute: " + name)
294 def build_method(self):
295 for f in ['maven', 'gradle', 'kivy', 'buildozer']:
302 # like build_method, but prioritize output=
303 def output_method(self):
306 for f in ['maven', 'gradle', 'kivy', 'buildozer']:
314 version = 'r12b' # falls back to latest
315 paths = fdroidserver.common.config['ndk_paths']
316 if version not in paths:
318 return paths[version]
322 'versionCode': TYPE_INT,
323 'extlibs': TYPE_LIST,
324 'srclibs': TYPE_LIST,
327 'buildjni': TYPE_LIST,
328 'preassemble': TYPE_LIST,
329 'androidupdate': TYPE_LIST,
330 'scanignore': TYPE_LIST,
331 'scandelete': TYPE_LIST,
333 'antcommands': TYPE_LIST,
334 'gradleprops': TYPE_LIST,
336 'prebuild': TYPE_SCRIPT,
337 'build': TYPE_SCRIPT,
338 'submodules': TYPE_BOOL,
339 'oldsdkloc': TYPE_BOOL,
340 'forceversion': TYPE_BOOL,
341 'forcevercode': TYPE_BOOL,
342 'novcheck': TYPE_BOOL,
347 if name in flagtypes:
348 return flagtypes[name]
352 class FieldValidator():
354 Designates App metadata field types and checks that it matches
356 'name' - The long name of the field type
357 'matching' - List of possible values or regex expression
358 'sep' - Separator to use if value may be a list
359 'fields' - Metadata fields (Field:Value) of this type
362 def __init__(self, name, matching, fields):
364 self.matching = matching
365 self.compiled = re.compile(matching)
368 def check(self, v, appid):
376 if not self.compiled.match(v):
377 warn_or_exception("'%s' is not a valid %s in %s. Regex pattern: %s"
378 % (v, self.name, appid, self.matching))
381 # Generic value types
383 FieldValidator("Hexadecimal",
387 FieldValidator("HTTP link",
389 ["WebSite", "SourceCode", "IssueTracker", "Changelog", "Donate"]),
391 FieldValidator("Email",
392 r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
395 FieldValidator("Bitcoin address",
396 r'^[a-zA-Z0-9]{27,34}$',
399 FieldValidator("Litecoin address",
400 r'^L[a-zA-Z0-9]{33}$',
403 FieldValidator("Repo Type",
404 r'^(git|git-svn|svn|hg|bzr|srclib)$',
407 FieldValidator("Binaries",
411 FieldValidator("Archive Policy",
412 r'^[0-9]+ versions$',
415 FieldValidator("Anti-Feature",
416 r'^(Ads|Tracking|NonFreeNet|NonFreeDep|NonFreeAdd|UpstreamNonFree|NonFreeAssets|KnownVuln)$',
419 FieldValidator("Auto Update Mode",
420 r"^(Version .+|None)$",
423 FieldValidator("Update Check Mode",
424 r"^(Tags|Tags .+|RepoManifest|RepoManifest/.+|RepoTrunk|HTTP|Static|None)$",
429 # Check an app's metadata information for integrity errors
430 def check_metadata(app):
433 v.check(app[k], app.id)
436 # Formatter for descriptions. Create an instance, and call parseline() with
437 # each line of the description source from the metadata. At the end, call
438 # end() and then text_txt and text_html will contain the result.
439 class DescriptionFormatter:
446 def __init__(self, linkres):
449 self.state = self.stNONE
450 self.laststate = self.stNONE
453 self.html = io.StringIO()
454 self.text = io.StringIO()
456 self.linkResolver = None
457 self.linkResolver = linkres
459 def endcur(self, notstates=None):
460 if notstates and self.state in notstates:
462 if self.state == self.stPARA:
464 elif self.state == self.stUL:
466 elif self.state == self.stOL:
470 self.laststate = self.state
471 self.state = self.stNONE
472 whole_para = ' '.join(self.para_lines)
473 self.addtext(whole_para)
474 wrapped = textwrap.fill(whole_para, 80,
475 break_long_words=False,
476 break_on_hyphens=False)
477 self.text.write(wrapped)
478 self.html.write('</p>')
479 del self.para_lines[:]
482 self.html.write('</ul>')
483 self.laststate = self.state
484 self.state = self.stNONE
487 self.html.write('</ol>')
488 self.laststate = self.state
489 self.state = self.stNONE
491 def formatted(self, txt, htmlbody):
494 txt = html.escape(txt, quote=False)
496 index = txt.find("''")
501 if txt.startswith("'''"):
507 self.bold = not self.bold
515 self.ital = not self.ital
518 def linkify(self, txt):
522 index = txt.find("[")
524 return (res_plain + self.formatted(txt, False), res_html + self.formatted(txt, True))
525 res_plain += self.formatted(txt[:index], False)
526 res_html += self.formatted(txt[:index], True)
528 if txt.startswith("[["):
529 index = txt.find("]]")
531 warn_or_exception("Unterminated ]]")
533 if self.linkResolver:
534 url, urltext = self.linkResolver(url)
537 res_html += '<a href="' + url + '">' + html.escape(urltext, quote=False) + '</a>'
539 txt = txt[index + 2:]
541 index = txt.find("]")
543 warn_or_exception("Unterminated ]")
545 index2 = url.find(' ')
549 urltxt = url[index2 + 1:]
552 warn_or_exception("Url title is just the URL - use [url]")
553 res_html += '<a href="' + url + '">' + html.escape(urltxt, quote=False) + '</a>'
556 res_plain += ' (' + url + ')'
557 txt = txt[index + 1:]
559 def addtext(self, txt):
560 p, h = self.linkify(txt)
563 def parseline(self, line):
566 elif line.startswith('* '):
567 self.endcur([self.stUL])
568 if self.state != self.stUL:
569 self.html.write('<ul>')
570 self.state = self.stUL
571 if self.laststate != self.stNONE:
572 self.text.write('\n\n')
574 self.text.write('\n')
575 self.text.write(line)
576 self.html.write('<li>')
577 self.addtext(line[1:])
578 self.html.write('</li>')
579 elif line.startswith('# '):
580 self.endcur([self.stOL])
581 if self.state != self.stOL:
582 self.html.write('<ol>')
583 self.state = self.stOL
584 if self.laststate != self.stNONE:
585 self.text.write('\n\n')
587 self.text.write('\n')
588 self.text.write(line)
589 self.html.write('<li>')
590 self.addtext(line[1:])
591 self.html.write('</li>')
593 self.para_lines.append(line)
594 self.endcur([self.stPARA])
595 if self.state == self.stNONE:
596 self.state = self.stPARA
597 if self.laststate != self.stNONE:
598 self.text.write('\n\n')
599 self.html.write('<p>')
603 self.text_txt = self.text.getvalue()
604 self.text_html = self.html.getvalue()
609 # Parse multiple lines of description as written in a metadata file, returning
610 # a single string in text format and wrapped to 80 columns.
611 def description_txt(s):
612 ps = DescriptionFormatter(None)
613 for line in s.splitlines():
619 # Parse multiple lines of description as written in a metadata file, returning
620 # a single string in wiki format. Used for the Maintainer Notes field as well,
621 # because it's the same format.
622 def description_wiki(s):
626 # Parse multiple lines of description as written in a metadata file, returning
627 # a single string in HTML format.
628 def description_html(s, linkres):
629 ps = DescriptionFormatter(linkres)
630 for line in s.splitlines():
636 def parse_srclib(metadatapath):
640 # Defaults for fields that come from metadata
641 thisinfo['Repo Type'] = ''
642 thisinfo['Repo'] = ''
643 thisinfo['Subdir'] = None
644 thisinfo['Prepare'] = None
646 if not os.path.exists(metadatapath):
649 metafile = open(metadatapath, "r", encoding='utf-8')
652 for line in metafile:
654 line = line.rstrip('\r\n')
655 if not line or line.startswith("#"):
659 f, v = line.split(':', 1)
661 warn_or_exception("Invalid metadata in %s:%d" % (line, n))
664 thisinfo[f] = v.split(',')
674 """Read all srclib metadata.
676 The information read will be accessible as metadata.srclibs, which is a
677 dictionary, keyed on srclib name, with the values each being a dictionary
678 in the same format as that returned by the parse_srclib function.
680 A MetaDataException is raised if there are any problems with the srclib
685 # They were already loaded
686 if srclibs is not None:
692 if not os.path.exists(srcdir):
695 for metadatapath in sorted(glob.glob(os.path.join(srcdir, '*.txt'))):
696 srclibname = os.path.basename(metadatapath[:-4])
697 srclibs[srclibname] = parse_srclib(metadatapath)
700 def read_metadata(xref=True, check_vcs=[]):
702 Read all metadata. Returns a list of 'app' objects (which are dictionaries as
703 returned by the parse_txt_metadata function.
705 check_vcs is the list of packageNames to check for .fdroid.yml in source
708 # Always read the srclibs before the apps, since they can use a srlib as
709 # their source repository.
714 for basedir in ('metadata', 'tmp'):
715 if not os.path.exists(basedir):
718 # If there are multiple metadata files for a single appid, then the first
719 # file that is parsed wins over all the others, and the rest throw an
720 # exception. So the original .txt format is parsed first, at least until
721 # newer formats stabilize.
723 for metadatapath in sorted(glob.glob(os.path.join('metadata', '*.txt'))
724 + glob.glob(os.path.join('metadata', '*.json'))
725 + glob.glob(os.path.join('metadata', '*.yml'))
726 + glob.glob('.fdroid.json')
727 + glob.glob('.fdroid.yml')):
728 packageName, _ = fdroidserver.common.get_extension(os.path.basename(metadatapath))
729 if packageName in apps:
730 warn_or_exception("Found multiple metadata files for " + packageName)
731 app = parse_metadata(metadatapath, packageName in check_vcs)
736 # Parse all descriptions at load time, just to ensure cross-referencing
737 # errors are caught early rather than when they hit the build server.
740 return ("fdroid.app:" + appid, "Dummy name - don't know yet")
741 warn_or_exception("Cannot resolve app id " + appid)
743 for appid, app in apps.items():
745 description_html(app.Description, linkres)
746 except MetaDataException as e:
747 warn_or_exception("Problem with description of " + appid +
753 # Port legacy ';' separators
754 list_sep = re.compile(r'[,;]')
757 def split_list_values(s):
759 for v in re.split(list_sep, s):
769 def get_default_app_info(metadatapath=None):
770 if metadatapath is None:
773 appid, _ = fdroidserver.common.get_extension(os.path.basename(metadatapath))
775 if appid == '.fdroid': # we have local metadata in the app's source
776 if os.path.exists('AndroidManifest.xml'):
777 manifestroot = fdroidserver.common.parse_xml('AndroidManifest.xml')
779 pattern = re.compile(""".*manifest\.srcFile\s+'AndroidManifest\.xml'.*""")
780 for root, dirs, files in os.walk(os.getcwd()):
781 if 'build.gradle' in files:
782 p = os.path.join(root, 'build.gradle')
783 with open(p, 'rb') as f:
785 m = pattern.search(data)
787 logging.debug('Using: ' + os.path.join(root, 'AndroidManifest.xml'))
788 manifestroot = fdroidserver.common.parse_xml(os.path.join(root, 'AndroidManifest.xml'))
790 if manifestroot is None:
791 warn_or_exception("Cannot find a packageName for {0}!".format(metadatapath))
792 appid = manifestroot.attrib['package']
795 app.metadatapath = metadatapath
796 if appid is not None:
802 def sorted_builds(builds):
803 return sorted(builds, key=lambda build: int(build.versionCode))
806 esc_newlines = re.compile(r'\\( |\n)')
809 def post_metadata_parse(app):
810 # TODO keep native types, convert only for .txt metadata
811 for k, v in app.items():
812 if type(v) in (float, int):
815 if isinstance(app.Categories, str):
816 app.Categories = [app.Categories]
817 elif app.Categories is None:
818 app.Categories = ['None']
820 app.Categories = [str(i) for i in app.Categories]
824 for build in app['builds']:
825 if not isinstance(build, Build):
827 for k, v in build.items():
828 if flagtype(k) == TYPE_LIST:
829 if isinstance(v, str):
831 elif isinstance(v, bool):
836 elif flagtype(k) == TYPE_STRING and type(v) in (float, int):
840 app.builds = sorted_builds(builds)
843 # Parse metadata for a single application.
845 # 'metadatapath' - the filename to read. The package id for the application comes
846 # from this filename. Pass None to get a blank entry.
848 # Returns a dictionary containing all the details of the application. There are
849 # two major kinds of information in the dictionary. Keys beginning with capital
850 # letters correspond directory to identically named keys in the metadata file.
851 # Keys beginning with lower case letters are generated in one way or another,
852 # and are not found verbatim in the metadata.
854 # Known keys not originating from the metadata are:
856 # 'builds' - a list of dictionaries containing build information
857 # for each defined build
858 # 'comments' - a list of comments from the metadata file. Each is
859 # a list of the form [field, comment] where field is
860 # the name of the field it preceded in the metadata
861 # file. Where field is None, the comment goes at the
862 # end of the file. Alternatively, 'build:version' is
863 # for a comment before a particular build version.
864 # 'descriptionlines' - original lines of description as formatted in the
869 bool_true = re.compile(r'([Yy]es|[Tt]rue)')
870 bool_false = re.compile(r'([Nn]o|[Ff]alse)')
874 if bool_true.match(s):
876 if bool_false.match(s):
878 warn_or_exception("Invalid bool '%s'" % s)
881 def parse_metadata(metadatapath, check_vcs=False):
882 '''parse metadata file, optionally checking the git repo for metadata first'''
884 _, ext = fdroidserver.common.get_extension(metadatapath)
885 accepted = fdroidserver.common.config['accepted_formats']
886 if ext not in accepted:
887 warn_or_exception('"%s" is not an accepted format, convert to: %s' % (
888 metadatapath, ', '.join(accepted)))
891 app.metadatapath = metadatapath
892 name, _ = fdroidserver.common.get_extension(os.path.basename(metadatapath))
893 if name == '.fdroid':
898 with open(metadatapath, 'r', encoding='utf-8') as mf:
900 parse_txt_metadata(mf, app)
902 parse_json_metadata(mf, app)
904 parse_yaml_metadata(mf, app)
906 warn_or_exception('Unknown metadata format: %s' % metadatapath)
908 if check_vcs and app.Repo:
909 build_dir = fdroidserver.common.get_build_dir(app)
910 metadata_in_repo = os.path.join(build_dir, '.fdroid.yml')
911 if not os.path.isfile(metadata_in_repo):
912 vcs, build_dir = fdroidserver.common.setup_vcs(app)
913 if isinstance(vcs, fdroidserver.common.vcs_git):
914 vcs.gotorevision('HEAD') # HEAD since we can't know where else to go
915 if os.path.isfile(metadata_in_repo):
916 logging.debug('Including metadata from ' + metadata_in_repo)
917 # do not include fields already provided by main metadata file
918 app_in_repo = parse_metadata(metadata_in_repo)
919 for k, v in app_in_repo.items():
923 post_metadata_parse(app)
927 build = app.builds[-1]
929 root_dir = build.subdir
932 paths = fdroidserver.common.manifest_paths(root_dir, build.gradle)
933 _, _, app.id = fdroidserver.common.parse_androidmanifests(paths, app)
938 def parse_json_metadata(mf, app):
940 # fdroid metadata is only strings and booleans, no floats or ints.
941 # TODO create schema using https://pypi.python.org/pypi/jsonschema
942 jsoninfo = json.load(mf, parse_int=lambda s: s,
943 parse_float=lambda s: s)
945 for f in ['Description', 'Maintainer Notes']:
948 app[f] = '\n'.join(v)
952 def parse_yaml_metadata(mf, app):
954 yamlinfo = yaml.load(mf, Loader=YamlLoader)
959 def write_yaml(mf, app):
961 def _class_as_dict_representer(dumper, data):
962 '''Creates a YAML representation of a App/Build instance'''
963 return dumper.represent_dict(data)
965 def _field_to_yaml(typ, value):
966 if typ is TYPE_STRING:
968 elif typ is TYPE_INT:
970 elif typ is TYPE_MULTILINE:
972 return ruamel.yaml.scalarstring.preserve_literal(str(value))
975 elif typ is TYPE_SCRIPT:
977 return ruamel.yaml.scalarstring.preserve_literal(value)
983 def _app_to_yaml(app):
984 cm = ruamel.yaml.comments.CommentedMap()
985 insert_newline = False
986 for field in yaml_app_field_order:
988 # next iteration will need to insert a newline
989 insert_newline = True
991 if (hasattr(app, field) and getattr(app, field)) or field is 'Builds':
992 if field is 'Builds':
993 cm.update({field: _builds_to_yaml(app)})
994 elif field is 'CurrentVersionCode':
995 cm.update({field: _field_to_yaml(TYPE_INT, getattr(app, field))})
997 cm.update({field: _field_to_yaml(fieldtype(field), getattr(app, field))})
1000 # we need to prepend a newline in front of this field
1001 insert_newline = False
1002 # inserting empty lines is not supported so we add a
1003 # bogus comment and over-write its value
1004 cm.yaml_set_comment_before_after_key(field, 'bogus')
1005 cm.ca.items[field][1][-1].value = '\n'
1008 def _builds_to_yaml(app):
1009 fields = ['versionName', 'versionCode']
1010 fields.extend(build_flags_order)
1011 builds = ruamel.yaml.comments.CommentedSeq()
1012 for build in app.builds:
1013 b = ruamel.yaml.comments.CommentedMap()
1014 for field in fields:
1015 if hasattr(build, field) and getattr(build, field):
1016 b.update({field: _field_to_yaml(flagtype(field), getattr(build, field))})
1019 # insert extra empty lines between build entries
1020 for i in range(1, len(builds)):
1021 builds.yaml_set_comment_before_after_key(i, 'bogus')
1022 builds.ca.items[i][1][-1].value = '\n'
1026 yaml_app_field_order = [
1050 'CurrentVersionCode',
1053 yaml_app = _app_to_yaml(app)
1054 ruamel.yaml.round_trip_dump(yaml_app, mf, indent=4, block_seq_indent=2)
1057 build_line_sep = re.compile(r'(?<!\\),')
1058 build_cont = re.compile(r'^[ \t]')
1061 def parse_txt_metadata(mf, app):
1065 def add_buildflag(p, build):
1067 warn_or_exception("Empty build flag at {1}"
1068 .format(buildlines[0], linedesc))
1069 bv = p.split('=', 1)
1071 warn_or_exception("Invalid build flag at {0} in {1}"
1072 .format(buildlines[0], linedesc))
1077 pk = 'androidupdate' # avoid conflicting with Build(dict).update()
1080 pv = split_list_values(pv)
1082 elif t == TYPE_STRING or t == TYPE_SCRIPT:
1084 elif t == TYPE_BOOL:
1085 build[pk] = _decode_bool(pv)
1087 def parse_buildline(lines):
1089 parts = [p.replace("\\,", ",") for p in re.split(build_line_sep, v)]
1091 warn_or_exception("Invalid build format: " + v + " in " + mf.name)
1093 build.versionName = parts[0]
1094 build.versionCode = parts[1]
1095 check_versionCode(build.versionCode)
1097 if parts[2].startswith('!'):
1098 # For backwards compatibility, handle old-style disabling,
1099 # including attempting to extract the commit from the message
1100 build.disable = parts[2][1:]
1101 commit = 'unknown - see disabled'
1102 index = parts[2].rfind('at ')
1104 commit = parts[2][index + 3:]
1105 if commit.endswith(')'):
1106 commit = commit[:-1]
1107 build.commit = commit
1109 build.commit = parts[2]
1111 add_buildflag(p, build)
1115 def check_versionCode(versionCode):
1119 warn_or_exception('Invalid versionCode: "' + versionCode + '" is not an integer!')
1121 def add_comments(key):
1124 app.comments[key] = list(curcomments)
1129 multiline_lines = []
1139 linedesc = "%s:%d" % (mf.name, c)
1140 line = line.rstrip('\r\n')
1142 if build_cont.match(line):
1143 if line.endswith('\\'):
1144 buildlines.append(line[:-1].lstrip())
1146 buildlines.append(line.lstrip())
1147 bl = ''.join(buildlines)
1148 add_buildflag(bl, build)
1151 if not build.commit and not build.disable:
1152 warn_or_exception("No commit specified for {0} in {1}"
1153 .format(build.versionName, linedesc))
1155 app.builds.append(build)
1156 add_comments('build:' + build.versionCode)
1162 if line.startswith("#"):
1163 curcomments.append(line[1:].strip())
1166 f, v = line.split(':', 1)
1168 warn_or_exception("Invalid metadata in " + linedesc)
1170 if f not in app_fields:
1171 warn_or_exception('Unrecognised app field: ' + f)
1173 # Translate obsolete fields...
1174 if f == 'Market Version':
1175 f = 'Current Version'
1176 if f == 'Market Version Code':
1177 f = 'Current Version Code'
1179 f = f.replace(' ', '')
1181 ftype = fieldtype(f)
1182 if ftype not in [TYPE_BUILD, TYPE_BUILD_V2]:
1184 if ftype == TYPE_MULTILINE:
1187 warn_or_exception("Unexpected text on same line as "
1188 + f + " in " + linedesc)
1189 elif ftype == TYPE_STRING:
1191 elif ftype == TYPE_LIST:
1192 app[f] = split_list_values(v)
1193 elif ftype == TYPE_BUILD:
1194 if v.endswith("\\"):
1197 buildlines.append(v[:-1])
1199 build = parse_buildline([v])
1200 app.builds.append(build)
1201 add_comments('build:' + app.builds[-1].versionCode)
1202 elif ftype == TYPE_BUILD_V2:
1205 warn_or_exception('Build should have comma-separated',
1206 'versionName and versionCode,',
1207 'not "{0}", in {1}'.format(v, linedesc))
1209 build.versionName = vv[0]
1210 build.versionCode = vv[1]
1211 check_versionCode(build.versionCode)
1213 if build.versionCode in vc_seen:
1214 warn_or_exception('Duplicate build recipe found for versionCode %s in %s'
1215 % (build.versionCode, linedesc))
1216 vc_seen.add(build.versionCode)
1219 elif ftype == TYPE_OBSOLETE:
1220 pass # Just throw it away!
1222 warn_or_exception("Unrecognised field '" + f + "' in " + linedesc)
1223 elif mode == 1: # Multiline field
1226 app[f] = '\n'.join(multiline_lines)
1227 del multiline_lines[:]
1229 multiline_lines.append(line)
1230 elif mode == 2: # Line continuation mode in Build Version
1231 if line.endswith("\\"):
1232 buildlines.append(line[:-1])
1234 buildlines.append(line)
1235 build = parse_buildline(buildlines)
1236 app.builds.append(build)
1237 add_comments('build:' + app.builds[-1].versionCode)
1241 # Mode at end of file should always be 0
1243 warn_or_exception(f + " not terminated in " + mf.name)
1245 warn_or_exception("Unterminated continuation in " + mf.name)
1247 warn_or_exception("Unterminated build in " + mf.name)
1252 def write_plaintext_metadata(mf, app, w_comment, w_field, w_build):
1254 def field_to_attr(f):
1256 Translates human-readable field names to attribute names, e.g.
1257 'Auto Name' to 'AutoName'
1259 return f.replace(' ', '')
1261 def attr_to_field(k):
1263 Translates attribute names to human-readable field names, e.g.
1264 'AutoName' to 'Auto Name'
1268 f = re.sub(r'([a-z])([A-Z])', r'\1 \2', k)
1271 def w_comments(key):
1272 if key not in app.comments:
1274 for line in app.comments[key]:
1277 def w_field_always(f, v=None):
1278 key = field_to_attr(f)
1284 def w_field_nonempty(f, v=None):
1285 key = field_to_attr(f)
1292 w_field_nonempty('Disabled')
1293 w_field_nonempty('AntiFeatures')
1294 w_field_nonempty('Provides')
1295 w_field_always('Categories')
1296 w_field_always('License')
1297 w_field_nonempty('Author Name')
1298 w_field_nonempty('Author Email')
1299 w_field_nonempty('Author Web Site')
1300 w_field_always('Web Site')
1301 w_field_always('Source Code')
1302 w_field_always('Issue Tracker')
1303 w_field_nonempty('Changelog')
1304 w_field_nonempty('Donate')
1305 w_field_nonempty('FlattrID')
1306 w_field_nonempty('Bitcoin')
1307 w_field_nonempty('Litecoin')
1309 w_field_nonempty('Name')
1310 w_field_nonempty('Auto Name')
1311 w_field_nonempty('Summary')
1312 w_field_nonempty('Description', description_txt(app.Description))
1314 if app.RequiresRoot:
1315 w_field_always('Requires Root', 'yes')
1318 w_field_always('Repo Type')
1319 w_field_always('Repo')
1321 w_field_always('Binaries')
1324 for build in app.builds:
1326 if build.versionName == "Ignore":
1329 w_comments('build:%s' % build.versionCode)
1333 if app.MaintainerNotes:
1334 w_field_always('Maintainer Notes', app.MaintainerNotes)
1337 w_field_nonempty('Archive Policy')
1338 w_field_always('Auto Update Mode')
1339 w_field_always('Update Check Mode')
1340 w_field_nonempty('Update Check Ignore')
1341 w_field_nonempty('Vercode Operation')
1342 w_field_nonempty('Update Check Name')
1343 w_field_nonempty('Update Check Data')
1344 if app.CurrentVersion:
1345 w_field_always('Current Version')
1346 w_field_always('Current Version Code')
1347 if app.NoSourceSince:
1349 w_field_always('No Source Since')
1353 # Write a metadata file in txt format.
1355 # 'mf' - Writer interface (file, StringIO, ...)
1356 # 'app' - The app data
1357 def write_txt(mf, app):
1359 def w_comment(line):
1360 mf.write("# %s\n" % line)
1366 elif t == TYPE_MULTILINE:
1367 v = '\n' + v + '\n.'
1368 mf.write("%s:%s\n" % (f, v))
1371 mf.write("Build:%s,%s\n" % (build.versionName, build.versionCode))
1373 for f in build_flags_order:
1379 if f == 'androidupdate':
1380 f = 'update' # avoid conflicting with Build(dict).update()
1381 mf.write(' %s=' % f)
1382 if t == TYPE_STRING:
1384 elif t == TYPE_BOOL:
1386 elif t == TYPE_SCRIPT:
1388 for s in v.split(' && '):
1392 mf.write(' && \\\n ')
1394 elif t == TYPE_LIST:
1395 mf.write(','.join(v))
1399 write_plaintext_metadata(mf, app, w_comment, w_field, w_build)
1402 def write_metadata(metadatapath, app):
1403 _, ext = fdroidserver.common.get_extension(metadatapath)
1404 accepted = fdroidserver.common.config['accepted_formats']
1405 if ext not in accepted:
1406 warn_or_exception('Cannot write "%s", not an accepted format, use: %s'
1407 % (metadatapath, ', '.join(accepted)))
1409 with open(metadatapath, 'w', encoding='utf8') as mf:
1411 return write_txt(mf, app)
1413 return write_yaml(mf, app)
1414 warn_or_exception('Unknown metadata format: %s' % metadatapath)
1417 def add_metadata_arguments(parser):
1418 '''add common command line flags related to metadata processing'''
1419 parser.add_argument("-W", default='error',
1420 help="force errors to be warnings, or ignore")