3 # checkupdates.py - part of the FDroid server tools
4 # Copyright (C) 2010-2015, 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 argparse import ArgumentParser
29 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 not app.UpdateCheckData:
47 raise FDroidException('Missing Update Check Data')
49 urlcode, codeex, urlver, verex = app.UpdateCheckData.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 # Check for a new version by looking at the tags in the source repo.
84 # Whether this can be used reliably or not depends on
85 # the development procedures used by the project's developers. Use it with
86 # caution, because it's inappropriate for many projects.
87 # Returns (None, "a message") if this didn't work, or (version, vercode, tag) for
88 # the details of the current version.
89 def check_tags(app, pattern):
93 if app.RepoType == 'srclib':
94 build_dir = os.path.join('build', 'srclib', app.Repo)
95 repotype = common.getsrclibvcs(app.Repo)
97 build_dir = os.path.join('build', app.id)
98 repotype = app.RepoType
100 if repotype not in ('git', 'git-svn', 'hg', 'bzr'):
101 return (None, 'Tags update mode only works for git, hg, bzr and git-svn repositories currently', None)
103 if repotype == 'git-svn' and ';' not in app.Repo:
104 return (None, 'Tags update mode used in git-svn, but the repo was not set up with tags', None)
106 # Set up vcs interface and make sure we have the latest code...
107 vcs = common.getvcs(app.RepoType, app.Repo, build_dir)
109 vcs.gotorevision(None)
111 last_build = metadata.Build()
112 if len(app.builds) > 0:
113 last_build = app.builds[-1]
115 if last_build.submodules:
125 return (None, "No tags found", None)
127 logging.debug("All tags: " + ','.join(tags))
129 pat = re.compile(pattern)
130 tags = [tag for tag in tags if pat.match(tag)]
132 return (None, "No matching tags found", None)
133 logging.debug("Matching tags: " + ','.join(tags))
135 if len(tags) > 5 and repotype in ('git',):
136 tags = vcs.latesttags(tags, 5)
137 logging.debug("Latest tags: " + ','.join(tags))
140 logging.debug("Check tag: '{0}'".format(tag))
141 vcs.gotorevision(tag)
143 for subdir in possible_subdirs(app):
147 root_dir = os.path.join(build_dir, subdir)
148 paths = common.manifest_paths(root_dir, last_build.gradle)
149 version, vercode, package = common.parse_androidmanifests(paths, app)
151 logging.debug("Manifest exists in subdir '{0}'. Found version {1} ({2})"
152 .format(subdir, version, vercode))
153 if int(vercode) > int(hcode):
156 hcode = str(int(vercode))
160 return (None, "Couldn't find package ID", None)
162 return (hver, hcode, htag)
163 return (None, "Couldn't find any version information", None)
165 except VCSException as vcse:
166 msg = "VCS error while scanning app {0}: {1}".format(app.id, vcse)
167 return (None, msg, None)
169 msg = "Could not scan app {0} due to unknown error: {1}".format(app.id, traceback.format_exc())
170 return (None, msg, None)
173 # Check for a new version by looking at the AndroidManifest.xml at the HEAD
174 # of the source repo. Whether this can be used reliably or not depends on
175 # the development procedures used by the project's developers. Use it with
176 # caution, because it's inappropriate for many projects.
177 # Returns (None, "a message") if this didn't work, or (version, vercode) for
178 # the details of the current version.
179 def check_repomanifest(app, branch=None):
183 if app.RepoType == 'srclib':
184 build_dir = os.path.join('build', 'srclib', app.Repo)
185 repotype = common.getsrclibvcs(app.Repo)
187 build_dir = os.path.join('build', app.id)
188 repotype = app.RepoType
190 # Set up vcs interface and make sure we have the latest code...
191 vcs = common.getvcs(app.RepoType, app.Repo, build_dir)
193 if repotype == 'git':
195 branch = 'origin/' + branch
196 vcs.gotorevision(branch)
197 elif repotype == 'git-svn':
198 vcs.gotorevision(branch)
199 elif repotype == 'hg':
200 vcs.gotorevision(branch)
201 elif repotype == 'bzr':
202 vcs.gotorevision(None)
204 last_build = metadata.Build()
205 if len(app.builds) > 0:
206 last_build = app.builds[-1]
208 if last_build.submodules:
214 for subdir in possible_subdirs(app):
218 root_dir = os.path.join(build_dir, subdir)
219 paths = common.manifest_paths(root_dir, last_build.gradle)
220 version, vercode, package = common.parse_androidmanifests(paths, app)
222 logging.debug("Manifest exists in subdir '{0}'. Found version {1} ({2})"
223 .format(subdir, version, vercode))
224 if int(vercode) > int(hcode):
226 hcode = str(int(vercode))
230 return (None, "Couldn't find package ID")
233 return (None, "Couldn't find any version information")
235 except VCSException as vcse:
236 msg = "VCS error while scanning app {0}: {1}".format(app.id, vcse)
239 msg = "Could not scan app {0} due to unknown error: {1}".format(app.id, traceback.format_exc())
243 def check_repotrunk(app, branch=None):
246 if app.RepoType == 'srclib':
247 build_dir = os.path.join('build', 'srclib', app.Repo)
248 repotype = common.getsrclibvcs(app.Repo)
250 build_dir = os.path.join('build', app.id)
251 repotype = app.RepoType
253 if repotype not in ('git-svn', ):
254 return (None, 'RepoTrunk update mode only makes sense in git-svn repositories')
256 # Set up vcs interface and make sure we have the latest code...
257 vcs = common.getvcs(app.RepoType, app.Repo, build_dir)
259 vcs.gotorevision(None)
263 except VCSException as vcse:
264 msg = "VCS error while scanning app {0}: {1}".format(app.id, vcse)
267 msg = "Could not scan app {0} due to unknown error: {1}".format(app.id, traceback.format_exc())
271 # Check for a new version by looking at the Google Play Store.
272 # Returns (None, "a message") if this didn't work, or (version, None) for
273 # the details of the current version.
274 def check_gplay(app):
276 url = 'https://play.google.com/store/apps/details?id=' + app.id
277 headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0'}
278 req = urllib2.Request(url, None, headers)
280 resp = urllib2.urlopen(req, None, 20)
282 except urllib2.HTTPError as e:
283 return (None, str(e.code))
284 except Exception as e:
285 return (None, 'Failed:' + str(e))
289 m = re.search('itemprop="softwareVersion">[ ]*([^<]+)[ ]*</div>', page)
291 html_parser = HTMLParser.HTMLParser()
292 version = html_parser.unescape(m.group(1))
294 if version == 'Varies with device':
295 return (None, 'Device-variable version, cannot use this method')
298 return (None, "Couldn't find version")
299 return (version.strip(), None)
302 # Return all directories under startdir that contain any of the manifest
303 # files, and thus are probably an Android project.
304 def dirs_with_manifest(startdir):
305 for r, d, f in os.walk(startdir):
306 if any(m in f for m in [
307 'AndroidManifest.xml', 'pom.xml', 'build.gradle']):
311 # Tries to find a new subdir starting from the root build_dir. Returns said
312 # subdir relative to the build dir if found, None otherwise.
313 def possible_subdirs(app):
315 if app.RepoType == 'srclib':
316 build_dir = os.path.join('build', 'srclib', app.Repo)
318 build_dir = os.path.join('build', app.id)
320 last_build = metadata.Build()
321 if len(app.builds) > 0:
322 last_build = app.builds[-1]
324 for d in dirs_with_manifest(build_dir):
325 m_paths = common.manifest_paths(d, last_build.gradle)
326 package = common.parse_androidmanifests(m_paths, app)[2]
327 if package is not None:
328 subdir = os.path.relpath(d, build_dir)
329 logging.debug("Adding possible subdir %s" % subdir)
333 def fetch_autoname(app, tag):
335 if not app.RepoType or app.UpdateCheckMode in ('None', 'Static'):
338 if app.RepoType == 'srclib':
339 build_dir = os.path.join('build', 'srclib', app.Repo)
341 build_dir = os.path.join('build', app.id)
344 vcs = common.getvcs(app.RepoType, app.Repo, build_dir)
345 vcs.gotorevision(tag)
349 last_build = metadata.Build()
350 if len(app.builds) > 0:
351 last_build = app.builds[-1]
353 logging.debug("...fetch auto name from " + build_dir)
355 for subdir in possible_subdirs(app):
359 root_dir = os.path.join(build_dir, subdir)
360 new_name = common.fetch_real_name(root_dir, last_build.gradle)
361 if new_name is not None:
365 logging.debug("...got autoname '" + new_name + "'")
366 if new_name != app.AutoName:
367 app.AutoName = new_name
369 commitmsg = "Set autoname of {0}".format(common.getappname(app))
371 logging.debug("...couldn't get autoname")
376 def checkupdates_app(app, first=True):
378 # If a change is made, commitmsg should be set to a description of it.
379 # Only if this is set will changes be written back to the metadata.
386 mode = app.UpdateCheckMode
387 if mode.startswith('Tags'):
388 pattern = mode[5:] if len(mode) > 4 else None
389 (version, vercode, tag) = check_tags(app, pattern)
390 if version == 'Unknown':
393 elif mode == 'RepoManifest':
394 (version, vercode) = check_repomanifest(app)
396 elif mode.startswith('RepoManifest/'):
398 (version, vercode) = check_repomanifest(app, tag)
400 elif mode == 'RepoTrunk':
401 (version, vercode) = check_repotrunk(app)
404 (version, vercode) = check_http(app)
406 elif mode in ('None', 'Static'):
408 msg = 'Checking disabled'
412 msg = 'Invalid update check method'
414 if version and vercode and app.VercodeOperation:
415 oldvercode = str(int(vercode))
416 op = app.VercodeOperation.replace("%c", oldvercode)
417 vercode = str(eval(op))
418 logging.debug("Applied vercode operation: %s -> %s" % (oldvercode, vercode))
420 if version and any(version.startswith(s) for s in [
421 '${', # Gradle variable names
422 '@string/', # Strings we could not resolve
428 logmsg = "...{0} : {1}".format(app.id, msg)
433 elif vercode == app.CurrentVersionCode:
434 logging.info("...up to date")
436 app.CurrentVersion = version
437 app.CurrentVersionCode = str(int(vercode))
440 commitmsg = fetch_autoname(app, tag)
443 name = common.getappname(app)
444 ver = common.getcvname(app)
445 logging.info('...updating to version %s' % ver)
446 commitmsg = 'Update CV of %s to %s' % (name, ver)
449 mode = app.AutoUpdateMode
450 if mode in ('None', 'Static'):
452 elif mode.startswith('Version '):
454 if pattern.startswith('+'):
456 suffix, pattern = pattern.split(' ', 1)
458 raise MetaDataException("Invalid AUM: " + mode)
463 for build in app.builds:
464 if int(build.vercode) >= int(app.CurrentVersionCode):
466 if not latest or int(build.vercode) > int(latest.vercode):
469 if int(latest.vercode) > int(app.CurrentVersionCode):
470 logging.info("Refusing to auto update, since the latest build is newer")
473 newbuild = copy.deepcopy(latest)
474 newbuild.disable = False
475 newbuild.vercode = app.CurrentVersionCode
476 newbuild.version = app.CurrentVersion + suffix
477 logging.info("...auto-generating build for " + newbuild.version)
478 commit = pattern.replace('%v', newbuild.version)
479 commit = commit.replace('%c', newbuild.vercode)
480 newbuild.commit = commit
481 app.builds.append(newbuild)
482 name = common.getappname(app)
483 ver = common.getcvname(app)
484 commitmsg = "Update %s to %s" % (name, ver)
486 logging.warn('Invalid auto update mode "' + mode + '" on ' + app.id)
489 metadatapath = os.path.join('metadata', app.id + '.txt')
490 with open(metadatapath, 'w') as f:
491 metadata.write_metadata('txt', f, app)
493 logging.info("Commiting update for " + metadatapath)
494 gitcmd = ["git", "commit", "-m", commitmsg]
495 if 'auto_author' in config:
496 gitcmd.extend(['--author', config['auto_author']])
497 gitcmd.extend(["--", metadatapath])
498 if subprocess.call(gitcmd) != 0:
499 logging.error("Git commit failed")
509 global config, options
511 # Parse command line...
512 parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
513 common.setup_global_opts(parser)
514 parser.add_argument("appid", nargs='*', help="app-id to check for updates")
515 parser.add_argument("--auto", action="store_true", default=False,
516 help="Process auto-updates")
517 parser.add_argument("--autoonly", action="store_true", default=False,
518 help="Only process apps with auto-updates")
519 parser.add_argument("--commit", action="store_true", default=False,
520 help="Commit changes")
521 parser.add_argument("--gplay", action="store_true", default=False,
522 help="Only print differences with the Play Store")
523 options = parser.parse_args()
525 config = common.read_config(options)
528 allapps = metadata.read_metadata()
530 apps = common.read_app_args(options.appid, allapps, False)
534 version, reason = check_gplay(app)
537 logging.info("{0} is not in the Play Store".format(common.getappname(app)))
539 logging.info("{0} encountered a problem: {1}".format(common.getappname(app), reason))
540 if version is not None:
541 stored = app.CurrentVersion
543 logging.info("{0} has no Current Version but has version {1} on the Play Store"
544 .format(common.getappname(app), version))
545 elif LooseVersion(stored) < LooseVersion(version):
546 logging.info("{0} has version {1} on the Play Store, which is bigger than {2}"
547 .format(common.getappname(app), version, stored))
549 if stored != version:
550 logging.info("{0} has version {1} on the Play Store, which differs from {2}"
551 .format(common.getappname(app), version, stored))
553 logging.info("{0} has the same version {1} on the Play Store"
554 .format(common.getappname(app), version))
557 for appid, app in apps.iteritems():
559 if options.autoonly and app.AutoUpdateMode in ('None', 'Static'):
560 logging.debug("Nothing to do for {0}...".format(appid))
563 logging.info("Processing " + appid + '...')
565 checkupdates_app(app)
567 logging.info("Finished.")
569 if __name__ == "__main__":