1 # -*- coding: utf-8 -*-
3 # common.py - part of the FDroid server tools
4 # Copyright (C) 2010-13, 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/>.
40 def get_default_config():
42 'sdk_path': os.getenv("ANDROID_HOME"),
43 'ndk_path': "$ANDROID_NDK",
44 'build_tools': "19.0.3",
49 'update_stats': False,
50 'stats_to_carbon': False,
52 'build_server_always': False,
53 'keystore': '$HOME/.local/share/fdroidserver/keystore.jks',
54 'smartcardoptions': [],
63 def read_config(opts, config_file='config.py'):
64 """Read the repository config
66 The config is read from config_file, which is in the current directory when
67 any of the repo management commands are used.
69 global config, options
71 if config is not None:
73 if not os.path.isfile(config_file):
74 logging.critical("Missing config file - is this a repo directory?")
81 logging.debug("Reading %s" % config_file)
82 execfile(config_file, config)
84 # smartcardoptions must be a list since its command line args for Popen
85 if 'smartcardoptions' in config:
86 config['smartcardoptions'] = config['smartcardoptions'].split(' ')
87 elif 'keystore' in config and config['keystore'] == 'NONE':
88 # keystore='NONE' means use smartcard, these are required defaults
89 config['smartcardoptions'] = ['-storetype', 'PKCS11', '-providerName',
90 'SunPKCS11-OpenSC', '-providerClass',
91 'sun.security.pkcs11.SunPKCS11',
92 '-providerArg', 'opensc-fdroid.cfg']
94 defconfig = get_default_config()
95 for k, v in defconfig.items():
99 # Expand environment variables
100 for k, v in config.items():
103 v = os.path.expanduser(v)
104 config[k] = os.path.expandvars(v)
106 if not test_sdk_exists(config):
109 if any(k in config for k in ["keystore", "keystorepass", "keypass"]):
110 st = os.stat(config_file)
111 if st.st_mode & stat.S_IRWXG or st.st_mode & stat.S_IRWXO:
112 logging.warn("unsafe permissions on {0} (should be 0600)!".format(config_file))
114 for k in ["keystorepass", "keypass"]:
116 write_password_file(k)
118 # since this is used with rsync, where trailing slashes have meaning,
119 # ensure there is always a trailing slash
120 if 'serverwebroot' in config:
121 if config['serverwebroot'][-1] != '/':
122 config['serverwebroot'] += '/'
123 config['serverwebroot'] = config['serverwebroot'].replace('//', '/')
128 def test_sdk_exists(c):
129 if c['sdk_path'] is None:
130 # c['sdk_path'] is set to the value of ANDROID_HOME by default
131 logging.critical('No Android SDK found! ANDROID_HOME is not set and sdk_path is not in config.py!')
132 logging.info('You can use ANDROID_HOME to set the path to your SDK, i.e.:')
133 logging.info('\texport ANDROID_HOME=/opt/android-sdk')
135 if not os.path.exists(c['sdk_path']):
136 logging.critical('Android SDK path "' + c['sdk_path'] + '" does not exist!')
138 if not os.path.isdir(c['sdk_path']):
139 logging.critical('Android SDK path "' + c['sdk_path'] + '" is not a directory!')
141 if not os.path.isdir(os.path.join(c['sdk_path'], 'build-tools')):
142 logging.critical('Android SDK path "' + c['sdk_path'] + '" does not contain "build-tools/"!')
147 def test_build_tools_exists(c):
148 if not test_sdk_exists(c):
150 build_tools = os.path.join(c['sdk_path'], 'build-tools')
151 versioned_build_tools = os.path.join(build_tools, c['build_tools'])
152 if not os.path.isdir(versioned_build_tools):
153 logging.critical('Android Build Tools path "'
154 + versioned_build_tools + '" does not exist!')
156 if not os.path.exists(os.path.join(c['sdk_path'], 'build-tools', c['build_tools'], 'aapt')):
157 logging.critical('Android Build Tools "'
158 + versioned_build_tools
159 + '" does not contain "aapt"!')
164 def write_password_file(pwtype, password=None):
166 writes out passwords to a protected file instead of passing passwords as
167 command line argments
169 filename = '.fdroid.' + pwtype + '.txt'
170 fd = os.open(filename, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0600)
172 os.write(fd, config[pwtype])
174 os.write(fd, password)
176 config[pwtype + 'file'] = filename
179 # Given the arguments in the form of multiple appid:[vc] strings, this returns
180 # a dictionary with the set of vercodes specified for each package.
181 def read_pkg_args(args, allow_vercodes=False):
188 if allow_vercodes and ':' in p:
189 package, vercode = p.split(':')
191 package, vercode = p, None
192 if package not in vercodes:
193 vercodes[package] = [vercode] if vercode else []
195 elif vercode and vercode not in vercodes[package]:
196 vercodes[package] += [vercode] if vercode else []
201 # On top of what read_pkg_args does, this returns the whole app metadata, but
202 # limiting the builds list to the builds matching the vercodes specified.
203 def read_app_args(args, allapps, allow_vercodes=False):
205 vercodes = read_pkg_args(args, allow_vercodes)
210 apps = [app for app in allapps if app['id'] in vercodes]
212 if len(apps) != len(vercodes):
213 allids = [app["id"] for app in allapps]
216 logging.critical("No such package: %s" % p)
217 raise Exception("Found invalid app ids in arguments")
219 raise Exception("No packages specified")
223 vc = vercodes[app['id']]
226 app['builds'] = [b for b in app['builds'] if b['vercode'] in vc]
227 if len(app['builds']) != len(vercodes[app['id']]):
229 allvcs = [b['vercode'] for b in app['builds']]
230 for v in vercodes[app['id']]:
232 logging.critical("No such vercode %s for app %s" % (v, app['id']))
235 raise Exception("Found invalid vercodes for some apps")
240 def has_extension(filename, extension):
241 name, ext = os.path.splitext(filename)
242 ext = ext.lower()[1:]
243 return ext == extension
248 def apknameinfo(filename):
250 filename = os.path.basename(filename)
251 if apk_regex is None:
252 apk_regex = re.compile(r"^(.+)_([0-9]+)\.apk$")
253 m = apk_regex.match(filename)
255 result = (m.group(1), m.group(2))
256 except AttributeError:
257 raise Exception("Invalid apk name: %s" % filename)
261 def getapkname(app, build):
262 return "%s_%s.apk" % (app['id'], build['vercode'])
265 def getsrcname(app, build):
266 return "%s_%s_src.tar.gz" % (app['id'], build['vercode'])
273 return app['Auto Name']
278 return '%s (%s)' % (app['Current Version'], app['Current Version Code'])
281 def getvcs(vcstype, remote, local):
283 return vcs_git(remote, local)
285 return vcs_svn(remote, local)
286 if vcstype == 'git-svn':
287 return vcs_gitsvn(remote, local)
289 return vcs_hg(remote, local)
291 return vcs_bzr(remote, local)
292 if vcstype == 'srclib':
293 if local != 'build/srclib/' + remote:
294 raise VCSException("Error: srclib paths are hard-coded!")
295 return getsrclib(remote, 'build/srclib', raw=True)
296 raise VCSException("Invalid vcs type " + vcstype)
299 def getsrclibvcs(name):
300 if name not in metadata.srclibs:
301 raise VCSException("Missing srclib " + name)
302 return metadata.srclibs[name]['Repo Type']
306 def __init__(self, remote, local):
308 # svn, git-svn and bzr may require auth
310 if self.repotype() in ('svn', 'git-svn', 'bzr'):
312 self.username, remote = remote.split('@')
313 if ':' not in self.username:
314 raise VCSException("Password required with username")
315 self.username, self.password = self.username.split(':')
319 self.refreshed = False
325 # Take the local repository to a clean version of the given revision, which
326 # is specificed in the VCS's native format. Beforehand, the repository can
327 # be dirty, or even non-existent. If the repository does already exist
328 # locally, it will be updated from the origin, but only once in the
329 # lifetime of the vcs object.
330 # None is acceptable for 'rev' if you know you are cloning a clean copy of
331 # the repo - otherwise it must specify a valid revision.
332 def gotorevision(self, rev):
334 # The .fdroidvcs-id file for a repo tells us what VCS type
335 # and remote that directory was created from, allowing us to drop it
336 # automatically if either of those things changes.
337 fdpath = os.path.join(self.local, '..',
338 '.fdroidvcs-' + os.path.basename(self.local))
339 cdata = self.repotype() + ' ' + self.remote
342 if os.path.exists(self.local):
343 if os.path.exists(fdpath):
344 with open(fdpath, 'r') as f:
345 fsdata = f.read().strip()
350 logging.info("Repository details changed - deleting")
353 logging.info("Repository details missing - deleting")
355 shutil.rmtree(self.local)
357 self.gotorevisionx(rev)
359 # If necessary, write the .fdroidvcs file.
361 with open(fdpath, 'w') as f:
364 # Derived classes need to implement this. It's called once basic checking
365 # has been performend.
366 def gotorevisionx(self, rev):
367 raise VCSException("This VCS type doesn't define gotorevisionx")
369 # Initialise and update submodules
370 def initsubmodules(self):
371 raise VCSException('Submodules not supported for this vcs type')
373 # Get a list of all known tags
375 raise VCSException('gettags not supported for this vcs type')
377 # Get a list of latest number tags
378 def latesttags(self, number):
379 raise VCSException('latesttags not supported for this vcs type')
381 # Get current commit reference (hash, revision, etc)
383 raise VCSException('getref not supported for this vcs type')
385 # Returns the srclib (name, path) used in setting up the current
396 # If the local directory exists, but is somehow not a git repository, git
397 # will traverse up the directory tree until it finds one that is (i.e.
398 # fdroidserver) and then we'll proceed to destroy it! This is called as
401 p = SilentPopen(['git', 'rev-parse', '--show-toplevel'], cwd=self.local)
402 result = p.stdout.rstrip()
403 if not result.endswith(self.local):
404 raise VCSException('Repository mismatch')
406 def gotorevisionx(self, rev):
407 if not os.path.exists(self.local):
409 p = FDroidPopen(['git', 'clone', self.remote, self.local])
410 if p.returncode != 0:
411 raise VCSException("Git clone failed")
415 # Discard any working tree changes
416 p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local)
417 if p.returncode != 0:
418 raise VCSException("Git reset failed")
419 # Remove untracked files now, in case they're tracked in the target
420 # revision (it happens!)
421 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
422 if p.returncode != 0:
423 raise VCSException("Git clean failed")
424 if not self.refreshed:
425 # Get latest commits and tags from remote
426 p = FDroidPopen(['git', 'fetch', 'origin'], cwd=self.local)
427 if p.returncode != 0:
428 raise VCSException("Git fetch failed")
429 p = SilentPopen(['git', 'fetch', '--prune', '--tags', 'origin'], cwd=self.local)
430 if p.returncode != 0:
431 raise VCSException("Git fetch failed")
432 self.refreshed = True
433 # Check out the appropriate revision
434 rev = str(rev if rev else 'origin/master')
435 p = SilentPopen(['git', 'checkout', '-f', rev], cwd=self.local)
436 if p.returncode != 0:
437 raise VCSException("Git checkout failed")
438 # Get rid of any uncontrolled files left behind
439 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
440 if p.returncode != 0:
441 raise VCSException("Git clean failed")
443 def initsubmodules(self):
445 submfile = os.path.join(self.local, '.gitmodules')
446 if not os.path.isfile(submfile):
447 raise VCSException("No git submodules available")
449 # fix submodules not accessible without an account and public key auth
450 with open(submfile, 'r') as f:
451 lines = f.readlines()
452 with open(submfile, 'w') as f:
454 if 'git@github.com' in line:
455 line = line.replace('git@github.com:', 'https://github.com/')
459 ['git', 'reset', '--hard'],
460 ['git', 'clean', '-dffx'],
462 p = SilentPopen(['git', 'submodule', 'foreach', '--recursive'] + cmd, cwd=self.local)
463 if p.returncode != 0:
464 raise VCSException("Git submodule reset failed")
465 p = FDroidPopen(['git', 'submodule', 'sync'], cwd=self.local)
466 if p.returncode != 0:
467 raise VCSException("Git submodule sync failed")
468 p = FDroidPopen(['git', 'submodule', 'update', '--init', '--force', '--recursive'], cwd=self.local)
469 if p.returncode != 0:
470 raise VCSException("Git submodule update failed")
474 p = SilentPopen(['git', 'tag'], cwd=self.local)
475 return p.stdout.splitlines()
477 def latesttags(self, alltags, number):
479 p = SilentPopen(['echo "' + '\n'.join(alltags) + '" | \
480 xargs -I@ git log --format=format:"%at @%n" -1 @ | \
481 sort -n | awk \'{print $2}\''],
482 cwd=self.local, shell=True)
483 return p.stdout.splitlines()[-number:]
486 class vcs_gitsvn(vcs):
491 # Damn git-svn tries to use a graphical password prompt, so we have to
492 # trick it into taking the password from stdin
494 if self.username is None:
496 return ('echo "%s" | DISPLAY="" ' % self.password, ' --username "%s"' % self.username)
498 # If the local directory exists, but is somehow not a git repository, git
499 # will traverse up the directory tree until it finds one that is (i.e.
500 # fdroidserver) and then we'll proceed to destory it! This is called as
503 p = SilentPopen(['git', 'rev-parse', '--show-toplevel'], cwd=self.local)
504 result = p.stdout.rstrip()
505 if not result.endswith(self.local):
506 raise VCSException('Repository mismatch')
508 def gotorevisionx(self, rev):
509 if not os.path.exists(self.local):
511 gitsvn_cmd = '%sgit svn clone%s' % self.userargs()
512 if ';' in self.remote:
513 remote_split = self.remote.split(';')
514 for i in remote_split[1:]:
515 if i.startswith('trunk='):
516 gitsvn_cmd += ' -T %s' % i[6:]
517 elif i.startswith('tags='):
518 gitsvn_cmd += ' -t %s' % i[5:]
519 elif i.startswith('branches='):
520 gitsvn_cmd += ' -b %s' % i[9:]
521 p = SilentPopen([gitsvn_cmd + " %s %s" % (remote_split[0], self.local)], shell=True)
522 if p.returncode != 0:
523 raise VCSException("Git clone failed")
525 p = SilentPopen([gitsvn_cmd + " %s %s" % (self.remote, self.local)], shell=True)
526 if p.returncode != 0:
527 raise VCSException("Git clone failed")
531 # Discard any working tree changes
532 p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local)
533 if p.returncode != 0:
534 raise VCSException("Git reset failed")
535 # Remove untracked files now, in case they're tracked in the target
536 # revision (it happens!)
537 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
538 if p.returncode != 0:
539 raise VCSException("Git clean failed")
540 if not self.refreshed:
541 # Get new commits, branches and tags from repo
542 p = SilentPopen(['%sgit svn fetch %s' % self.userargs()], cwd=self.local, shell=True)
543 if p.returncode != 0:
544 raise VCSException("Git svn fetch failed")
545 p = SilentPopen(['%sgit svn rebase %s' % self.userargs()], cwd=self.local, shell=True)
546 if p.returncode != 0:
547 raise VCSException("Git svn rebase failed")
548 self.refreshed = True
550 rev = str(rev if rev else 'master')
552 nospaces_rev = rev.replace(' ', '%20')
553 # Try finding a svn tag
554 p = SilentPopen(['git', 'checkout', 'tags/' + nospaces_rev], cwd=self.local)
555 if p.returncode != 0:
556 # No tag found, normal svn rev translation
557 # Translate svn rev into git format
558 rev_split = rev.split('/')
559 if len(rev_split) > 1:
560 treeish = rev_split[0]
561 svn_rev = rev_split[1]
564 # if no branch is specified, then assume trunk (ie. 'master'
569 p = SilentPopen(['git', 'svn', 'find-rev', 'r' + svn_rev, treeish], cwd=self.local)
570 git_rev = p.stdout.rstrip()
572 if p.returncode != 0 or not git_rev:
573 # Try a plain git checkout as a last resort
574 p = SilentPopen(['git', 'checkout', rev], cwd=self.local)
575 if p.returncode != 0:
576 raise VCSException("No git treeish found and direct git checkout failed")
578 # Check out the git rev equivalent to the svn rev
579 p = SilentPopen(['git', 'checkout', git_rev], cwd=self.local)
580 if p.returncode != 0:
581 raise VCSException("Git svn checkout failed")
583 # Get rid of any uncontrolled files left behind
584 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
585 if p.returncode != 0:
586 raise VCSException("Git clean failed")
590 return os.listdir(os.path.join(self.local, '.git/svn/refs/remotes/tags'))
594 p = SilentPopen(['git', 'svn', 'find-rev', 'HEAD'], cwd=self.local)
595 if p.returncode != 0:
597 return p.stdout.strip()
606 if self.username is None:
607 return ['--non-interactive']
608 return ['--username', self.username,
609 '--password', self.password,
612 def gotorevisionx(self, rev):
613 if not os.path.exists(self.local):
614 p = SilentPopen(['svn', 'checkout', self.remote, self.local] + self.userargs())
615 if p.returncode != 0:
616 raise VCSException("Svn checkout failed")
620 r"svn status | awk '/\?/ {print $2}' | xargs rm -rf"):
621 p = SilentPopen([svncommand], cwd=self.local, shell=True)
622 if p.returncode != 0:
623 raise VCSException("Svn reset ({0}) failed in {1}".format(svncommand, self.local))
624 if not self.refreshed:
625 p = SilentPopen(['svn', 'update'] + self.userargs(), cwd=self.local)
626 if p.returncode != 0:
627 raise VCSException("Svn update failed")
628 self.refreshed = True
630 revargs = list(['-r', rev] if rev else [])
631 p = SilentPopen(['svn', 'update', '--force'] + revargs + self.userargs(), cwd=self.local)
632 if p.returncode != 0:
633 raise VCSException("Svn update failed")
636 p = SilentPopen(['svn', 'info'], cwd=self.local)
637 for line in p.stdout.splitlines():
638 if line and line.startswith('Last Changed Rev: '):
648 def gotorevisionx(self, rev):
649 if not os.path.exists(self.local):
650 p = SilentPopen(['hg', 'clone', self.remote, self.local])
651 if p.returncode != 0:
652 raise VCSException("Hg clone failed")
654 p = SilentPopen(['hg status -uS | xargs rm -rf'], cwd=self.local, shell=True)
655 if p.returncode != 0:
656 raise VCSException("Hg clean failed")
657 if not self.refreshed:
658 p = SilentPopen(['hg', 'pull'], cwd=self.local)
659 if p.returncode != 0:
660 raise VCSException("Hg pull failed")
661 self.refreshed = True
663 rev = str(rev if rev else 'default')
666 p = SilentPopen(['hg', 'update', '-C', rev], cwd=self.local)
667 if p.returncode != 0:
668 raise VCSException("Hg checkout failed")
669 p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local)
670 # Also delete untracked files, we have to enable purge extension for that:
671 if "'purge' is provided by the following extension" in p.stdout:
672 with open(self.local + "/.hg/hgrc", "a") as myfile:
673 myfile.write("\n[extensions]\nhgext.purge=\n")
674 p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local)
675 if p.returncode != 0:
676 raise VCSException("HG purge failed")
677 elif p.returncode != 0:
678 raise VCSException("HG purge failed")
681 p = SilentPopen(['hg', 'tags', '-q'], cwd=self.local)
682 return p.stdout.splitlines()[1:]
690 def gotorevisionx(self, rev):
691 if not os.path.exists(self.local):
692 p = SilentPopen(['bzr', 'branch', self.remote, self.local])
693 if p.returncode != 0:
694 raise VCSException("Bzr branch failed")
696 p = SilentPopen(['bzr', 'clean-tree', '--force', '--unknown', '--ignored'], cwd=self.local)
697 if p.returncode != 0:
698 raise VCSException("Bzr revert failed")
699 if not self.refreshed:
700 p = SilentPopen(['bzr', 'pull'], cwd=self.local)
701 if p.returncode != 0:
702 raise VCSException("Bzr update failed")
703 self.refreshed = True
705 revargs = list(['-r', rev] if rev else [])
706 p = SilentPopen(['bzr', 'revert'] + revargs, cwd=self.local)
707 if p.returncode != 0:
708 raise VCSException("Bzr revert failed")
711 p = SilentPopen(['bzr', 'tags'], cwd=self.local)
712 return [tag.split(' ')[0].strip() for tag in
713 p.stdout.splitlines()]
716 def retrieve_string(app_dir, string, xmlfiles=None):
719 os.path.join(app_dir, 'res'),
720 os.path.join(app_dir, 'src/main'),
725 for res_dir in res_dirs:
726 for r, d, f in os.walk(res_dir):
727 if r.endswith('/values'):
728 xmlfiles += [os.path.join(r, x) for x in f if x.endswith('.xml')]
731 if string.startswith('@string/'):
732 string_search = re.compile(r'.*name="' + string[8:] + '".*?>([^<]+?)<.*').search
733 elif string.startswith('&') and string.endswith(';'):
734 string_search = re.compile(r'.*<!ENTITY.*' + string[1:-1] + '.*?"([^"]+?)".*>').search
736 if string_search is not None:
737 for xmlfile in xmlfiles:
738 for line in file(xmlfile):
739 matches = string_search(line)
741 return retrieve_string(app_dir, matches.group(1), xmlfiles)
744 return string.replace("\\'", "'")
747 # Return list of existing files that will be used to find the highest vercode
748 def manifest_paths(app_dir, flavour):
750 possible_manifests = \
751 [os.path.join(app_dir, 'AndroidManifest.xml'),
752 os.path.join(app_dir, 'src', 'main', 'AndroidManifest.xml'),
753 os.path.join(app_dir, 'src', 'AndroidManifest.xml'),
754 os.path.join(app_dir, 'build.gradle')]
757 possible_manifests.append(
758 os.path.join(app_dir, 'src', flavour, 'AndroidManifest.xml'))
760 return [path for path in possible_manifests if os.path.isfile(path)]
763 # Retrieve the package name. Returns the name, or None if not found.
764 def fetch_real_name(app_dir, flavour):
765 app_search = re.compile(r'.*<application.*').search
766 name_search = re.compile(r'.*android:label="([^"]+)".*').search
768 for f in manifest_paths(app_dir, flavour):
769 if not has_extension(f, 'xml'):
771 logging.debug("fetch_real_name: Checking manifest at " + f)
777 matches = name_search(line)
779 stringname = matches.group(1)
780 logging.debug("fetch_real_name: using string " + stringname)
781 result = retrieve_string(app_dir, stringname)
783 result = result.strip()
788 # Retrieve the version name
789 def version_name(original, app_dir, flavour):
790 for f in manifest_paths(app_dir, flavour):
791 if not has_extension(f, 'xml'):
793 string = retrieve_string(app_dir, original)
799 def get_library_references(root_dir):
801 proppath = os.path.join(root_dir, 'project.properties')
802 if not os.path.isfile(proppath):
804 with open(proppath) as f:
805 for line in f.readlines():
806 if not line.startswith('android.library.reference.'):
808 path = line.split('=')[1].strip()
809 relpath = os.path.join(root_dir, path)
810 if not os.path.isdir(relpath):
812 logging.info("Found subproject at %s" % path)
813 libraries.append(path)
817 def ant_subprojects(root_dir):
818 subprojects = get_library_references(root_dir)
819 for subpath in subprojects:
820 subrelpath = os.path.join(root_dir, subpath)
821 for p in get_library_references(subrelpath):
822 relp = os.path.normpath(os.path.join(subpath, p))
823 if relp not in subprojects:
824 subprojects.insert(0, relp)
828 def remove_debuggable_flags(root_dir):
829 # Remove forced debuggable flags
830 logging.info("Removing debuggable flags")
831 for root, dirs, files in os.walk(root_dir):
832 if 'AndroidManifest.xml' in files:
833 path = os.path.join(root, 'AndroidManifest.xml')
834 p = FDroidPopen(['sed', '-i', 's/android:debuggable="[^"]*"//g', path])
835 if p.returncode != 0:
836 raise BuildException("Failed to remove debuggable flags of %s" % path)
839 # Extract some information from the AndroidManifest.xml at the given path.
840 # Returns (version, vercode, package), any or all of which might be None.
841 # All values returned are strings.
842 def parse_androidmanifests(paths, ignoreversions=None):
845 return (None, None, None)
847 vcsearch = re.compile(r'.*:versionCode="([0-9]+?)".*').search
848 vnsearch = re.compile(r'.*:versionName="([^"]+?)".*').search
849 psearch = re.compile(r'.*package="([^"]+)".*').search
851 vcsearch_g = re.compile(r'.*versionCode *=* *["\']*([0-9]+)["\']*').search
852 vnsearch_g = re.compile(r'.*versionName *=* *(["\'])((?:(?=(\\?))\3.)*?)\1.*').search
853 psearch_g = re.compile(r'.*packageName *=* *["\']([^"]+)["\'].*').search
855 ignoresearch = re.compile(ignoreversions).search if ignoreversions else None
863 gradle = has_extension(path, 'gradle')
866 # Remember package name, may be defined separately from version+vercode
867 package = max_package
869 for line in file(path):
872 matches = psearch_g(line)
874 matches = psearch(line)
876 package = matches.group(1)
879 matches = vnsearch_g(line)
881 matches = vnsearch(line)
883 version = matches.group(2 if gradle else 1)
886 matches = vcsearch_g(line)
888 matches = vcsearch(line)
890 vercode = matches.group(1)
892 # Better some package name than nothing
893 if max_package is None:
894 max_package = package
896 if max_vercode is None or (vercode is not None and vercode > max_vercode):
897 if not ignoresearch or not ignoresearch(version):
898 max_version = version
899 max_vercode = vercode
900 max_package = package
902 max_version = "Ignore"
904 if max_version is None:
905 max_version = "Unknown"
907 return (max_version, max_vercode, max_package)
910 class BuildException(Exception):
911 def __init__(self, value, detail=None):
915 def get_wikitext(self):
916 ret = repr(self.value) + "\n"
920 txt = self.detail[-8192:] if len(self.detail) > 8192 else self.detail
928 ret += "\n==== detail begin ====\n%s\n==== detail end ====" % self.detail.strip()
932 class VCSException(Exception):
933 def __init__(self, value):
940 # Get the specified source library.
941 # Returns the path to it. Normally this is the path to be used when referencing
942 # it, which may be a subdirectory of the actual project. If you want the base
943 # directory of the project, pass 'basepath=True'.
944 def getsrclib(spec, srclib_dir, srclibpaths=[], subdir=None,
945 basepath=False, raw=False, prepare=True, preponly=False):
953 name, ref = spec.split('@')
955 number, name = name.split(':', 1)
957 name, subdir = name.split('/', 1)
959 if name not in metadata.srclibs:
960 raise BuildException('srclib ' + name + ' not found.')
962 srclib = metadata.srclibs[name]
964 sdir = os.path.join(srclib_dir, name)
967 vcs = getvcs(srclib["Repo Type"], srclib["Repo"], sdir)
968 vcs.srclib = (name, number, sdir)
970 vcs.gotorevision(ref)
977 libdir = os.path.join(sdir, subdir)
978 elif srclib["Subdir"]:
979 for subdir in srclib["Subdir"]:
980 libdir_candidate = os.path.join(sdir, subdir)
981 if os.path.exists(libdir_candidate):
982 libdir = libdir_candidate
988 if srclib["Srclibs"]:
990 for lib in srclib["Srclibs"].replace(';', ',').split(','):
992 for t in srclibpaths:
997 raise BuildException('Missing recursive srclib %s for %s' % (
999 place_srclib(libdir, n, s_tuple[2])
1002 remove_signing_keys(sdir)
1003 remove_debuggable_flags(sdir)
1007 if srclib["Prepare"]:
1008 cmd = replace_config_vars(srclib["Prepare"])
1010 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=libdir)
1011 if p.returncode != 0:
1012 raise BuildException("Error running prepare command for srclib %s"
1018 return (name, number, libdir)
1021 # Prepare the source code for a particular build
1022 # 'vcs' - the appropriate vcs object for the application
1023 # 'app' - the application details from the metadata
1024 # 'build' - the build details from the metadata
1025 # 'build_dir' - the path to the build directory, usually
1027 # 'srclib_dir' - the path to the source libraries directory, usually
1029 # 'extlib_dir' - the path to the external libraries directory, usually
1031 # Returns the (root, srclibpaths) where:
1032 # 'root' is the root directory, which may be the same as 'build_dir' or may
1033 # be a subdirectory of it.
1034 # 'srclibpaths' is information on the srclibs being used
1035 def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=False):
1037 # Optionally, the actual app source can be in a subdirectory
1039 root_dir = os.path.join(build_dir, build['subdir'])
1041 root_dir = build_dir
1043 # Get a working copy of the right revision
1044 logging.info("Getting source for revision " + build['commit'])
1045 vcs.gotorevision(build['commit'])
1047 # Initialise submodules if requred
1048 if build['submodules']:
1049 logging.info("Initialising submodules")
1050 vcs.initsubmodules()
1052 # Check that a subdir (if we're using one) exists. This has to happen
1053 # after the checkout, since it might not exist elsewhere
1054 if not os.path.exists(root_dir):
1055 raise BuildException('Missing subdir ' + root_dir)
1057 # Run an init command if one is required
1059 cmd = replace_config_vars(build['init'])
1060 logging.info("Running 'init' commands in %s" % root_dir)
1062 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
1063 if p.returncode != 0:
1064 raise BuildException("Error running init command for %s:%s" %
1065 (app['id'], build['version']), p.stdout)
1067 # Apply patches if any
1069 logging.info("Applying patches")
1070 for patch in build['patch']:
1071 patch = patch.strip()
1072 logging.info("Applying " + patch)
1073 patch_path = os.path.join('metadata', app['id'], patch)
1074 p = FDroidPopen(['patch', '-p1', '-i', os.path.abspath(patch_path)], cwd=build_dir)
1075 if p.returncode != 0:
1076 raise BuildException("Failed to apply patch %s" % patch_path)
1078 # Get required source libraries
1080 if build['srclibs']:
1081 logging.info("Collecting source libraries")
1082 for lib in build['srclibs']:
1083 srclibpaths.append(getsrclib(lib, srclib_dir, srclibpaths,
1086 for name, number, libpath in srclibpaths:
1087 place_srclib(root_dir, int(number) if number else None, libpath)
1089 basesrclib = vcs.getsrclib()
1090 # If one was used for the main source, add that too.
1092 srclibpaths.append(basesrclib)
1094 # Update the local.properties file
1095 localprops = [os.path.join(build_dir, 'local.properties')]
1097 localprops += [os.path.join(root_dir, 'local.properties')]
1098 for path in localprops:
1099 if not os.path.isfile(path):
1101 logging.info("Updating properties file at %s" % path)
1106 # Fix old-fashioned 'sdk-location' by copying
1107 # from sdk.dir, if necessary
1108 if build['oldsdkloc']:
1109 sdkloc = re.match(r".*^sdk.dir=(\S+)$.*", props,
1110 re.S | re.M).group(1)
1111 props += "sdk-location=%s\n" % sdkloc
1113 props += "sdk.dir=%s\n" % config['sdk_path']
1114 props += "sdk-location=%s\n" % config['sdk_path']
1115 if 'ndk_path' in config:
1117 props += "ndk.dir=%s\n" % config['ndk_path']
1118 props += "ndk-location=%s\n" % config['ndk_path']
1119 # Add java.encoding if necessary
1120 if build['encoding']:
1121 props += "java.encoding=%s\n" % build['encoding']
1127 if build['type'] == 'gradle':
1128 flavour = build['gradle'].split('@')[0]
1129 if flavour in ['main', 'yes', '']:
1133 n = build["target"].split('-')[1]
1134 FDroidPopen(['sed', '-i',
1135 's@compileSdkVersion *[0-9]*@compileSdkVersion ' + n + '@g',
1138 if '@' in build['gradle']:
1139 gradle_dir = os.path.join(root_dir, build['gradle'].split('@', 1)[1])
1140 gradle_dir = os.path.normpath(gradle_dir)
1141 FDroidPopen(['sed', '-i',
1142 's@compileSdkVersion *[0-9]*@compileSdkVersion ' + n + '@g',
1146 # Remove forced debuggable flags
1147 remove_debuggable_flags(root_dir)
1149 # Insert version code and number into the manifest if necessary
1150 if build['forceversion']:
1151 logging.info("Changing the version name")
1152 for path in manifest_paths(root_dir, flavour):
1153 if not os.path.isfile(path):
1155 if has_extension(path, 'xml'):
1156 p = SilentPopen(['sed', '-i',
1157 's/android:versionName="[^"]*"/android:versionName="'
1158 + build['version'] + '"/g',
1160 if p.returncode != 0:
1161 raise BuildException("Failed to amend manifest")
1162 elif has_extension(path, 'gradle'):
1163 p = SilentPopen(['sed', '-i',
1164 's/versionName *=* *"[^"]*"/versionName = "'
1165 + build['version'] + '"/g',
1167 if p.returncode != 0:
1168 raise BuildException("Failed to amend build.gradle")
1169 if build['forcevercode']:
1170 logging.info("Changing the version code")
1171 for path in manifest_paths(root_dir, flavour):
1172 if not os.path.isfile(path):
1174 if has_extension(path, 'xml'):
1175 p = SilentPopen(['sed', '-i',
1176 's/android:versionCode="[^"]*"/android:versionCode="'
1177 + build['vercode'] + '"/g',
1179 if p.returncode != 0:
1180 raise BuildException("Failed to amend manifest")
1181 elif has_extension(path, 'gradle'):
1182 p = SilentPopen(['sed', '-i',
1183 's/versionCode *=* *[0-9]*/versionCode = '
1184 + build['vercode'] + '/g',
1186 if p.returncode != 0:
1187 raise BuildException("Failed to amend build.gradle")
1189 # Delete unwanted files
1191 logging.info("Removing specified files")
1192 for part in getpaths(build_dir, build, 'rm'):
1193 dest = os.path.join(build_dir, part)
1194 logging.info("Removing {0}".format(part))
1195 if os.path.lexists(dest):
1196 if os.path.islink(dest):
1197 SilentPopen(['unlink ' + dest], shell=True)
1199 SilentPopen(['rm -rf ' + dest], shell=True)
1201 logging.info("...but it didn't exist")
1203 remove_signing_keys(build_dir)
1205 # Add required external libraries
1206 if build['extlibs']:
1207 logging.info("Collecting prebuilt libraries")
1208 libsdir = os.path.join(root_dir, 'libs')
1209 if not os.path.exists(libsdir):
1211 for lib in build['extlibs']:
1213 logging.info("...installing extlib {0}".format(lib))
1214 libf = os.path.basename(lib)
1215 libsrc = os.path.join(extlib_dir, lib)
1216 if not os.path.exists(libsrc):
1217 raise BuildException("Missing extlib file {0}".format(libsrc))
1218 shutil.copyfile(libsrc, os.path.join(libsdir, libf))
1220 # Run a pre-build command if one is required
1221 if build['prebuild']:
1222 logging.info("Running 'prebuild' commands in %s" % root_dir)
1224 cmd = replace_config_vars(build['prebuild'])
1226 # Substitute source library paths into prebuild commands
1227 for name, number, libpath in srclibpaths:
1228 libpath = os.path.relpath(libpath, root_dir)
1229 cmd = cmd.replace('$$' + name + '$$', libpath)
1231 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
1232 if p.returncode != 0:
1233 raise BuildException("Error running prebuild command for %s:%s" %
1234 (app['id'], build['version']), p.stdout)
1236 # Generate (or update) the ant build file, build.xml...
1237 if build['update'] and build['update'] != ['no'] and build['type'] == 'ant':
1238 parms = [os.path.join(config['sdk_path'], 'tools', 'android'), 'update']
1239 lparms = parms + ['lib-project']
1240 parms = parms + ['project']
1243 parms += ['-t', build['target']]
1244 lparms += ['-t', build['target']]
1245 if build['update'] == ['auto']:
1246 update_dirs = ant_subprojects(root_dir) + ['.']
1248 update_dirs = build['update']
1250 for d in update_dirs:
1251 subdir = os.path.join(root_dir, d)
1253 print("Updating main project")
1254 cmd = parms + ['-p', d]
1256 print("Updating subproject %s" % d)
1257 cmd = lparms + ['-p', d]
1258 p = FDroidPopen(cmd, cwd=root_dir)
1259 # Check to see whether an error was returned without a proper exit
1260 # code (this is the case for the 'no target set or target invalid'
1262 if p.returncode != 0 or p.stdout.startswith("Error: "):
1263 raise BuildException("Failed to update project at %s" % d, p.stdout)
1264 # Clean update dirs via ant
1266 logging.info("Cleaning subproject %s" % d)
1267 p = FDroidPopen(['ant', 'clean'], cwd=subdir)
1269 return (root_dir, srclibpaths)
1272 # Split and extend via globbing the paths from a field
1273 def getpaths(build_dir, build, field):
1275 for p in build[field]:
1277 full_path = os.path.join(build_dir, p)
1278 full_path = os.path.normpath(full_path)
1279 paths += [r[len(build_dir) + 1:] for r in glob.glob(full_path)]
1283 # Scan the source code in the given directory (and all subdirectories)
1284 # and return the number of fatal problems encountered
1285 def scan_source(build_dir, root_dir, thisbuild):
1289 # Common known non-free blobs (always lower case):
1291 re.compile(r'flurryagent', re.IGNORECASE),
1292 re.compile(r'paypal.*mpl', re.IGNORECASE),
1293 re.compile(r'libgoogleanalytics', re.IGNORECASE),
1294 re.compile(r'admob.*sdk.*android', re.IGNORECASE),
1295 re.compile(r'googleadview', re.IGNORECASE),
1296 re.compile(r'googleadmobadssdk', re.IGNORECASE),
1297 re.compile(r'google.*play.*services', re.IGNORECASE),
1298 re.compile(r'crittercism', re.IGNORECASE),
1299 re.compile(r'heyzap', re.IGNORECASE),
1300 re.compile(r'jpct.*ae', re.IGNORECASE),
1301 re.compile(r'youtubeandroidplayerapi', re.IGNORECASE),
1302 re.compile(r'bugsense', re.IGNORECASE),
1303 re.compile(r'crashlytics', re.IGNORECASE),
1304 re.compile(r'ouya.*sdk', re.IGNORECASE),
1305 re.compile(r'libspen23', re.IGNORECASE),
1308 scanignore = getpaths(build_dir, thisbuild, 'scanignore')
1309 scandelete = getpaths(build_dir, thisbuild, 'scandelete')
1312 ms = magic.open(magic.MIME_TYPE)
1314 except AttributeError:
1318 for i in scanignore:
1319 if fd.startswith(i):
1324 for i in scandelete:
1325 if fd.startswith(i):
1329 def removeproblem(what, fd, fp):
1330 logging.info('Removing %s at %s' % (what, fd))
1333 def warnproblem(what, fd):
1334 logging.warn('Found %s at %s' % (what, fd))
1336 def handleproblem(what, fd, fp):
1338 removeproblem(what, fd, fp)
1340 logging.error('Found %s at %s' % (what, fd))
1344 def insidedir(path, dirname):
1345 return path.endswith('/%s' % dirname) or '/%s/' % dirname in path
1347 # Iterate through all files in the source code
1348 for r, d, f in os.walk(build_dir):
1350 if any(insidedir(r, d) for d in ('.hg', '.git', '.svn', '.bzr')):
1355 # Path (relative) to the file
1356 fp = os.path.join(r, curfile)
1357 fd = fp[len(build_dir) + 1:]
1359 # Check if this file has been explicitly excluded from scanning
1363 mime = magic.from_file(fp, mime=True) if ms is None else ms.file(fp)
1365 if mime == 'application/x-sharedlib':
1366 count += handleproblem('shared library', fd, fp)
1368 elif mime == 'application/x-archive':
1369 count += handleproblem('static library', fd, fp)
1371 elif mime == 'application/x-executable':
1372 count += handleproblem('binary executable', fd, fp)
1374 elif mime == 'application/x-java-applet':
1375 count += handleproblem('Java compiled class', fd, fp)
1380 'application/java-archive',
1381 'application/octet-stream',
1385 if has_extension(fp, 'apk'):
1386 removeproblem('APK file', fd, fp)
1388 elif has_extension(fp, 'jar'):
1390 if any(suspect.match(curfile) for suspect in usual_suspects):
1391 count += handleproblem('usual supect', fd, fp)
1393 warnproblem('JAR file', fd)
1395 elif has_extension(fp, 'zip'):
1396 warnproblem('ZIP file', fd)
1399 warnproblem('unknown compressed or binary file', fd)
1401 elif has_extension(fp, 'java'):
1402 for line in file(fp):
1403 if 'DexClassLoader' in line:
1404 count += handleproblem('DexClassLoader', fd, fp)
1409 # Presence of a jni directory without buildjni=yes might
1410 # indicate a problem (if it's not a problem, explicitly use
1411 # buildjni=no to bypass this check)
1412 if (os.path.exists(os.path.join(root_dir, 'jni')) and
1413 not thisbuild['buildjni']):
1414 logging.error('Found jni directory, but buildjni is not enabled')
1423 self.path = os.path.join('stats', 'known_apks.txt')
1425 if os.path.exists(self.path):
1426 for line in file(self.path):
1427 t = line.rstrip().split(' ')
1429 self.apks[t[0]] = (t[1], None)
1431 self.apks[t[0]] = (t[1], time.strptime(t[2], '%Y-%m-%d'))
1432 self.changed = False
1434 def writeifchanged(self):
1436 if not os.path.exists('stats'):
1438 f = open(self.path, 'w')
1440 for apk, app in self.apks.iteritems():
1442 line = apk + ' ' + appid
1444 line += ' ' + time.strftime('%Y-%m-%d', added)
1446 for line in sorted(lst):
1447 f.write(line + '\n')
1450 # Record an apk (if it's new, otherwise does nothing)
1451 # Returns the date it was added.
1452 def recordapk(self, apk, app):
1453 if apk not in self.apks:
1454 self.apks[apk] = (app, time.gmtime(time.time()))
1456 _, added = self.apks[apk]
1459 # Look up information - given the 'apkname', returns (app id, date added/None).
1460 # Or returns None for an unknown apk.
1461 def getapp(self, apkname):
1462 if apkname in self.apks:
1463 return self.apks[apkname]
1466 # Get the most recent 'num' apps added to the repo, as a list of package ids
1467 # with the most recent first.
1468 def getlatest(self, num):
1470 for apk, app in self.apks.iteritems():
1474 if apps[appid] > added:
1478 sortedapps = sorted(apps.iteritems(), key=operator.itemgetter(1))[-num:]
1479 lst = [app for app, _ in sortedapps]
1484 def isApkDebuggable(apkfile, config):
1485 """Returns True if the given apk file is debuggable
1487 :param apkfile: full path to the apk to check"""
1489 p = SilentPopen([os.path.join(config['sdk_path'], 'build-tools',
1490 config['build_tools'], 'aapt'),
1491 'dump', 'xmltree', apkfile, 'AndroidManifest.xml'])
1492 if p.returncode != 0:
1493 logging.critical("Failed to get apk manifest information")
1495 for line in p.stdout.splitlines():
1496 if 'android:debuggable' in line and not line.endswith('0x0'):
1501 class AsynchronousFileReader(threading.Thread):
1503 Helper class to implement asynchronous reading of a file
1504 in a separate thread. Pushes read lines on a queue to
1505 be consumed in another thread.
1508 def __init__(self, fd, queue):
1509 assert isinstance(queue, Queue.Queue)
1510 assert callable(fd.readline)
1511 threading.Thread.__init__(self)
1516 '''The body of the tread: read lines and put them on the queue.'''
1517 for line in iter(self._fd.readline, ''):
1518 self._queue.put(line)
1521 '''Check whether there is no more content to expect.'''
1522 return not self.is_alive() and self._queue.empty()
1530 def SilentPopen(commands, cwd=None, shell=False):
1531 return FDroidPopen(commands, cwd=cwd, shell=shell, output=False)
1534 def FDroidPopen(commands, cwd=None, shell=False, output=False):
1536 Run a command and capture the possibly huge output.
1538 :param commands: command and argument list like in subprocess.Popen
1539 :param cwd: optionally specifies a working directory
1540 :returns: A PopenResult.
1545 cwd = os.path.normpath(cwd)
1546 logging.info("Directory: %s" % cwd)
1547 logging.info("> %s" % ' '.join(commands))
1549 result = PopenResult()
1550 p = subprocess.Popen(commands, cwd=cwd, shell=shell,
1551 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
1553 stdout_queue = Queue.Queue()
1554 stdout_reader = AsynchronousFileReader(p.stdout, stdout_queue)
1555 stdout_reader.start()
1557 # Check the queue for output (until there is no more to get)
1558 while not stdout_reader.eof():
1559 while not stdout_queue.empty():
1560 line = stdout_queue.get()
1561 if output or options.verbose:
1562 # Output directly to console
1563 sys.stdout.write(line)
1565 result.stdout += line
1570 result.returncode = p.returncode
1574 def remove_signing_keys(build_dir):
1575 comment = re.compile(r'[ ]*//')
1576 signing_configs = re.compile(r'^[\t ]*signingConfigs[ \t]*{[ \t]*$')
1578 re.compile(r'^[\t ]*signingConfig [^ ]*$'),
1579 re.compile(r'.*android\.signingConfigs\.[^{]*$'),
1580 re.compile(r'.*variant\.outputFile = .*'),
1581 re.compile(r'.*\.readLine\(.*'),
1583 for root, dirs, files in os.walk(build_dir):
1584 if 'build.gradle' in files:
1585 path = os.path.join(root, 'build.gradle')
1587 with open(path, "r") as o:
1588 lines = o.readlines()
1591 with open(path, "w") as o:
1593 if comment.match(line):
1597 opened += line.count('{')
1598 opened -= line.count('}')
1601 if signing_configs.match(line):
1605 if any(s.match(line) for s in line_matches):
1611 logging.info("Cleaned build.gradle of keysigning configs at %s" % path)
1614 'project.properties',
1616 'default.properties',
1619 if propfile in files:
1620 path = os.path.join(root, propfile)
1622 with open(path, "r") as o:
1623 lines = o.readlines()
1625 with open(path, "w") as o:
1627 if line.startswith('key.store'):
1629 if line.startswith('key.alias'):
1633 logging.info("Cleaned %s of keysigning configs at %s" % (propfile, path))
1636 def replace_config_vars(cmd):
1637 cmd = cmd.replace('$$SDK$$', config['sdk_path'])
1638 cmd = cmd.replace('$$NDK$$', config['ndk_path'])
1639 cmd = cmd.replace('$$MVN3$$', config['mvn3'])
1643 def place_srclib(root_dir, number, libpath):
1646 relpath = os.path.relpath(libpath, root_dir)
1647 proppath = os.path.join(root_dir, 'project.properties')
1650 if os.path.isfile(proppath):
1651 with open(proppath, "r") as o:
1652 lines = o.readlines()
1654 with open(proppath, "w") as o:
1657 if line.startswith('android.library.reference.%d=' % number):
1658 o.write('android.library.reference.%d=%s\n' % (number, relpath))
1663 o.write('android.library.reference.%d=%s\n' % (number, relpath))