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/>.
33 from distutils.version import LooseVersion
42 def get_default_config():
44 'sdk_path': os.getenv("ANDROID_HOME"),
45 'ndk_path': os.getenv("ANDROID_NDK"),
46 'build_tools': "20.0.0",
50 'sync_from_local_copy_dir': False,
51 'update_stats': False,
52 'stats_to_carbon': False,
54 'build_server_always': False,
55 'keystore': os.path.join(os.getenv("HOME"), '.local', 'share', 'fdroidserver', 'keystore.jks'),
56 'smartcardoptions': [],
62 'repo_url': "https://MyFirstFDroidRepo.org/fdroid/repo",
63 'repo_name': "My First FDroid Repo Demo",
64 'repo_icon': "fdroid-icon.png",
66 "This is a repository of apps to be used with FDroid. Applications in this "
67 + "repository are either official binaries built by the original application "
68 + "developers, or are binaries built from source by the admin of f-droid.org "
69 + "using the tools on https://gitlab.com/u/fdroid."),
74 def read_config(opts, config_file='config.py'):
75 """Read the repository config
77 The config is read from config_file, which is in the current directory when
78 any of the repo management commands are used.
80 global config, options, env
82 if config is not None:
84 if not os.path.isfile(config_file):
85 logging.critical("Missing config file - is this a repo directory?")
92 logging.debug("Reading %s" % config_file)
93 execfile(config_file, config)
95 # smartcardoptions must be a list since its command line args for Popen
96 if 'smartcardoptions' in config:
97 config['smartcardoptions'] = config['smartcardoptions'].split(' ')
98 elif 'keystore' in config and config['keystore'] == 'NONE':
99 # keystore='NONE' means use smartcard, these are required defaults
100 config['smartcardoptions'] = ['-storetype', 'PKCS11', '-providerName',
101 'SunPKCS11-OpenSC', '-providerClass',
102 'sun.security.pkcs11.SunPKCS11',
103 '-providerArg', 'opensc-fdroid.cfg']
105 if any(k in config for k in ["keystore", "keystorepass", "keypass"]):
106 st = os.stat(config_file)
107 if st.st_mode & stat.S_IRWXG or st.st_mode & stat.S_IRWXO:
108 logging.warn("unsafe permissions on {0} (should be 0600)!".format(config_file))
110 defconfig = get_default_config()
111 for k, v in defconfig.items():
115 # Expand environment variables
116 for k, v in config.items():
119 v = os.path.expanduser(v)
120 config[k] = os.path.expandvars(v)
122 if not test_sdk_exists(config):
125 if not test_build_tools_exists(config):
130 os.path.join(config['sdk_path'], 'build-tools', config['build_tools'], 'aapt'),
133 os.path.join(config['sdk_path'], 'tools', 'zipalign'),
134 os.path.join(config['sdk_path'], 'build-tools', config['build_tools'], 'zipalign'),
137 os.path.join(config['sdk_path'], 'tools', 'android'),
141 for b, paths in bin_paths.items():
144 if os.path.isfile(path):
147 if config[b] is None:
148 logging.warn("Could not find %s in any of the following paths:\n%s" % (
149 b, '\n'.join(paths)))
151 # There is no standard, so just set up the most common environment
154 for n in ['ANDROID_HOME', 'ANDROID_SDK']:
155 env[n] = config['sdk_path']
156 for n in ['ANDROID_NDK', 'NDK']:
157 env[n] = config['ndk_path']
159 for k in ["keystorepass", "keypass"]:
161 write_password_file(k)
163 # since this is used with rsync, where trailing slashes have meaning,
164 # ensure there is always a trailing slash
165 if 'serverwebroot' in config:
166 if config['serverwebroot'][-1] != '/':
167 config['serverwebroot'] += '/'
168 config['serverwebroot'] = config['serverwebroot'].replace('//', '/')
173 def test_sdk_exists(c):
174 if c['sdk_path'] is None:
175 # c['sdk_path'] is set to the value of ANDROID_HOME by default
176 logging.critical('No Android SDK found! ANDROID_HOME is not set and sdk_path is not in config.py!')
177 logging.info('You can use ANDROID_HOME to set the path to your SDK, i.e.:')
178 logging.info('\texport ANDROID_HOME=/opt/android-sdk')
180 if not os.path.exists(c['sdk_path']):
181 logging.critical('Android SDK path "' + c['sdk_path'] + '" does not exist!')
183 if not os.path.isdir(c['sdk_path']):
184 logging.critical('Android SDK path "' + c['sdk_path'] + '" is not a directory!')
186 if not os.path.isdir(os.path.join(c['sdk_path'], 'build-tools')):
187 logging.critical('Android SDK path "' + c['sdk_path'] + '" does not contain "build-tools/"!')
192 def test_build_tools_exists(c):
193 if not test_sdk_exists(c):
195 build_tools = os.path.join(c['sdk_path'], 'build-tools')
196 versioned_build_tools = os.path.join(build_tools, c['build_tools'])
197 if not os.path.isdir(versioned_build_tools):
198 logging.critical('Android Build Tools path "'
199 + versioned_build_tools + '" does not exist!')
204 def write_password_file(pwtype, password=None):
206 writes out passwords to a protected file instead of passing passwords as
207 command line argments
209 filename = '.fdroid.' + pwtype + '.txt'
210 fd = os.open(filename, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0600)
212 os.write(fd, config[pwtype])
214 os.write(fd, password)
216 config[pwtype + 'file'] = filename
219 # Given the arguments in the form of multiple appid:[vc] strings, this returns
220 # a dictionary with the set of vercodes specified for each package.
221 def read_pkg_args(args, allow_vercodes=False):
228 if allow_vercodes and ':' in p:
229 package, vercode = p.split(':')
231 package, vercode = p, None
232 if package not in vercodes:
233 vercodes[package] = [vercode] if vercode else []
235 elif vercode and vercode not in vercodes[package]:
236 vercodes[package] += [vercode] if vercode else []
241 # On top of what read_pkg_args does, this returns the whole app metadata, but
242 # limiting the builds list to the builds matching the vercodes specified.
243 def read_app_args(args, allapps, allow_vercodes=False):
245 vercodes = read_pkg_args(args, allow_vercodes)
250 apps = [app for app in allapps if app['id'] in vercodes]
252 if len(apps) != len(vercodes):
253 allids = [app["id"] for app in allapps]
256 logging.critical("No such package: %s" % p)
257 raise Exception("Found invalid app ids in arguments")
259 raise Exception("No packages specified")
263 vc = vercodes[app['id']]
266 app['builds'] = [b for b in app['builds'] if b['vercode'] in vc]
267 if len(app['builds']) != len(vercodes[app['id']]):
269 allvcs = [b['vercode'] for b in app['builds']]
270 for v in vercodes[app['id']]:
272 logging.critical("No such vercode %s for app %s" % (v, app['id']))
275 raise Exception("Found invalid vercodes for some apps")
280 def has_extension(filename, extension):
281 name, ext = os.path.splitext(filename)
282 ext = ext.lower()[1:]
283 return ext == extension
288 def apknameinfo(filename):
290 filename = os.path.basename(filename)
291 if apk_regex is None:
292 apk_regex = re.compile(r"^(.+)_([0-9]+)\.apk$")
293 m = apk_regex.match(filename)
295 result = (m.group(1), m.group(2))
296 except AttributeError:
297 raise Exception("Invalid apk name: %s" % filename)
301 def getapkname(app, build):
302 return "%s_%s.apk" % (app['id'], build['vercode'])
305 def getsrcname(app, build):
306 return "%s_%s_src.tar.gz" % (app['id'], build['vercode'])
313 return app['Auto Name']
318 return '%s (%s)' % (app['Current Version'], app['Current Version Code'])
321 def getvcs(vcstype, remote, local):
323 return vcs_git(remote, local)
325 return vcs_svn(remote, local)
326 if vcstype == 'git-svn':
327 return vcs_gitsvn(remote, local)
329 return vcs_hg(remote, local)
331 return vcs_bzr(remote, local)
332 if vcstype == 'srclib':
333 if local != 'build/srclib/' + remote:
334 raise VCSException("Error: srclib paths are hard-coded!")
335 return getsrclib(remote, 'build/srclib', raw=True)
336 raise VCSException("Invalid vcs type " + vcstype)
339 def getsrclibvcs(name):
340 if name not in metadata.srclibs:
341 raise VCSException("Missing srclib " + name)
342 return metadata.srclibs[name]['Repo Type']
346 def __init__(self, remote, local):
348 # svn, git-svn and bzr may require auth
350 if self.repotype() in ('svn', 'git-svn', 'bzr'):
352 self.username, remote = remote.split('@')
353 if ':' not in self.username:
354 raise VCSException("Password required with username")
355 self.username, self.password = self.username.split(':')
359 self.clone_failed = False
360 self.refreshed = False
366 # Take the local repository to a clean version of the given revision, which
367 # is specificed in the VCS's native format. Beforehand, the repository can
368 # be dirty, or even non-existent. If the repository does already exist
369 # locally, it will be updated from the origin, but only once in the
370 # lifetime of the vcs object.
371 # None is acceptable for 'rev' if you know you are cloning a clean copy of
372 # the repo - otherwise it must specify a valid revision.
373 def gotorevision(self, rev):
375 if self.clone_failed:
376 raise VCSException("Downloading the repository already failed once, not trying again.")
378 # The .fdroidvcs-id file for a repo tells us what VCS type
379 # and remote that directory was created from, allowing us to drop it
380 # automatically if either of those things changes.
381 fdpath = os.path.join(self.local, '..',
382 '.fdroidvcs-' + os.path.basename(self.local))
383 cdata = self.repotype() + ' ' + self.remote
386 if os.path.exists(self.local):
387 if os.path.exists(fdpath):
388 with open(fdpath, 'r') as f:
389 fsdata = f.read().strip()
395 "Repository details for {0} changed - deleting"
399 logging.info("Repository details missing - deleting")
401 shutil.rmtree(self.local)
403 self.gotorevisionx(rev)
405 # If necessary, write the .fdroidvcs file.
407 with open(fdpath, 'w') as f:
410 # Derived classes need to implement this. It's called once basic checking
411 # has been performend.
412 def gotorevisionx(self, rev):
413 raise VCSException("This VCS type doesn't define gotorevisionx")
415 # Initialise and update submodules
416 def initsubmodules(self):
417 raise VCSException('Submodules not supported for this vcs type')
419 # Get a list of all known tags
421 raise VCSException('gettags not supported for this vcs type')
423 # Get a list of latest number tags
424 def latesttags(self, number):
425 raise VCSException('latesttags not supported for this vcs type')
427 # Get current commit reference (hash, revision, etc)
429 raise VCSException('getref not supported for this vcs type')
431 # Returns the srclib (name, path) used in setting up the current
442 # If the local directory exists, but is somehow not a git repository, git
443 # will traverse up the directory tree until it finds one that is (i.e.
444 # fdroidserver) and then we'll proceed to destroy it! This is called as
447 p = SilentPopen(['git', 'rev-parse', '--show-toplevel'], cwd=self.local)
448 result = p.output.rstrip()
449 if not result.endswith(self.local):
450 raise VCSException('Repository mismatch')
452 def gotorevisionx(self, rev):
453 if not os.path.exists(self.local):
455 p = FDroidPopen(['git', 'clone', self.remote, self.local])
456 if p.returncode != 0:
457 self.clone_failed = True
458 raise VCSException("Git clone failed", p.output)
462 # Discard any working tree changes
463 p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local)
464 if p.returncode != 0:
465 raise VCSException("Git reset failed", p.output)
466 # Remove untracked files now, in case they're tracked in the target
467 # revision (it happens!)
468 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
469 if p.returncode != 0:
470 raise VCSException("Git clean failed", p.output)
471 if not self.refreshed:
472 # Get latest commits and tags from remote
473 p = FDroidPopen(['git', 'fetch', 'origin'], cwd=self.local)
474 if p.returncode != 0:
475 raise VCSException("Git fetch failed", p.output)
476 p = SilentPopen(['git', 'fetch', '--prune', '--tags', 'origin'], cwd=self.local)
477 if p.returncode != 0:
478 raise VCSException("Git fetch failed", p.output)
479 # Recreate origin/HEAD as git clone would do it, in case it disappeared
480 p = SilentPopen(['git', 'remote', 'set-head', 'origin', '--auto'], cwd=self.local)
481 if p.returncode != 0:
482 raise VCSException("Git remote set-head failed", p.output)
483 self.refreshed = True
484 # origin/HEAD is the HEAD of the remote, e.g. the "default branch" on
485 # a github repo. Most of the time this is the same as origin/master.
486 rev = str(rev if rev else 'origin/HEAD')
487 p = SilentPopen(['git', 'checkout', '-f', rev], cwd=self.local)
488 if p.returncode != 0:
489 raise VCSException("Git checkout of '%s' failed" % rev, p.output)
490 # Get rid of any uncontrolled files left behind
491 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
492 if p.returncode != 0:
493 raise VCSException("Git clean failed", p.output)
495 def initsubmodules(self):
497 submfile = os.path.join(self.local, '.gitmodules')
498 if not os.path.isfile(submfile):
499 raise VCSException("No git submodules available")
501 # fix submodules not accessible without an account and public key auth
502 with open(submfile, 'r') as f:
503 lines = f.readlines()
504 with open(submfile, 'w') as f:
506 if 'git@github.com' in line:
507 line = line.replace('git@github.com:', 'https://github.com/')
511 ['git', 'reset', '--hard'],
512 ['git', 'clean', '-dffx'],
514 p = SilentPopen(['git', 'submodule', 'foreach', '--recursive'] + cmd, cwd=self.local)
515 if p.returncode != 0:
516 raise VCSException("Git submodule reset failed", p.output)
517 p = FDroidPopen(['git', 'submodule', 'sync'], cwd=self.local)
518 if p.returncode != 0:
519 raise VCSException("Git submodule sync failed", p.output)
520 p = FDroidPopen(['git', 'submodule', 'update', '--init', '--force', '--recursive'], cwd=self.local)
521 if p.returncode != 0:
522 raise VCSException("Git submodule update failed", p.output)
526 p = SilentPopen(['git', 'tag'], cwd=self.local)
527 return p.output.splitlines()
529 def latesttags(self, alltags, number):
531 p = SilentPopen(['echo "' + '\n'.join(alltags) + '" | '
532 + 'xargs -I@ git log --format=format:"%at @%n" -1 @ | '
533 + 'sort -n | awk \'{print $2}\''],
534 cwd=self.local, shell=True)
535 return p.output.splitlines()[-number:]
538 class vcs_gitsvn(vcs):
543 # Damn git-svn tries to use a graphical password prompt, so we have to
544 # trick it into taking the password from stdin
546 if self.username is None:
548 return ('echo "%s" | DISPLAY="" ' % self.password, ' --username "%s"' % self.username)
550 # If the local directory exists, but is somehow not a git repository, git
551 # will traverse up the directory tree until it finds one that is (i.e.
552 # fdroidserver) and then we'll proceed to destory it! This is called as
555 p = SilentPopen(['git', 'rev-parse', '--show-toplevel'], cwd=self.local)
556 result = p.output.rstrip()
557 if not result.endswith(self.local):
558 raise VCSException('Repository mismatch')
560 def gotorevisionx(self, rev):
561 if not os.path.exists(self.local):
563 gitsvn_cmd = '%sgit svn clone%s' % self.userargs()
564 if ';' in self.remote:
565 remote_split = self.remote.split(';')
566 for i in remote_split[1:]:
567 if i.startswith('trunk='):
568 gitsvn_cmd += ' -T %s' % i[6:]
569 elif i.startswith('tags='):
570 gitsvn_cmd += ' -t %s' % i[5:]
571 elif i.startswith('branches='):
572 gitsvn_cmd += ' -b %s' % i[9:]
573 p = SilentPopen([gitsvn_cmd + " %s %s" % (remote_split[0], self.local)], shell=True)
574 if p.returncode != 0:
575 self.clone_failed = True
576 raise VCSException("Git clone failed", p.output)
578 p = SilentPopen([gitsvn_cmd + " %s %s" % (self.remote, self.local)], shell=True)
579 if p.returncode != 0:
580 self.clone_failed = True
581 raise VCSException("Git clone failed", p.output)
585 # Discard any working tree changes
586 p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local)
587 if p.returncode != 0:
588 raise VCSException("Git reset failed", p.output)
589 # Remove untracked files now, in case they're tracked in the target
590 # revision (it happens!)
591 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
592 if p.returncode != 0:
593 raise VCSException("Git clean failed", p.output)
594 if not self.refreshed:
595 # Get new commits, branches and tags from repo
596 p = SilentPopen(['%sgit svn fetch %s' % self.userargs()], cwd=self.local, shell=True)
597 if p.returncode != 0:
598 raise VCSException("Git svn fetch failed")
599 p = SilentPopen(['%sgit svn rebase %s' % self.userargs()], cwd=self.local, shell=True)
600 if p.returncode != 0:
601 raise VCSException("Git svn rebase failed", p.output)
602 self.refreshed = True
604 rev = str(rev if rev else 'master')
606 nospaces_rev = rev.replace(' ', '%20')
607 # Try finding a svn tag
608 p = SilentPopen(['git', 'checkout', 'tags/' + nospaces_rev], cwd=self.local)
609 if p.returncode != 0:
610 # No tag found, normal svn rev translation
611 # Translate svn rev into git format
612 rev_split = rev.split('/')
613 if len(rev_split) > 1:
614 treeish = 'origin/' + rev_split[0]
615 svn_rev = rev_split[1]
618 # if no branch is specified, then assume trunk (ie. 'master'
620 treeish = 'origin/master'
623 p = SilentPopen(['git', 'svn', 'find-rev', 'r' + svn_rev, treeish], cwd=self.local)
624 git_rev = p.output.rstrip()
626 if p.returncode != 0 or not git_rev:
627 # Try a plain git checkout as a last resort
628 p = SilentPopen(['git', 'checkout', rev], cwd=self.local)
629 if p.returncode != 0:
630 raise VCSException("No git treeish found and direct git checkout of '%s' failed" % rev, p.output)
632 # Check out the git rev equivalent to the svn rev
633 p = SilentPopen(['git', 'checkout', git_rev], cwd=self.local)
634 if p.returncode != 0:
635 raise VCSException("Git svn checkout of '%s' failed" % rev, p.output)
637 # Get rid of any uncontrolled files left behind
638 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
639 if p.returncode != 0:
640 raise VCSException("Git clean failed", p.output)
644 return os.listdir(os.path.join(self.local, '.git/svn/refs/remotes/tags'))
648 p = SilentPopen(['git', 'svn', 'find-rev', 'HEAD'], cwd=self.local)
649 if p.returncode != 0:
651 return p.output.strip()
660 if self.username is None:
661 return ['--non-interactive']
662 return ['--username', self.username,
663 '--password', self.password,
666 def gotorevisionx(self, rev):
667 if not os.path.exists(self.local):
668 p = SilentPopen(['svn', 'checkout', self.remote, self.local] + self.userargs())
669 if p.returncode != 0:
670 self.clone_failed = True
671 raise VCSException("Svn checkout of '%s' failed" % rev, p.output)
675 r"svn status | awk '/\?/ {print $2}' | xargs rm -rf"):
676 p = SilentPopen([svncommand], cwd=self.local, shell=True)
677 if p.returncode != 0:
678 raise VCSException("Svn reset ({0}) failed in {1}".format(svncommand, self.local), p.output)
679 if not self.refreshed:
680 p = SilentPopen(['svn', 'update'] + self.userargs(), cwd=self.local)
681 if p.returncode != 0:
682 raise VCSException("Svn update failed", p.output)
683 self.refreshed = True
685 revargs = list(['-r', rev] if rev else [])
686 p = SilentPopen(['svn', 'update', '--force'] + revargs + self.userargs(), cwd=self.local)
687 if p.returncode != 0:
688 raise VCSException("Svn update failed", p.output)
691 p = SilentPopen(['svn', 'info'], cwd=self.local)
692 for line in p.output.splitlines():
693 if line and line.startswith('Last Changed Rev: '):
703 def gotorevisionx(self, rev):
704 if not os.path.exists(self.local):
705 p = SilentPopen(['hg', 'clone', self.remote, self.local])
706 if p.returncode != 0:
707 self.clone_failed = True
708 raise VCSException("Hg clone failed", p.output)
710 p = SilentPopen(['hg status -uS | xargs rm -rf'], cwd=self.local, shell=True)
711 if p.returncode != 0:
712 raise VCSException("Hg clean failed", p.output)
713 if not self.refreshed:
714 p = SilentPopen(['hg', 'pull'], cwd=self.local)
715 if p.returncode != 0:
716 raise VCSException("Hg pull failed", p.output)
717 self.refreshed = True
719 rev = str(rev if rev else 'default')
722 p = SilentPopen(['hg', 'update', '-C', rev], cwd=self.local)
723 if p.returncode != 0:
724 raise VCSException("Hg checkout of '%s' failed" % rev, p.output)
725 p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local)
726 # Also delete untracked files, we have to enable purge extension for that:
727 if "'purge' is provided by the following extension" in p.output:
728 with open(self.local + "/.hg/hgrc", "a") as myfile:
729 myfile.write("\n[extensions]\nhgext.purge=\n")
730 p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local)
731 if p.returncode != 0:
732 raise VCSException("HG purge failed", p.output)
733 elif p.returncode != 0:
734 raise VCSException("HG purge failed", p.output)
737 p = SilentPopen(['hg', 'tags', '-q'], cwd=self.local)
738 return p.output.splitlines()[1:]
746 def gotorevisionx(self, rev):
747 if not os.path.exists(self.local):
748 p = SilentPopen(['bzr', 'branch', self.remote, self.local])
749 if p.returncode != 0:
750 self.clone_failed = True
751 raise VCSException("Bzr branch failed", p.output)
753 p = SilentPopen(['bzr', 'clean-tree', '--force', '--unknown', '--ignored'], cwd=self.local)
754 if p.returncode != 0:
755 raise VCSException("Bzr revert failed", p.output)
756 if not self.refreshed:
757 p = SilentPopen(['bzr', 'pull'], cwd=self.local)
758 if p.returncode != 0:
759 raise VCSException("Bzr update failed", p.output)
760 self.refreshed = True
762 revargs = list(['-r', rev] if rev else [])
763 p = SilentPopen(['bzr', 'revert'] + revargs, cwd=self.local)
764 if p.returncode != 0:
765 raise VCSException("Bzr revert of '%s' failed" % rev, p.output)
768 p = SilentPopen(['bzr', 'tags'], cwd=self.local)
769 return [tag.split(' ')[0].strip() for tag in
770 p.output.splitlines()]
773 def retrieve_string(app_dir, string, xmlfiles=None):
776 os.path.join(app_dir, 'res'),
777 os.path.join(app_dir, 'src/main'),
782 for res_dir in res_dirs:
783 for r, d, f in os.walk(res_dir):
784 if r.endswith('/values'):
785 xmlfiles += [os.path.join(r, x) for x in f if x.endswith('.xml')]
788 if string.startswith('@string/'):
789 string_search = re.compile(r'.*name="' + string[8:] + '".*?>([^<]+?)<.*').search
790 elif string.startswith('&') and string.endswith(';'):
791 string_search = re.compile(r'.*<!ENTITY.*' + string[1:-1] + '.*?"([^"]+?)".*>').search
793 if string_search is not None:
794 for xmlfile in xmlfiles:
795 for line in file(xmlfile):
796 matches = string_search(line)
798 return retrieve_string(app_dir, matches.group(1), xmlfiles)
801 return string.replace("\\'", "'")
804 # Return list of existing files that will be used to find the highest vercode
805 def manifest_paths(app_dir, flavour):
807 possible_manifests = \
808 [os.path.join(app_dir, 'AndroidManifest.xml'),
809 os.path.join(app_dir, 'src', 'main', 'AndroidManifest.xml'),
810 os.path.join(app_dir, 'src', 'AndroidManifest.xml'),
811 os.path.join(app_dir, 'build.gradle')]
814 possible_manifests.append(
815 os.path.join(app_dir, 'src', flavour, 'AndroidManifest.xml'))
817 return [path for path in possible_manifests if os.path.isfile(path)]
820 # Retrieve the package name. Returns the name, or None if not found.
821 def fetch_real_name(app_dir, flavour):
822 app_search = re.compile(r'.*<application.*').search
823 name_search = re.compile(r'.*android:label="([^"]+)".*').search
825 for f in manifest_paths(app_dir, flavour):
826 if not has_extension(f, 'xml'):
828 logging.debug("fetch_real_name: Checking manifest at " + f)
834 matches = name_search(line)
836 stringname = matches.group(1)
837 logging.debug("fetch_real_name: using string " + stringname)
838 result = retrieve_string(app_dir, stringname)
840 result = result.strip()
845 # Retrieve the version name
846 def version_name(original, app_dir, flavour):
847 for f in manifest_paths(app_dir, flavour):
848 if not has_extension(f, 'xml'):
850 string = retrieve_string(app_dir, original)
856 def get_library_references(root_dir):
858 proppath = os.path.join(root_dir, 'project.properties')
859 if not os.path.isfile(proppath):
861 with open(proppath) as f:
862 for line in f.readlines():
863 if not line.startswith('android.library.reference.'):
865 path = line.split('=')[1].strip()
866 relpath = os.path.join(root_dir, path)
867 if not os.path.isdir(relpath):
869 logging.info("Found subproject at %s" % path)
870 libraries.append(path)
874 def ant_subprojects(root_dir):
875 subprojects = get_library_references(root_dir)
876 for subpath in subprojects:
877 subrelpath = os.path.join(root_dir, subpath)
878 for p in get_library_references(subrelpath):
879 relp = os.path.normpath(os.path.join(subpath, p))
880 if relp not in subprojects:
881 subprojects.insert(0, relp)
885 def remove_debuggable_flags(root_dir):
886 # Remove forced debuggable flags
887 logging.info("Removing debuggable flags")
888 for root, dirs, files in os.walk(root_dir):
889 if 'AndroidManifest.xml' in files:
890 path = os.path.join(root, 'AndroidManifest.xml')
891 p = FDroidPopen(['sed', '-i', 's/android:debuggable="[^"]*"//g', path])
892 if p.returncode != 0:
893 raise BuildException("Failed to remove debuggable flags of %s" % path)
896 # Extract some information from the AndroidManifest.xml at the given path.
897 # Returns (version, vercode, package), any or all of which might be None.
898 # All values returned are strings.
899 def parse_androidmanifests(paths, ignoreversions=None):
902 return (None, None, None)
904 vcsearch = re.compile(r'.*:versionCode="([0-9]+?)".*').search
905 vnsearch = re.compile(r'.*:versionName="([^"]+?)".*').search
906 psearch = re.compile(r'.*package="([^"]+)".*').search
908 vcsearch_g = re.compile(r'.*versionCode *=* *["\']*([0-9]+)["\']*').search
909 vnsearch_g = re.compile(r'.*versionName *=* *(["\'])((?:(?=(\\?))\3.)*?)\1.*').search
910 psearch_g = re.compile(r'.*packageName *=* *["\']([^"]+)["\'].*').search
912 ignoresearch = re.compile(ignoreversions).search if ignoreversions else None
920 gradle = has_extension(path, 'gradle')
923 # Remember package name, may be defined separately from version+vercode
924 package = max_package
926 for line in file(path):
929 matches = psearch_g(line)
931 matches = psearch(line)
933 package = matches.group(1)
936 matches = vnsearch_g(line)
938 matches = vnsearch(line)
940 version = matches.group(2 if gradle else 1)
943 matches = vcsearch_g(line)
945 matches = vcsearch(line)
947 vercode = matches.group(1)
949 # Always grab the package name and version name in case they are not
950 # together with the highest version code
951 if max_package is None and package is not None:
952 max_package = package
953 if max_version is None and version is not None:
954 max_version = version
956 if max_vercode is None or (vercode is not None and vercode > max_vercode):
957 if not ignoresearch or not ignoresearch(version):
958 if version is not None:
959 max_version = version
960 if vercode is not None:
961 max_vercode = vercode
962 if package is not None:
963 max_package = package
965 max_version = "Ignore"
967 if max_version is None:
968 max_version = "Unknown"
970 return (max_version, max_vercode, max_package)
973 class _FDroidException(Exception):
974 def __init__(self, value, detail=None):
978 def get_wikitext(self):
979 ret = repr(self.value) + "\n"
983 txt = self.detail[-8192:] if len(self.detail) > 8192 else self.detail
991 ret += "\n==== detail begin ====\n%s\n==== detail end ====" % self.detail.strip()
995 class VCSException(_FDroidException):
999 class BuildException(_FDroidException):
1003 # Get the specified source library.
1004 # Returns the path to it. Normally this is the path to be used when referencing
1005 # it, which may be a subdirectory of the actual project. If you want the base
1006 # directory of the project, pass 'basepath=True'.
1007 def getsrclib(spec, srclib_dir, srclibpaths=[], subdir=None,
1008 basepath=False, raw=False, prepare=True, preponly=False):
1016 name, ref = spec.split('@')
1018 number, name = name.split(':', 1)
1020 name, subdir = name.split('/', 1)
1022 if name not in metadata.srclibs:
1023 raise BuildException('srclib ' + name + ' not found.')
1025 srclib = metadata.srclibs[name]
1027 sdir = os.path.join(srclib_dir, name)
1030 vcs = getvcs(srclib["Repo Type"], srclib["Repo"], sdir)
1031 vcs.srclib = (name, number, sdir)
1033 vcs.gotorevision(ref)
1040 libdir = os.path.join(sdir, subdir)
1041 elif srclib["Subdir"]:
1042 for subdir in srclib["Subdir"]:
1043 libdir_candidate = os.path.join(sdir, subdir)
1044 if os.path.exists(libdir_candidate):
1045 libdir = libdir_candidate
1051 if srclib["Srclibs"]:
1053 for lib in srclib["Srclibs"].replace(';', ',').split(','):
1055 for t in srclibpaths:
1060 raise BuildException('Missing recursive srclib %s for %s' % (
1062 place_srclib(libdir, n, s_tuple[2])
1065 remove_signing_keys(sdir)
1066 remove_debuggable_flags(sdir)
1070 if srclib["Prepare"]:
1071 cmd = replace_config_vars(srclib["Prepare"])
1073 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=libdir)
1074 if p.returncode != 0:
1075 raise BuildException("Error running prepare command for srclib %s"
1081 return (name, number, libdir)
1084 # Prepare the source code for a particular build
1085 # 'vcs' - the appropriate vcs object for the application
1086 # 'app' - the application details from the metadata
1087 # 'build' - the build details from the metadata
1088 # 'build_dir' - the path to the build directory, usually
1090 # 'srclib_dir' - the path to the source libraries directory, usually
1092 # 'extlib_dir' - the path to the external libraries directory, usually
1094 # Returns the (root, srclibpaths) where:
1095 # 'root' is the root directory, which may be the same as 'build_dir' or may
1096 # be a subdirectory of it.
1097 # 'srclibpaths' is information on the srclibs being used
1098 def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=False):
1100 # Optionally, the actual app source can be in a subdirectory
1102 root_dir = os.path.join(build_dir, build['subdir'])
1104 root_dir = build_dir
1106 # Get a working copy of the right revision
1107 logging.info("Getting source for revision " + build['commit'])
1108 vcs.gotorevision(build['commit'])
1110 # Initialise submodules if requred
1111 if build['submodules']:
1112 logging.info("Initialising submodules")
1113 vcs.initsubmodules()
1115 # Check that a subdir (if we're using one) exists. This has to happen
1116 # after the checkout, since it might not exist elsewhere
1117 if not os.path.exists(root_dir):
1118 raise BuildException('Missing subdir ' + root_dir)
1120 # Run an init command if one is required
1122 cmd = replace_config_vars(build['init'])
1123 logging.info("Running 'init' commands in %s" % root_dir)
1125 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
1126 if p.returncode != 0:
1127 raise BuildException("Error running init command for %s:%s" %
1128 (app['id'], build['version']), p.output)
1130 # Apply patches if any
1132 logging.info("Applying patches")
1133 for patch in build['patch']:
1134 patch = patch.strip()
1135 logging.info("Applying " + patch)
1136 patch_path = os.path.join('metadata', app['id'], patch)
1137 p = FDroidPopen(['patch', '-p1', '-i', os.path.abspath(patch_path)], cwd=build_dir)
1138 if p.returncode != 0:
1139 raise BuildException("Failed to apply patch %s" % patch_path)
1141 # Get required source libraries
1143 if build['srclibs']:
1144 logging.info("Collecting source libraries")
1145 for lib in build['srclibs']:
1146 srclibpaths.append(getsrclib(lib, srclib_dir, srclibpaths,
1149 for name, number, libpath in srclibpaths:
1150 place_srclib(root_dir, int(number) if number else None, libpath)
1152 basesrclib = vcs.getsrclib()
1153 # If one was used for the main source, add that too.
1155 srclibpaths.append(basesrclib)
1157 # Update the local.properties file
1158 localprops = [os.path.join(build_dir, 'local.properties')]
1160 localprops += [os.path.join(root_dir, 'local.properties')]
1161 for path in localprops:
1162 if not os.path.isfile(path):
1164 logging.info("Updating properties file at %s" % path)
1169 # Fix old-fashioned 'sdk-location' by copying
1170 # from sdk.dir, if necessary
1171 if build['oldsdkloc']:
1172 sdkloc = re.match(r".*^sdk.dir=(\S+)$.*", props,
1173 re.S | re.M).group(1)
1174 props += "sdk-location=%s\n" % sdkloc
1176 props += "sdk.dir=%s\n" % config['sdk_path']
1177 props += "sdk-location=%s\n" % config['sdk_path']
1178 if 'ndk_path' in config:
1180 props += "ndk.dir=%s\n" % config['ndk_path']
1181 props += "ndk-location=%s\n" % config['ndk_path']
1182 # Add java.encoding if necessary
1183 if build['encoding']:
1184 props += "java.encoding=%s\n" % build['encoding']
1190 if build['type'] == 'gradle':
1191 flavour = build['gradle'].split('@')[0]
1192 if flavour in ['main', 'yes', '']:
1195 version_regex = re.compile(r".*'com\.android\.tools\.build:gradle:([^\.]+\.[^\.]+).*'.*")
1196 gradlepluginver = None
1198 gradle_files = [os.path.join(root_dir, 'build.gradle')]
1200 # Parent dir build.gradle
1201 parent_dir = os.path.normpath(os.path.join(root_dir, '..'))
1202 if parent_dir.startswith(build_dir):
1203 gradle_files.append(os.path.join(parent_dir, 'build.gradle'))
1205 # Gradle execution dir build.gradle
1206 if '@' in build['gradle']:
1207 gradle_file = os.path.join(root_dir, build['gradle'].split('@', 1)[1], 'build.gradle')
1208 gradle_file = os.path.normpath(gradle_file)
1209 if gradle_file not in gradle_files:
1210 gradle_files.append(gradle_file)
1212 for path in gradle_files:
1215 if not os.path.isfile(path):
1217 with open(path) as f:
1219 match = version_regex.match(line)
1221 gradlepluginver = match.group(1)
1225 build['gradlepluginver'] = LooseVersion(gradlepluginver)
1227 logging.warn("Could not fetch the gradle plugin version, defaulting to 0.11")
1228 build['gradlepluginver'] = LooseVersion('0.11')
1231 n = build["target"].split('-')[1]
1232 FDroidPopen(['sed', '-i',
1233 's@compileSdkVersion *[0-9]*@compileSdkVersion ' + n + '@g',
1237 # Remove forced debuggable flags
1238 remove_debuggable_flags(root_dir)
1240 # Insert version code and number into the manifest if necessary
1241 if build['forceversion']:
1242 logging.info("Changing the version name")
1243 for path in manifest_paths(root_dir, flavour):
1244 if not os.path.isfile(path):
1246 if has_extension(path, 'xml'):
1247 p = SilentPopen(['sed', '-i',
1248 's/android:versionName="[^"]*"/android:versionName="'
1249 + build['version'] + '"/g',
1251 if p.returncode != 0:
1252 raise BuildException("Failed to amend manifest")
1253 elif has_extension(path, 'gradle'):
1254 p = SilentPopen(['sed', '-i',
1255 's/versionName *=* *"[^"]*"/versionName = "'
1256 + build['version'] + '"/g',
1258 if p.returncode != 0:
1259 raise BuildException("Failed to amend build.gradle")
1260 if build['forcevercode']:
1261 logging.info("Changing the version code")
1262 for path in manifest_paths(root_dir, flavour):
1263 if not os.path.isfile(path):
1265 if has_extension(path, 'xml'):
1266 p = SilentPopen(['sed', '-i',
1267 's/android:versionCode="[^"]*"/android:versionCode="'
1268 + build['vercode'] + '"/g',
1270 if p.returncode != 0:
1271 raise BuildException("Failed to amend manifest")
1272 elif has_extension(path, 'gradle'):
1273 p = SilentPopen(['sed', '-i',
1274 's/versionCode *=* *[0-9]*/versionCode = '
1275 + build['vercode'] + '/g',
1277 if p.returncode != 0:
1278 raise BuildException("Failed to amend build.gradle")
1280 # Delete unwanted files
1282 logging.info("Removing specified files")
1283 for part in getpaths(build_dir, build, 'rm'):
1284 dest = os.path.join(build_dir, part)
1285 logging.info("Removing {0}".format(part))
1286 if os.path.lexists(dest):
1287 if os.path.islink(dest):
1288 SilentPopen(['unlink ' + dest], shell=True)
1290 SilentPopen(['rm -rf ' + dest], shell=True)
1292 logging.info("...but it didn't exist")
1294 remove_signing_keys(build_dir)
1296 # Add required external libraries
1297 if build['extlibs']:
1298 logging.info("Collecting prebuilt libraries")
1299 libsdir = os.path.join(root_dir, 'libs')
1300 if not os.path.exists(libsdir):
1302 for lib in build['extlibs']:
1304 logging.info("...installing extlib {0}".format(lib))
1305 libf = os.path.basename(lib)
1306 libsrc = os.path.join(extlib_dir, lib)
1307 if not os.path.exists(libsrc):
1308 raise BuildException("Missing extlib file {0}".format(libsrc))
1309 shutil.copyfile(libsrc, os.path.join(libsdir, libf))
1311 # Run a pre-build command if one is required
1312 if build['prebuild']:
1313 logging.info("Running 'prebuild' commands in %s" % root_dir)
1315 cmd = replace_config_vars(build['prebuild'])
1317 # Substitute source library paths into prebuild commands
1318 for name, number, libpath in srclibpaths:
1319 libpath = os.path.relpath(libpath, root_dir)
1320 cmd = cmd.replace('$$' + name + '$$', libpath)
1322 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
1323 if p.returncode != 0:
1324 raise BuildException("Error running prebuild command for %s:%s" %
1325 (app['id'], build['version']), p.output)
1327 # Generate (or update) the ant build file, build.xml...
1328 if build['update'] and build['update'] != ['no'] and build['type'] == 'ant':
1329 parms = [config['android'], 'update', 'lib-project']
1330 lparms = [config['android'], 'update', 'project']
1333 parms += ['-t', build['target']]
1334 lparms += ['-t', build['target']]
1335 if build['update'] == ['auto']:
1336 update_dirs = ant_subprojects(root_dir) + ['.']
1338 update_dirs = build['update']
1340 for d in update_dirs:
1341 subdir = os.path.join(root_dir, d)
1343 logging.info("Updating main project")
1344 cmd = parms + ['-p', d]
1346 logging.info("Updating subproject %s" % d)
1347 cmd = lparms + ['-p', d]
1348 p = FDroidPopen(cmd, cwd=root_dir)
1349 # Check to see whether an error was returned without a proper exit
1350 # code (this is the case for the 'no target set or target invalid'
1352 if p.returncode != 0 or p.output.startswith("Error: "):
1353 raise BuildException("Failed to update project at %s" % d, p.output)
1354 # Clean update dirs via ant
1356 logging.info("Cleaning subproject %s" % d)
1357 p = FDroidPopen(['ant', 'clean'], cwd=subdir)
1359 return (root_dir, srclibpaths)
1362 # Split and extend via globbing the paths from a field
1363 def getpaths(build_dir, build, field):
1365 for p in build[field]:
1367 full_path = os.path.join(build_dir, p)
1368 full_path = os.path.normpath(full_path)
1369 paths += [r[len(build_dir) + 1:] for r in glob.glob(full_path)]
1373 # Scan the source code in the given directory (and all subdirectories)
1374 # and return the number of fatal problems encountered
1375 def scan_source(build_dir, root_dir, thisbuild):
1379 # Common known non-free blobs (always lower case):
1381 re.compile(r'flurryagent', re.IGNORECASE),
1382 re.compile(r'paypal.*mpl', re.IGNORECASE),
1383 re.compile(r'libgoogleanalytics', re.IGNORECASE),
1384 re.compile(r'admob.*sdk.*android', re.IGNORECASE),
1385 re.compile(r'googleadview', re.IGNORECASE),
1386 re.compile(r'googleadmobadssdk', re.IGNORECASE),
1387 re.compile(r'google.*play.*services', re.IGNORECASE),
1388 re.compile(r'crittercism', re.IGNORECASE),
1389 re.compile(r'heyzap', re.IGNORECASE),
1390 re.compile(r'jpct.*ae', re.IGNORECASE),
1391 re.compile(r'youtubeandroidplayerapi', re.IGNORECASE),
1392 re.compile(r'bugsense', re.IGNORECASE),
1393 re.compile(r'crashlytics', re.IGNORECASE),
1394 re.compile(r'ouya.*sdk', re.IGNORECASE),
1395 re.compile(r'libspen23', re.IGNORECASE),
1398 scanignore = getpaths(build_dir, thisbuild, 'scanignore')
1399 scandelete = getpaths(build_dir, thisbuild, 'scandelete')
1402 ms = magic.open(magic.MIME_TYPE)
1404 except AttributeError:
1408 for i in scanignore:
1409 if fd.startswith(i):
1414 for i in scandelete:
1415 if fd.startswith(i):
1419 def removeproblem(what, fd, fp):
1420 logging.info('Removing %s at %s' % (what, fd))
1423 def warnproblem(what, fd):
1424 logging.warn('Found %s at %s' % (what, fd))
1426 def handleproblem(what, fd, fp):
1428 removeproblem(what, fd, fp)
1430 logging.error('Found %s at %s' % (what, fd))
1434 def insidedir(path, dirname):
1435 return path.endswith('/%s' % dirname) or '/%s/' % dirname in path
1437 # Iterate through all files in the source code
1438 for r, d, f in os.walk(build_dir):
1440 if any(insidedir(r, d) for d in ('.hg', '.git', '.svn', '.bzr')):
1445 # Path (relative) to the file
1446 fp = os.path.join(r, curfile)
1447 fd = fp[len(build_dir) + 1:]
1449 # Check if this file has been explicitly excluded from scanning
1453 mime = magic.from_file(fp, mime=True) if ms is None else ms.file(fp)
1455 if mime == 'application/x-sharedlib':
1456 count += handleproblem('shared library', fd, fp)
1458 elif mime == 'application/x-archive':
1459 count += handleproblem('static library', fd, fp)
1461 elif mime == 'application/x-executable':
1462 count += handleproblem('binary executable', fd, fp)
1464 elif mime == 'application/x-java-applet':
1465 count += handleproblem('Java compiled class', fd, fp)
1470 'application/java-archive',
1471 'application/octet-stream',
1475 if has_extension(fp, 'apk'):
1476 removeproblem('APK file', fd, fp)
1478 elif has_extension(fp, 'jar'):
1480 if any(suspect.match(curfile) for suspect in usual_suspects):
1481 count += handleproblem('usual supect', fd, fp)
1483 warnproblem('JAR file', fd)
1485 elif has_extension(fp, 'zip'):
1486 warnproblem('ZIP file', fd)
1489 warnproblem('unknown compressed or binary file', fd)
1491 elif has_extension(fp, 'java'):
1492 for line in file(fp):
1493 if 'DexClassLoader' in line:
1494 count += handleproblem('DexClassLoader', fd, fp)
1499 # Presence of a jni directory without buildjni=yes might
1500 # indicate a problem (if it's not a problem, explicitly use
1501 # buildjni=no to bypass this check)
1502 if (os.path.exists(os.path.join(root_dir, 'jni')) and
1503 not thisbuild['buildjni']):
1504 logging.error('Found jni directory, but buildjni is not enabled. Set it to \'no\' to ignore.')
1513 self.path = os.path.join('stats', 'known_apks.txt')
1515 if os.path.exists(self.path):
1516 for line in file(self.path):
1517 t = line.rstrip().split(' ')
1519 self.apks[t[0]] = (t[1], None)
1521 self.apks[t[0]] = (t[1], time.strptime(t[2], '%Y-%m-%d'))
1522 self.changed = False
1524 def writeifchanged(self):
1526 if not os.path.exists('stats'):
1528 f = open(self.path, 'w')
1530 for apk, app in self.apks.iteritems():
1532 line = apk + ' ' + appid
1534 line += ' ' + time.strftime('%Y-%m-%d', added)
1536 for line in sorted(lst):
1537 f.write(line + '\n')
1540 # Record an apk (if it's new, otherwise does nothing)
1541 # Returns the date it was added.
1542 def recordapk(self, apk, app):
1543 if apk not in self.apks:
1544 self.apks[apk] = (app, time.gmtime(time.time()))
1546 _, added = self.apks[apk]
1549 # Look up information - given the 'apkname', returns (app id, date added/None).
1550 # Or returns None for an unknown apk.
1551 def getapp(self, apkname):
1552 if apkname in self.apks:
1553 return self.apks[apkname]
1556 # Get the most recent 'num' apps added to the repo, as a list of package ids
1557 # with the most recent first.
1558 def getlatest(self, num):
1560 for apk, app in self.apks.iteritems():
1564 if apps[appid] > added:
1568 sortedapps = sorted(apps.iteritems(), key=operator.itemgetter(1))[-num:]
1569 lst = [app for app, _ in sortedapps]
1574 def isApkDebuggable(apkfile, config):
1575 """Returns True if the given apk file is debuggable
1577 :param apkfile: full path to the apk to check"""
1579 p = SilentPopen([os.path.join(config['sdk_path'], 'build-tools',
1580 config['build_tools'], 'aapt'),
1581 'dump', 'xmltree', apkfile, 'AndroidManifest.xml'])
1582 if p.returncode != 0:
1583 logging.critical("Failed to get apk manifest information")
1585 for line in p.output.splitlines():
1586 if 'android:debuggable' in line and not line.endswith('0x0'):
1591 class AsynchronousFileReader(threading.Thread):
1593 Helper class to implement asynchronous reading of a file
1594 in a separate thread. Pushes read lines on a queue to
1595 be consumed in another thread.
1598 def __init__(self, fd, queue):
1599 assert isinstance(queue, Queue.Queue)
1600 assert callable(fd.readline)
1601 threading.Thread.__init__(self)
1606 '''The body of the tread: read lines and put them on the queue.'''
1607 for line in iter(self._fd.readline, ''):
1608 self._queue.put(line)
1611 '''Check whether there is no more content to expect.'''
1612 return not self.is_alive() and self._queue.empty()
1620 def SilentPopen(commands, cwd=None, shell=False):
1621 return FDroidPopen(commands, cwd=cwd, shell=shell, output=False)
1624 def FDroidPopen(commands, cwd=None, shell=False, output=True):
1626 Run a command and capture the possibly huge output.
1628 :param commands: command and argument list like in subprocess.Popen
1629 :param cwd: optionally specifies a working directory
1630 :returns: A PopenResult.
1636 cwd = os.path.normpath(cwd)
1637 logging.debug("Directory: %s" % cwd)
1638 logging.debug("> %s" % ' '.join(commands))
1640 result = PopenResult()
1641 p = subprocess.Popen(commands, cwd=cwd, shell=shell, env=env,
1642 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
1644 stdout_queue = Queue.Queue()
1645 stdout_reader = AsynchronousFileReader(p.stdout, stdout_queue)
1646 stdout_reader.start()
1648 # Check the queue for output (until there is no more to get)
1649 while not stdout_reader.eof():
1650 while not stdout_queue.empty():
1651 line = stdout_queue.get()
1653 # Output directly to console
1654 sys.stdout.write(line)
1656 result.output += line
1661 result.returncode = p.returncode
1665 def remove_signing_keys(build_dir):
1666 comment = re.compile(r'[ ]*//')
1667 signing_configs = re.compile(r'^[\t ]*signingConfigs[ \t]*{[ \t]*$')
1669 re.compile(r'^[\t ]*signingConfig [^ ]*$'),
1670 re.compile(r'.*android\.signingConfigs\.[^{]*$'),
1671 re.compile(r'.*variant\.outputFile = .*'),
1672 re.compile(r'.*\.readLine\(.*'),
1674 for root, dirs, files in os.walk(build_dir):
1675 if 'build.gradle' in files:
1676 path = os.path.join(root, 'build.gradle')
1678 with open(path, "r") as o:
1679 lines = o.readlines()
1684 with open(path, "w") as o:
1686 if comment.match(line):
1690 opened += line.count('{')
1691 opened -= line.count('}')
1694 if signing_configs.match(line):
1699 if any(s.match(line) for s in line_matches):
1707 logging.info("Cleaned build.gradle of keysigning configs at %s" % path)
1710 'project.properties',
1712 'default.properties',
1715 if propfile in files:
1716 path = os.path.join(root, propfile)
1718 with open(path, "r") as o:
1719 lines = o.readlines()
1723 with open(path, "w") as o:
1725 if any(line.startswith(s) for s in ('key.store', 'key.alias')):
1732 logging.info("Cleaned %s of keysigning configs at %s" % (propfile, path))
1735 def replace_config_vars(cmd):
1736 cmd = cmd.replace('$$SDK$$', config['sdk_path'])
1737 cmd = cmd.replace('$$NDK$$', config['ndk_path'])
1738 cmd = cmd.replace('$$MVN3$$', config['mvn3'])
1742 def place_srclib(root_dir, number, libpath):
1745 relpath = os.path.relpath(libpath, root_dir)
1746 proppath = os.path.join(root_dir, 'project.properties')
1749 if os.path.isfile(proppath):
1750 with open(proppath, "r") as o:
1751 lines = o.readlines()
1753 with open(proppath, "w") as o:
1756 if line.startswith('android.library.reference.%d=' % number):
1757 o.write('android.library.reference.%d=%s\n' % (number, relpath))
1762 o.write('android.library.reference.%d=%s\n' % (number, relpath))