2 # -*- coding: utf-8 -*-
4 # checkupdates.py - part of the FDroid server tools
5 # Copyright (C) 2010-2015, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013-2014 Daniel Martà <mvdan@mvdan.cc>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 from argparse import ArgumentParser
30 from distutils.version import LooseVersion
35 from common import VCSException, FDroidException
36 from metadata import MetaDataException
39 # Check for a new version by looking at a document retrieved via HTTP.
40 # The app's Update Check Data field is used to provide the information
46 if 'Update Check Data' not in app:
47 raise FDroidException('Missing Update Check Data')
49 urlcode, codeex, urlver, verex = app['Update Check Data'].split('|')
53 logging.debug("...requesting {0}".format(urlcode))
54 req = urllib2.Request(urlcode, None)
55 resp = urllib2.urlopen(req, None, 20)
58 m = re.search(codeex, page)
60 raise FDroidException("No RE match for version code")
66 logging.debug("...requesting {0}".format(urlver))
67 req = urllib2.Request(urlver, None)
68 resp = urllib2.urlopen(req, None, 20)
71 m = re.search(verex, page)
73 raise FDroidException("No RE match for version")
76 return (version, vercode)
78 except FDroidException:
79 msg = "Could not complete http check for app {0} due to unknown error: {1}".format(app['id'], traceback.format_exc())
83 def app_matches_packagename(app, package):
86 appid = app['Update Check Name'] or app['id']
89 return appid == package
92 # Check for a new version by looking at the tags in the source repo.
93 # Whether this can be used reliably or not depends on
94 # the development procedures used by the project's developers. Use it with
95 # caution, because it's inappropriate for many projects.
96 # Returns (None, "a message") if this didn't work, or (version, vercode, tag) for
97 # the details of the current version.
98 def check_tags(app, pattern):
102 if app['Repo Type'] == 'srclib':
103 build_dir = os.path.join('build', 'srclib', app['Repo'])
104 repotype = common.getsrclibvcs(app['Repo'])
106 build_dir = os.path.join('build', app['id'])
107 repotype = app['Repo Type']
109 if repotype not in ('git', 'git-svn', 'hg', 'bzr'):
110 return (None, 'Tags update mode only works for git, hg, bzr and git-svn repositories currently', None)
112 if repotype == 'git-svn' and ';' not in app['Repo']:
113 return (None, 'Tags update mode used in git-svn, but the repo was not set up with tags', None)
115 # Set up vcs interface and make sure we have the latest code...
116 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
118 vcs.gotorevision(None)
121 if len(app['builds']) > 0:
122 if app['builds'][-1]['gradle']:
123 flavours = app['builds'][-1]['gradle']
132 return (None, "No tags found", None)
134 logging.debug("All tags: " + ','.join(tags))
136 pat = re.compile(pattern)
137 tags = [tag for tag in tags if pat.match(tag)]
139 return (None, "No matching tags found", None)
140 logging.debug("Matching tags: " + ','.join(tags))
142 if len(tags) > 5 and repotype in ('git',):
143 tags = vcs.latesttags(tags, 5)
144 logging.debug("Latest tags: " + ','.join(tags))
147 logging.debug("Check tag: '{0}'".format(tag))
148 vcs.gotorevision(tag)
150 for subdir in possible_subdirs(app):
154 root_dir = os.path.join(build_dir, subdir)
155 paths = common.manifest_paths(root_dir, flavours)
156 version, vercode, package = \
157 common.parse_androidmanifests(paths, app['Update Check Ignore'])
159 logging.debug("Manifest exists in subdir '{0}'. Found version {1} ({2})"
160 .format(subdir, version, vercode))
161 if int(vercode) > int(hcode):
164 hcode = str(int(vercode))
168 return (None, "Couldn't find package ID", None)
170 return (hver, hcode, htag)
171 return (None, "Couldn't find any version information", None)
173 except VCSException as vcse:
174 msg = "VCS error while scanning app {0}: {1}".format(app['id'], vcse)
175 return (None, msg, None)
177 msg = "Could not scan app {0} due to unknown error: {1}".format(app['id'], traceback.format_exc())
178 return (None, msg, None)
181 # Check for a new version by looking at the AndroidManifest.xml at the HEAD
182 # of the source repo. Whether this can be used reliably or not depends on
183 # the development procedures used by the project's developers. Use it with
184 # caution, because it's inappropriate for many projects.
185 # Returns (None, "a message") if this didn't work, or (version, vercode) for
186 # the details of the current version.
187 def check_repomanifest(app, branch=None):
191 if app['Repo Type'] == 'srclib':
192 build_dir = os.path.join('build', 'srclib', app['Repo'])
193 repotype = common.getsrclibvcs(app['Repo'])
195 build_dir = os.path.join('build', app['id'])
196 repotype = app['Repo Type']
198 # Set up vcs interface and make sure we have the latest code...
199 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
201 if repotype == 'git':
203 branch = 'origin/' + branch
204 vcs.gotorevision(branch)
205 elif repotype == 'git-svn':
206 vcs.gotorevision(branch)
207 elif repotype == 'hg':
208 vcs.gotorevision(branch)
209 elif repotype == 'bzr':
210 vcs.gotorevision(None)
213 if len(app['builds']) > 0:
214 if app['builds'][-1]['gradle']:
215 flavours = app['builds'][-1]['gradle']
220 for subdir in possible_subdirs(app):
224 root_dir = os.path.join(build_dir, subdir)
225 paths = common.manifest_paths(root_dir, flavours)
226 version, vercode, package = \
227 common.parse_androidmanifests(paths, app['Update Check Ignore'])
229 logging.debug("Manifest exists in subdir '{0}'. Found version {1} ({2})"
230 .format(subdir, version, vercode))
231 if int(vercode) > int(hcode):
233 hcode = str(int(vercode))
237 return (None, "Couldn't find package ID")
240 return (None, "Couldn't find any version information")
242 except VCSException as vcse:
243 msg = "VCS error while scanning app {0}: {1}".format(app['id'], vcse)
246 msg = "Could not scan app {0} due to unknown error: {1}".format(app['id'], traceback.format_exc())
250 def check_repotrunk(app, branch=None):
253 if app['Repo Type'] == 'srclib':
254 build_dir = os.path.join('build', 'srclib', app['Repo'])
255 repotype = common.getsrclibvcs(app['Repo'])
257 build_dir = os.path.join('build', app['id'])
258 repotype = app['Repo Type']
260 if repotype not in ('git-svn', ):
261 return (None, 'RepoTrunk update mode only makes sense in git-svn repositories')
263 # Set up vcs interface and make sure we have the latest code...
264 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
266 vcs.gotorevision(None)
270 except VCSException as vcse:
271 msg = "VCS error while scanning app {0}: {1}".format(app['id'], vcse)
274 msg = "Could not scan app {0} due to unknown error: {1}".format(app['id'], traceback.format_exc())
278 # Check for a new version by looking at the Google Play Store.
279 # Returns (None, "a message") if this didn't work, or (version, None) for
280 # the details of the current version.
281 def check_gplay(app):
283 url = 'https://play.google.com/store/apps/details?id=' + app['id']
284 headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0'}
285 req = urllib2.Request(url, None, headers)
287 resp = urllib2.urlopen(req, None, 20)
289 except urllib2.HTTPError, e:
290 return (None, str(e.code))
292 return (None, 'Failed:' + str(e))
296 m = re.search('itemprop="softwareVersion">[ ]*([^<]+)[ ]*</div>', page)
298 html_parser = HTMLParser.HTMLParser()
299 version = html_parser.unescape(m.group(1))
301 if version == 'Varies with device':
302 return (None, 'Device-variable version, cannot use this method')
305 return (None, "Couldn't find version")
306 return (version.strip(), None)
309 # Return all directories under startdir that contain any of the manifest
310 # files, and thus are probably an Android project.
311 def dirs_with_manifest(startdir):
312 for r, d, f in os.walk(startdir):
313 if any(m in f for m in [
314 'AndroidManifest.xml', 'pom.xml', 'build.gradle']):
318 # Tries to find a new subdir starting from the root build_dir. Returns said
319 # subdir relative to the build dir if found, None otherwise.
320 def possible_subdirs(app):
322 if app['Repo Type'] == 'srclib':
323 build_dir = os.path.join('build', 'srclib', app['Repo'])
325 build_dir = os.path.join('build', app['id'])
328 if len(app['builds']) > 0:
329 build = app['builds'][-1]
331 flavours = build['gradle']
333 for d in dirs_with_manifest(build_dir):
334 m_paths = common.manifest_paths(d, flavours)
335 package = common.parse_androidmanifests(m_paths, app['Update Check Ignore'])[2]
336 if app_matches_packagename(app, package):
337 subdir = os.path.relpath(d, build_dir)
338 logging.debug("Adding possible subdir %s" % subdir)
342 def fetch_autoname(app, tag):
344 if not app["Repo Type"] or app['Update Check Mode'] in ('None', 'Static'):
347 if app['Repo Type'] == 'srclib':
348 app_dir = os.path.join('build', 'srclib', app['Repo'])
350 app_dir = os.path.join('build', app['id'])
353 vcs = common.getvcs(app["Repo Type"], app["Repo"], app_dir)
354 vcs.gotorevision(tag)
359 if len(app['builds']) > 0:
360 if app['builds'][-1]['subdir']:
361 app_dir = os.path.join(app_dir, app['builds'][-1]['subdir'])
362 if app['builds'][-1]['gradle']:
363 flavours = app['builds'][-1]['gradle']
365 logging.debug("...fetch auto name from " + app_dir)
366 new_name = common.fetch_real_name(app_dir, flavours)
369 logging.debug("...got autoname '" + new_name + "'")
370 if new_name != app['Auto Name']:
371 app['Auto Name'] = new_name
373 commitmsg = "Set autoname of {0}".format(common.getappname(app))
375 logging.debug("...couldn't get autoname")
380 def checkupdates_app(app, first=True):
382 # If a change is made, commitmsg should be set to a description of it.
383 # Only if this is set will changes be written back to the metadata.
390 mode = app['Update Check Mode']
391 if mode.startswith('Tags'):
392 pattern = mode[5:] if len(mode) > 4 else None
393 (version, vercode, tag) = check_tags(app, pattern)
395 elif mode == 'RepoManifest':
396 (version, vercode) = check_repomanifest(app)
398 elif mode.startswith('RepoManifest/'):
400 (version, vercode) = check_repomanifest(app, tag)
402 elif mode == 'RepoTrunk':
403 (version, vercode) = check_repotrunk(app)
406 (version, vercode) = check_http(app)
408 elif mode in ('None', 'Static'):
410 msg = 'Checking disabled'
414 msg = 'Invalid update check method'
416 if version and vercode and app['Vercode Operation']:
417 oldvercode = str(int(vercode))
418 op = app['Vercode Operation'].replace("%c", oldvercode)
419 vercode = str(eval(op))
420 logging.debug("Applied vercode operation: %s -> %s" % (oldvercode, vercode))
422 if version and any(version.startswith(s) for s in [
423 '${', # Gradle variable names
424 '@string/', # Strings we could not resolve
430 logmsg = "...{0} : {1}".format(app['id'], msg)
435 elif vercode == app['Current Version Code']:
436 logging.info("...up to date")
438 app['Current Version'] = version
439 app['Current Version Code'] = str(int(vercode))
442 commitmsg = fetch_autoname(app, tag)
445 name = common.getappname(app)
446 ver = common.getcvname(app)
447 logging.info('...updating to version %s' % ver)
448 commitmsg = 'Update CV of %s to %s' % (name, ver)
451 mode = app['Auto Update Mode']
452 if mode in ('None', 'Static'):
454 elif mode.startswith('Version '):
456 if pattern.startswith('+'):
458 suffix, pattern = pattern.split(' ', 1)
460 raise MetaDataException("Invalid AUM: " + mode)
465 for build in app['builds']:
466 if int(build['vercode']) >= int(app['Current Version Code']):
468 if not latest or int(build['vercode']) > int(latest['vercode']):
471 if int(latest['vercode']) > int(app['Current Version Code']):
472 logging.info("Refusing to auto update, since the latest build is newer")
475 newbuild = latest.copy()
476 if 'origlines' in newbuild:
477 del newbuild['origlines']
478 newbuild['disable'] = False
479 newbuild['vercode'] = app['Current Version Code']
480 newbuild['version'] = app['Current Version'] + suffix
481 logging.info("...auto-generating build for " + newbuild['version'])
482 commit = pattern.replace('%v', newbuild['version'])
483 commit = commit.replace('%c', newbuild['vercode'])
484 newbuild['commit'] = commit
485 app['builds'].append(newbuild)
486 name = common.getappname(app)
487 ver = common.getcvname(app)
488 commitmsg = "Update %s to %s" % (name, ver)
490 logging.warn('Invalid auto update mode "' + mode + '" on ' + app['id'])
493 metadatapath = os.path.join('metadata', app['id'] + '.txt')
494 with open(metadatapath, 'w') as f:
495 metadata.write_metadata('txt', f, app)
497 logging.info("Commiting update for " + metadatapath)
498 gitcmd = ["git", "commit", "-m", commitmsg]
499 if 'auto_author' in config:
500 gitcmd.extend(['--author', config['auto_author']])
501 gitcmd.extend(["--", metadatapath])
502 if subprocess.call(gitcmd) != 0:
503 logging.error("Git commit failed")
513 global config, options
515 # Parse command line...
516 parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
517 common.setup_global_opts(parser)
518 parser.add_argument("appid", nargs='*', help="app-id to check for updates")
519 parser.add_argument("--auto", action="store_true", default=False,
520 help="Process auto-updates")
521 parser.add_argument("--autoonly", action="store_true", default=False,
522 help="Only process apps with auto-updates")
523 parser.add_argument("--commit", action="store_true", default=False,
524 help="Commit changes")
525 parser.add_argument("--gplay", action="store_true", default=False,
526 help="Only print differences with the Play Store")
527 options = parser.parse_args()
529 config = common.read_config(options)
532 allapps = metadata.read_metadata()
534 apps = common.read_app_args(options.appid, allapps, False)
538 version, reason = check_gplay(app)
541 logging.info("{0} is not in the Play Store".format(common.getappname(app)))
543 logging.info("{0} encountered a problem: {1}".format(common.getappname(app), reason))
544 if version is not None:
545 stored = app['Current Version']
547 logging.info("{0} has no Current Version but has version {1} on the Play Store"
548 .format(common.getappname(app), version))
549 elif LooseVersion(stored) < LooseVersion(version):
550 logging.info("{0} has version {1} on the Play Store, which is bigger than {2}"
551 .format(common.getappname(app), version, stored))
553 if stored != version:
554 logging.info("{0} has version {1} on the Play Store, which differs from {2}"
555 .format(common.getappname(app), version, stored))
557 logging.info("{0} has the same version {1} on the Play Store"
558 .format(common.getappname(app), version))
561 for appid, app in apps.iteritems():
563 if options.autoonly and app['Auto Update Mode'] in ('None', 'Static'):
564 logging.debug("Nothing to do for {0}...".format(appid))
567 logging.info("Processing " + appid + '...')
569 checkupdates_app(app)
571 logging.info("Finished.")
573 if __name__ == "__main__":