1 # -*- coding: utf-8 -*-
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/>.
26 from collections import OrderedDict
33 class MetaDataException(Exception):
35 def __init__(self, value):
41 # In the order in which they are laid out on files
42 app_defaults = OrderedDict([
44 ('AntiFeatures', None),
46 ('Categories', ['None']),
47 ('License', 'Unknown'),
50 ('Issue Tracker', ''),
61 ('Requires Root', False),
65 ('Maintainer Notes', []),
66 ('Archive Policy', None),
67 ('Auto Update Mode', 'None'),
68 ('Update Check Mode', 'None'),
69 ('Update Check Ignore', None),
70 ('Vercode Operation', None),
71 ('Update Check Name', None),
72 ('Update Check Data', None),
73 ('Current Version', ''),
74 ('Current Version Code', '0'),
75 ('No Source Since', ''),
79 # In the order in which they are laid out on files
80 # Sorted by their action and their place in the build timeline
81 flag_defaults = OrderedDict([
85 ('submodules', False),
95 ('forceversion', False),
96 ('forcevercode', False),
100 ('update', ['auto']),
106 ('ndk', 'r9b'), # defaults to oldest
108 ('antcommands', None),
113 # Designates a metadata field type and checks that it matches
115 # 'name' - The long name of the field type
116 # 'matching' - List of possible values or regex expression
117 # 'sep' - Separator to use if value may be a list
118 # 'fields' - Metadata fields (Field:Value) of this type
119 # 'attrs' - Build attributes (attr=value) of this type
121 class FieldValidator():
123 def __init__(self, name, matching, sep, fields, attrs):
125 self.matching = matching
126 if type(matching) is str:
127 self.compiled = re.compile(matching)
132 def _assert_regex(self, values, appid):
134 if not self.compiled.match(v):
135 raise MetaDataException("'%s' is not a valid %s in %s. "
136 % (v, self.name, appid) +
137 "Regex pattern: %s" % (self.matching))
139 def _assert_list(self, values, appid):
141 if v not in self.matching:
142 raise MetaDataException("'%s' is not a valid %s in %s. "
143 % (v, self.name, appid) +
144 "Possible values: %s" % (", ".join(self.matching)))
146 def check(self, value, appid):
147 if type(value) is not str or not value:
149 if self.sep is not None:
150 values = value.split(self.sep)
153 if type(self.matching) is list:
154 self._assert_list(values, appid)
156 self._assert_regex(values, appid)
159 # Generic value types
161 FieldValidator("Integer",
162 r'^[1-9][0-9]*$', None,
166 FieldValidator("Hexadecimal",
167 r'^[0-9a-f]+$', None,
171 FieldValidator("HTTP link",
172 r'^http[s]?://', None,
173 ["Web Site", "Source Code", "Issue Tracker", "Changelog", "Donate"], []),
175 FieldValidator("Bitcoin address",
176 r'^[a-zA-Z0-9]{27,34}$', None,
180 FieldValidator("Litecoin address",
181 r'^L[a-zA-Z0-9]{33}$', None,
185 FieldValidator("Dogecoin address",
186 r'^D[a-zA-Z0-9]{33}$', None,
190 FieldValidator("Boolean",
195 FieldValidator("bool",
198 ['submodules', 'oldsdkloc', 'forceversion', 'forcevercode',
201 FieldValidator("Repo Type",
202 ['git', 'git-svn', 'svn', 'hg', 'bzr', 'srclib'], None,
206 FieldValidator("Binaries",
207 r'^http[s]?://', None,
211 FieldValidator("Archive Policy",
212 r'^[0-9]+ versions$', None,
216 FieldValidator("Anti-Feature",
217 ["Ads", "Tracking", "NonFreeNet", "NonFreeDep", "NonFreeAdd", "UpstreamNonFree"], ',',
221 FieldValidator("Auto Update Mode",
222 r"^(Version .+|None)$", None,
223 ["Auto Update Mode"],
226 FieldValidator("Update Check Mode",
227 r"^(Tags|Tags .+|RepoManifest|RepoManifest/.+|RepoTrunk|HTTP|Static|None)$", None,
228 ["Update Check Mode"],
233 # Check an app's metadata information for integrity errors
234 def check_metadata(info):
236 for field in v.fields:
237 v.check(info[field], info['id'])
238 for build in info['builds']:
240 v.check(build[attr], info['id'])
243 # Formatter for descriptions. Create an instance, and call parseline() with
244 # each line of the description source from the metadata. At the end, call
245 # end() and then text_wiki and text_html will contain the result.
246 class DescriptionFormatter:
258 def __init__(self, linkres):
259 self.linkResolver = linkres
261 def endcur(self, notstates=None):
262 if notstates and self.state in notstates:
264 if self.state == self.stPARA:
266 elif self.state == self.stUL:
268 elif self.state == self.stOL:
272 self.text_html += '</p>'
273 self.state = self.stNONE
276 self.text_html += '</ul>'
277 self.state = self.stNONE
280 self.text_html += '</ol>'
281 self.state = self.stNONE
283 def formatted(self, txt, html):
286 txt = cgi.escape(txt)
288 index = txt.find("''")
290 return formatted + txt
291 formatted += txt[:index]
293 if txt.startswith("'''"):
299 self.bold = not self.bold
307 self.ital = not self.ital
310 def linkify(self, txt):
314 index = txt.find("[")
316 return (linkified_plain + self.formatted(txt, False), linkified_html + self.formatted(txt, True))
317 linkified_plain += self.formatted(txt[:index], False)
318 linkified_html += self.formatted(txt[:index], True)
320 if txt.startswith("[["):
321 index = txt.find("]]")
323 raise MetaDataException("Unterminated ]]")
325 if self.linkResolver:
326 url, urltext = self.linkResolver(url)
329 linkified_html += '<a href="' + url + '">' + cgi.escape(urltext) + '</a>'
330 linkified_plain += urltext
331 txt = txt[index + 2:]
333 index = txt.find("]")
335 raise MetaDataException("Unterminated ]")
337 index2 = url.find(' ')
341 urltxt = url[index2 + 1:]
343 linkified_html += '<a href="' + url + '">' + cgi.escape(urltxt) + '</a>'
344 linkified_plain += urltxt
346 linkified_plain += ' (' + url + ')'
347 txt = txt[index + 1:]
349 def addtext(self, txt):
350 p, h = self.linkify(txt)
353 def parseline(self, line):
354 self.text_wiki += "%s\n" % line
357 elif line.startswith('* '):
358 self.endcur([self.stUL])
359 if self.state != self.stUL:
360 self.text_html += '<ul>'
361 self.state = self.stUL
362 self.text_html += '<li>'
363 self.addtext(line[1:])
364 self.text_html += '</li>'
365 elif line.startswith('# '):
366 self.endcur([self.stOL])
367 if self.state != self.stOL:
368 self.text_html += '<ol>'
369 self.state = self.stOL
370 self.text_html += '<li>'
371 self.addtext(line[1:])
372 self.text_html += '</li>'
374 self.endcur([self.stPARA])
375 if self.state == self.stNONE:
376 self.text_html += '<p>'
377 self.state = self.stPARA
378 elif self.state == self.stPARA:
379 self.text_html += ' '
386 # Parse multiple lines of description as written in a metadata file, returning
387 # a single string in wiki format. Used for the Maintainer Notes field as well,
388 # because it's the same format.
389 def description_wiki(lines):
390 ps = DescriptionFormatter(None)
397 # Parse multiple lines of description as written in a metadata file, returning
398 # a single string in HTML format.
399 def description_html(lines, linkres):
400 ps = DescriptionFormatter(linkres)
407 def parse_srclib(metafile):
410 if metafile and not isinstance(metafile, file):
411 metafile = open(metafile, "r")
413 # Defaults for fields that come from metadata
414 thisinfo['Repo Type'] = ''
415 thisinfo['Repo'] = ''
416 thisinfo['Subdir'] = None
417 thisinfo['Prepare'] = None
418 thisinfo['Srclibs'] = None
424 for line in metafile:
426 line = line.rstrip('\r\n')
427 if not line or line.startswith("#"):
431 field, value = line.split(':', 1)
433 raise MetaDataException("Invalid metadata in %s:%d" % (line, n))
435 if field == "Subdir":
436 thisinfo[field] = value.split(',')
438 thisinfo[field] = value
444 """Read all srclib metadata.
446 The information read will be accessible as metadata.srclibs, which is a
447 dictionary, keyed on srclib name, with the values each being a dictionary
448 in the same format as that returned by the parse_srclib function.
450 A MetaDataException is raised if there are any problems with the srclib
455 # They were already loaded
456 if srclibs is not None:
462 if not os.path.exists(srcdir):
465 for metafile in sorted(glob.glob(os.path.join(srcdir, '*.txt'))):
466 srclibname = os.path.basename(metafile[:-4])
467 srclibs[srclibname] = parse_srclib(metafile)
470 # Read all metadata. Returns a list of 'app' objects (which are dictionaries as
471 # returned by the parse_metadata function.
472 def read_metadata(xref=True):
474 # Always read the srclibs before the apps, since they can use a srlib as
475 # their source repository.
480 for basedir in ('metadata', 'tmp'):
481 if not os.path.exists(basedir):
484 for metafile in sorted(glob.glob(os.path.join('metadata', '*.txt'))):
485 appid, appinfo = parse_metadata(metafile)
486 check_metadata(appinfo)
487 apps[appid] = appinfo
490 # Parse all descriptions at load time, just to ensure cross-referencing
491 # errors are caught early rather than when they hit the build server.
494 return ("fdroid.app:" + appid, "Dummy name - don't know yet")
495 raise MetaDataException("Cannot resolve app id " + appid)
497 for appid, app in apps.iteritems():
499 description_html(app['Description'], linkres)
500 except MetaDataException, e:
501 raise MetaDataException("Problem with description of " + appid +
507 # Get the type expected for a given metadata field.
508 def metafieldtype(name):
509 if name in ['Description', 'Maintainer Notes']:
511 if name in ['Categories']:
513 if name == 'Build Version':
517 if name == 'Use Built':
519 if name not in app_defaults:
525 if name in ['extlibs', 'srclibs', 'patch', 'rm', 'buildjni', 'preassemble',
526 'update', 'scanignore', 'scandelete', 'gradle', 'antcommands']:
528 if name in ['init', 'prebuild', 'build']:
530 if name in ['submodules', 'oldsdkloc', 'forceversion', 'forcevercode',
536 def fill_build_defaults(build):
538 def get_build_type():
539 for t in ['maven', 'gradle', 'kivy']:
546 for flag, value in flag_defaults.iteritems():
550 build['type'] = get_build_type()
551 build['ndk_path'] = common.get_ndk_path(build['ndk'])
554 # Parse metadata for a single application.
556 # 'metafile' - the filename to read. The package id for the application comes
557 # from this filename. Pass None to get a blank entry.
559 # Returns a dictionary containing all the details of the application. There are
560 # two major kinds of information in the dictionary. Keys beginning with capital
561 # letters correspond directory to identically named keys in the metadata file.
562 # Keys beginning with lower case letters are generated in one way or another,
563 # and are not found verbatim in the metadata.
565 # Known keys not originating from the metadata are:
567 # 'builds' - a list of dictionaries containing build information
568 # for each defined build
569 # 'comments' - a list of comments from the metadata file. Each is
570 # a tuple of the form (field, comment) where field is
571 # the name of the field it preceded in the metadata
572 # file. Where field is None, the comment goes at the
573 # end of the file. Alternatively, 'build:version' is
574 # for a comment before a particular build version.
575 # 'descriptionlines' - original lines of description as formatted in the
578 def parse_metadata(metafile):
583 def add_buildflag(p, thisbuild):
586 raise MetaDataException("Invalid build flag at {0} in {1}"
587 .format(buildlines[0], linedesc))
590 raise MetaDataException("Duplicate definition on {0} in version {1} of {2}"
591 .format(pk, thisbuild['version'], linedesc))
594 if pk not in flag_defaults:
595 raise MetaDataException("Unrecognised build flag at {0} in {1}"
596 .format(p, linedesc))
599 # Port legacy ';' separators
600 pv = [v.strip() for v in pv.replace(';', ',').split(',')]
602 if len(pv) == 1 and pv[0] in ['main', 'yes']:
605 elif t == 'string' or t == 'script':
612 logging.debug("...ignoring bool flag %s" % p)
615 raise MetaDataException("Unrecognised build flag type '%s' at %s in %s"
618 def parse_buildline(lines):
619 value = "".join(lines)
620 parts = [p.replace("\\,", ",")
621 for p in re.split(r"(?<!\\),", value)]
623 raise MetaDataException("Invalid build format: " + value + " in " + metafile.name)
625 thisbuild['origlines'] = lines
626 thisbuild['version'] = parts[0]
627 thisbuild['vercode'] = parts[1]
628 if parts[2].startswith('!'):
629 # For backwards compatibility, handle old-style disabling,
630 # including attempting to extract the commit from the message
631 thisbuild['disable'] = parts[2][1:]
632 commit = 'unknown - see disabled'
633 index = parts[2].rfind('at ')
635 commit = parts[2][index + 3:]
636 if commit.endswith(')'):
638 thisbuild['commit'] = commit
640 thisbuild['commit'] = parts[2]
642 add_buildflag(p, thisbuild)
646 def add_comments(key):
649 for comment in curcomments:
650 thisinfo['comments'].append((key, comment))
655 if not isinstance(metafile, file):
656 metafile = open(metafile, "r")
657 appid = metafile.name[9:-4]
659 thisinfo.update(app_defaults)
660 thisinfo['id'] = appid
662 # General defaults...
663 thisinfo['builds'] = []
664 thisinfo['comments'] = []
667 return appid, thisinfo
676 for line in metafile:
678 linedesc = "%s:%d" % (metafile.name, c)
679 line = line.rstrip('\r\n')
681 if not any(line.startswith(s) for s in (' ', '\t')):
682 commit = curbuild['commit'] if 'commit' in curbuild else None
683 if not commit and 'disable' not in curbuild:
684 raise MetaDataException("No commit specified for {0} in {1}"
685 .format(curbuild['version'], linedesc))
687 thisinfo['builds'].append(curbuild)
688 add_comments('build:' + curbuild['vercode'])
691 if line.endswith('\\'):
692 buildlines.append(line[:-1].lstrip())
694 buildlines.append(line.lstrip())
695 bl = ''.join(buildlines)
696 add_buildflag(bl, curbuild)
702 if line.startswith("#"):
703 curcomments.append(line)
706 field, value = line.split(':', 1)
708 raise MetaDataException("Invalid metadata in " + linedesc)
709 if field != field.strip() or value != value.strip():
710 raise MetaDataException("Extra spacing found in " + linedesc)
712 # Translate obsolete fields...
713 if field == 'Market Version':
714 field = 'Current Version'
715 if field == 'Market Version Code':
716 field = 'Current Version Code'
718 fieldtype = metafieldtype(field)
719 if fieldtype not in ['build', 'buildv2']:
721 if fieldtype == 'multiline':
725 raise MetaDataException("Unexpected text on same line as " + field + " in " + linedesc)
726 elif fieldtype == 'string':
727 thisinfo[field] = value
728 elif fieldtype == 'list':
729 thisinfo[field] = [v.strip() for v in value.replace(';', ',').split(',')]
730 elif fieldtype == 'build':
731 if value.endswith("\\"):
733 buildlines = [value[:-1]]
735 curbuild = parse_buildline([value])
736 thisinfo['builds'].append(curbuild)
737 add_comments('build:' + thisinfo['builds'][-1]['vercode'])
738 elif fieldtype == 'buildv2':
740 vv = value.split(',')
742 raise MetaDataException('Build should have comma-separated version and vercode, not "{0}", in {1}'
743 .format(value, linedesc))
744 curbuild['version'] = vv[0]
745 curbuild['vercode'] = vv[1]
746 if curbuild['vercode'] in vc_seen:
747 raise MetaDataException('Duplicate build recipe found for vercode %s in %s' % (
748 curbuild['vercode'], linedesc))
749 vc_seen[curbuild['vercode']] = True
752 elif fieldtype == 'obsolete':
753 pass # Just throw it away!
755 raise MetaDataException("Unrecognised field type for " + field + " in " + linedesc)
756 elif mode == 1: # Multiline field
760 thisinfo[field].append(line)
761 elif mode == 2: # Line continuation mode in Build Version
762 if line.endswith("\\"):
763 buildlines.append(line[:-1])
765 buildlines.append(line)
766 curbuild = parse_buildline(buildlines)
767 thisinfo['builds'].append(curbuild)
768 add_comments('build:' + thisinfo['builds'][-1]['vercode'])
772 # Mode at end of file should always be 0...
774 raise MetaDataException(field + " not terminated in " + metafile.name)
776 raise MetaDataException("Unterminated continuation in " + metafile.name)
778 raise MetaDataException("Unterminated build in " + metafile.name)
780 if not thisinfo['Description']:
781 thisinfo['Description'].append('No description available')
783 for build in thisinfo['builds']:
784 fill_build_defaults(build)
786 thisinfo['builds'] = sorted(thisinfo['builds'], key=lambda build: int(build['vercode']))
788 return (appid, thisinfo)
791 # Write a metadata file.
793 # 'dest' - The path to the output file
794 # 'app' - The app data
795 def write_metadata(dest, app):
797 def writecomments(key):
799 for pf, comment in app['comments']:
801 mf.write("%s\n" % comment)
804 logging.debug("...writing comments for " + (key or 'EOF'))
806 def writefield(field, value=None):
810 t = metafieldtype(field)
812 value = ','.join(value)
813 mf.write("%s:%s\n" % (field, value))
815 def writefield_nonempty(field, value=None):
819 writefield(field, value)
822 writefield_nonempty('Disabled')
823 writefield_nonempty('AntiFeatures')
824 writefield_nonempty('Provides')
825 writefield('Categories')
826 writefield('License')
827 writefield('Web Site')
828 writefield('Source Code')
829 writefield('Issue Tracker')
830 writefield('Changelog')
831 writefield_nonempty('Donate')
832 writefield_nonempty('FlattrID')
833 writefield_nonempty('Bitcoin')
834 writefield_nonempty('Litecoin')
835 writefield_nonempty('Dogecoin')
837 writefield_nonempty('Name')
838 writefield_nonempty('Auto Name')
839 writefield('Summary')
840 writefield('Description', '')
841 for line in app['Description']:
842 mf.write("%s\n" % line)
845 if app['Requires Root']:
846 writefield('Requires Root', 'Yes')
849 writefield('Repo Type')
852 writefield('Binaries')
854 for build in app['builds']:
856 if build['version'] == "Ignore":
859 writecomments('build:' + build['vercode'])
860 mf.write("Build:%s,%s\n" % (build['version'], build['vercode']))
862 def write_builditem(key, value):
864 if key in ['version', 'vercode']:
867 if value == flag_defaults[key]:
872 logging.debug("...writing {0} : {1}".format(key, value))
873 outline = ' %s=' % key
880 outline += '&& \\\n '.join([s.lstrip() for s in value.split('&& ')])
882 outline += ','.join(value) if type(value) == list else value
887 for flag in flag_defaults:
890 write_builditem(flag, value)
893 if app['Maintainer Notes']:
894 writefield('Maintainer Notes', '')
895 for line in app['Maintainer Notes']:
896 mf.write("%s\n" % line)
900 writefield_nonempty('Archive Policy')
901 writefield('Auto Update Mode')
902 writefield('Update Check Mode')
903 writefield_nonempty('Update Check Ignore')
904 writefield_nonempty('Vercode Operation')
905 writefield_nonempty('Update Check Name')
906 writefield_nonempty('Update Check Data')
907 if app['Current Version']:
908 writefield('Current Version')
909 writefield('Current Version Code')
911 if app['No Source Since']:
912 writefield('No Source Since')