2 # -*- coding: utf-8 -*-
4 # checkupdates.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013 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 optparse import OptionParser
30 from distutils.version import LooseVersion
31 import common, metadata
32 from common import BuildException
33 from common import VCSException
34 from metadata import MetaDataException
37 # Check for a new version by looking at a document retrieved via HTTP.
38 # The app's Update Check Data field is used to provide the information
44 if not 'Update Check Data' in app:
45 raise Exception('Missing Update Check Data')
47 urlcode, codeex, urlver, verex = app['Update Check Data'].split('|')
51 print "...requesting {0}".format(urlcode)
52 req = urllib2.Request(urlcode, None)
53 resp = urllib2.urlopen(req, None, 20)
56 m = re.search(codeex, page)
58 raise Exception("No RE match for version code")
64 print "...requesting {0}".format(urlver)
65 req = urllib2.Request(urlver, None)
66 resp = urllib2.urlopen(req, None, 20)
69 m = re.search(verex, page)
71 raise Exception("No RE match for version")
74 return (version, vercode)
77 msg = "Could not complete http check for app %s due to unknown error: %s" % (app['id'], traceback.format_exc())
80 # Check for a new version by looking at the tags in the source repo.
81 # Whether this can be used reliably or not depends on
82 # the development procedures used by the project's developers. Use it with
83 # caution, because it's inappropriate for many projects.
84 # Returns (None, "a message") if this didn't work, or (version, vercode) for
85 # the details of the current version.
90 if app['Repo Type'] == 'srclib':
91 build_dir = os.path.join('build', 'srclib', app['Repo'])
92 repotype = common.getsrclibvcs(app['Repo'])
94 build_dir = os.path.join('build/', app['id'])
95 repotype = app['Repo Type']
97 if repotype not in ('git', 'git-svn', 'hg', 'bzr'):
98 return (None, 'Tags update mode only works for git, hg, bzr and git-svn repositories currently', None)
100 # Set up vcs interface and make sure we have the latest code...
101 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
103 vcs.gotorevision(None)
106 if len(app['builds']) > 0:
107 if 'subdir' in app['builds'][-1]:
108 build_dir = os.path.join(build_dir, app['builds'][-1]['subdir'])
109 if 'gradle' in app['builds'][-1]:
110 flavour = app['builds'][-1]['gradle']
116 for tag in vcs.gettags():
117 vcs.gotorevision(tag)
119 # Only process tags where the manifest exists...
120 paths = common.manifest_paths(build_dir, flavour)
121 version, vercode, package = common.parse_androidmanifests(paths)
122 if package and package == app['id'] and version and vercode:
123 print "Manifest exists. Found version %s (%s)" % (
125 if int(vercode) > int(hcode):
127 hcode = str(int(vercode))
131 return (hver, hcode, htag)
132 return (None, "Couldn't find any version information", None)
134 except BuildException as be:
135 msg = "Could not scan app %s due to BuildException: %s" % (app['id'], be)
136 return (None, msg, None)
137 except VCSException as vcse:
138 msg = "VCS error while scanning app %s: %s" % (app['id'], vcse)
139 return (None, msg, None)
141 msg = "Could not scan app %s due to unknown error: %s" % (app['id'], traceback.format_exc())
142 return (None, msg, None)
144 # Check for a new version by looking at the AndroidManifest.xml at the HEAD
145 # of the source repo. Whether this can be used reliably or not depends on
146 # the development procedures used by the project's developers. Use it with
147 # caution, because it's inappropriate for many projects.
148 # Returns (None, "a message") if this didn't work, or (version, vercode) for
149 # the details of the current version.
150 def check_repomanifest(app, branch=None):
154 if app['Repo Type'] == 'srclib':
155 build_dir = os.path.join('build', 'srclib', app['Repo'])
156 repotype = common.getsrclibvcs(app['Repo'])
158 build_dir = os.path.join('build/', app['id'])
159 repotype = app['Repo Type']
161 # Set up vcs interface and make sure we have the latest code...
162 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
164 if repotype == 'git':
166 branch = 'origin/'+branch
167 vcs.gotorevision(branch)
168 elif repotype == 'git-svn':
169 vcs.gotorevision(branch)
170 elif repotype == 'svn':
171 vcs.gotorevision(None)
172 elif repotype == 'hg':
173 vcs.gotorevision(branch)
174 elif repotype == 'bzr':
175 vcs.gotorevision(None)
179 if len(app['builds']) > 0:
180 if 'subdir' in app['builds'][-1]:
181 build_dir = os.path.join(build_dir, app['builds'][-1]['subdir'])
182 if 'gradle' in app['builds'][-1]:
183 flavour = app['builds'][-1]['gradle']
185 if not os.path.isdir(build_dir):
186 return (None, "Subdir '" + app['builds'][-1]['subdir'] + "'is not a valid directory")
188 paths = common.manifest_paths(build_dir, flavour)
190 version, vercode, package = common.parse_androidmanifests(paths)
192 return (None, "Couldn't find package ID")
193 if package != app['id']:
194 return (None, "Package ID mismatch")
196 return (None,"Couldn't find latest version name")
198 return (None,"Couldn't find latest version code")
200 return (version, str(int(vercode)))
202 except BuildException as be:
203 msg = "Could not scan app %s due to BuildException: %s" % (app['id'], be)
205 except VCSException as vcse:
206 msg = "VCS error while scanning app %s: %s" % (app['id'], vcse)
209 msg = "Could not scan app %s due to unknown error: %s" % (app['id'], traceback.format_exc())
212 def check_repotrunk(app, branch=None):
215 if app['Repo Type'] == 'srclib':
216 build_dir = os.path.join('build', 'srclib', app['Repo'])
217 repotype = common.getsrclibvcs(app['Repo'])
219 build_dir = os.path.join('build/', app['id'])
220 repotype = app['Repo Type']
222 if repotype not in ('svn', 'git-svn'):
223 return (None, 'RepoTrunk update mode only makes sense in svn and git-svn repositories')
225 # Set up vcs interface and make sure we have the latest code...
226 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
228 vcs.gotorevision(None)
232 except BuildException as be:
233 msg = "Could not scan app %s due to BuildException: %s" % (app['id'], be)
235 except VCSException as vcse:
236 msg = "VCS error while scanning app %s: %s" % (app['id'], vcse)
239 msg = "Could not scan app %s due to unknown error: %s" % (app['id'], traceback.format_exc())
242 # Check for a new version by looking at the Google Play Store.
243 # Returns (None, "a message") if this didn't work, or (version, None) for
244 # the details of the current version.
245 def check_gplay(app):
247 url = 'https://play.google.com/store/apps/details?id=' + app['id']
248 headers = {'User-Agent' : 'Mozilla/5.0 (X11; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0'}
249 req = urllib2.Request(url, None, headers)
251 resp = urllib2.urlopen(req, None, 20)
253 except urllib2.HTTPError, e:
254 return (None, str(e.code))
256 return (None, 'Failed:' + str(e))
260 m = re.search('itemprop="softwareVersion">[ ]*([^<]+)[ ]*</div>', page)
262 html_parser = HTMLParser.HTMLParser()
263 version = html_parser.unescape(m.group(1))
265 if version == 'Varies with device':
266 return (None, 'Device-variable version, cannot use this method')
269 return (None, "Couldn't find version")
270 return (version.strip(), None)
278 global config, options
280 # Parse command line...
281 parser = OptionParser(usage="Usage: %prog [options] [APPID [APPID ...]]")
282 parser.add_option("-v", "--verbose", action="store_true", default=False,
283 help="Spew out even more information than normal")
284 parser.add_option("--auto", action="store_true", default=False,
285 help="Process auto-updates")
286 parser.add_option("--autoonly", action="store_true", default=False,
287 help="Only process apps with auto-updates")
288 parser.add_option("--commit", action="store_true", default=False,
289 help="Commit changes")
290 parser.add_option("--gplay", action="store_true", default=False,
291 help="Only print differences with the Play Store")
292 (options, args) = parser.parse_args()
294 config = common.read_config(options)
297 allapps = metadata.read_metadata(options.verbose)
299 apps = common.read_app_args(args, allapps, False)
303 version, reason = check_gplay(app)
304 if version is None and options.verbose:
306 print "%s is not in the Play Store" % common.getappname(app)
308 print "%s encountered a problem: %s" % common.getappname(app)
309 if version is not None:
310 stored = app['Current Version']
311 if LooseVersion(stored) < LooseVersion(version):
312 print "%s has version %s on the Play Store, which is bigger than %s" % (
313 common.getappname(app), version, stored)
314 elif options.verbose:
315 print "%s has the same version %s on the Play Store" % (
316 common.getappname(app), version)
322 if options.autoonly and app['Auto Update Mode'] == 'None':
324 print "Nothing to do for %s..." % app['id']
327 print "Processing " + app['id'] + '...'
335 mode = app['Update Check Mode']
337 (version, vercode, tag) = check_tags(app)
338 elif mode == 'RepoManifest':
339 (version, vercode) = check_repomanifest(app)
340 elif mode.startswith('RepoManifest/'):
342 (version, vercode) = check_repomanifest(app, tag)
343 elif mode == 'RepoTrunk':
344 (version, vercode) = check_repotrunk(app)
346 (version, vercode) = check_http(app)
347 elif mode == 'Static':
349 msg = 'Checking disabled'
352 msg = 'Checking disabled'
355 msg = 'Invalid update check method'
357 if vercode and app['Vercode Operation']:
358 op = app['Vercode Operation'].replace("%c", str(int(vercode)))
359 vercode = str(eval(op))
364 elif vercode == app['Current Version Code']:
365 print "...up to date"
367 app['Current Version'] = version
368 app['Current Version Code'] = str(int(vercode))
372 # Do the Auto Name thing as well as finding the CV real name
373 if len(app["Repo Type"]) > 0:
377 if app['Repo Type'] == 'srclib':
378 app_dir = os.path.join('build', 'srclib', app['Repo'])
380 app_dir = os.path.join('build/', app['id'])
382 vcs = common.getvcs(app["Repo Type"], app["Repo"], app_dir)
383 vcs.gotorevision(tag)
386 if len(app['builds']) > 0:
387 if 'subdir' in app['builds'][-1]:
388 app_dir = os.path.join(app_dir, app['builds'][-1]['subdir'])
389 if 'gradle' in app['builds'][-1]:
390 flavour = app['builds'][-1]['gradle']
392 new_name = common.fetch_real_name(app_dir, flavour)
393 if new_name != app['Auto Name']:
394 app['Auto Name'] = new_name
396 if app['Current Version'].startswith('@string/'):
397 cv = common.version_name(app['Current Version'], app_dir, flavour)
398 if app['Current Version'] != cv:
399 app['Current Version'] = cv
402 print "ERROR: Auto Name or Current Version failed for %s due to exception: %s" % (app['id'], traceback.format_exc())
405 name = common.getappname(app)
406 ver = common.getcvname(app)
407 print '...updating to version %s' % ver
408 logmsg = 'Update CV of %s to %s' % (name, ver)
411 mode = app['Auto Update Mode']
414 elif mode.startswith('Version '):
416 if pattern.startswith('+'):
418 suffix, pattern = pattern.split(' ', 1)
420 raise MetaDataException("Invalid AUM: " + mode)
425 for build in app['builds']:
426 if build['vercode'] == app['Current Version Code']:
428 if not latest or int(build['vercode']) > int(latest['vercode']):
431 newbuild = latest.copy()
432 if 'origlines' in newbuild:
433 del newbuild['origlines']
434 newbuild['vercode'] = app['Current Version Code']
435 newbuild['version'] = app['Current Version'] + suffix
436 print "...auto-generating build for " + newbuild['version']
437 commit = pattern.replace('%v', newbuild['version'])
438 commit = commit.replace('%c', newbuild['vercode'])
439 newbuild['commit'] = commit
440 app['builds'].append(newbuild)
442 name = common.getappname(app)
443 ver = common.getcvname(app)
444 logmsg = "Update %s to %s" % (name, ver)
446 print 'Invalid auto update mode "' + mode + '"'
449 metafile = os.path.join('metadata', app['id'] + '.txt')
450 metadata.write_metadata(metafile, app)
451 if options.commit and logmsg:
452 print "Commiting update for " + metafile
453 gitcmd = ["git", "commit", "-m",
455 if 'auto_author' in config:
456 gitcmd.extend(['--author', config['auto_author']])
457 gitcmd.extend(["--", metafile])
458 if subprocess.call(gitcmd) != 0:
459 print "Git commit failed"
464 if __name__ == "__main__":