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
43 'sdk_path': "$ANDROID_HOME",
44 'ndk_path': "$ANDROID_NDK",
45 'build_tools': "21.1.2",
49 'sync_from_local_copy_dir': False,
50 'make_current_version_link': True,
51 'current_version_name_source': 'Name',
52 'update_stats': False,
56 'stats_to_carbon': False,
58 'build_server_always': False,
59 'keystore': os.path.join("$HOME", '.local', 'share', 'fdroidserver', 'keystore.jks'),
60 'smartcardoptions': [],
66 'repo_url': "https://MyFirstFDroidRepo.org/fdroid/repo",
67 'repo_name': "My First FDroid Repo Demo",
68 'repo_icon': "fdroid-icon.png",
69 'repo_description': '''
70 This is a repository of apps to be used with FDroid. Applications in this
71 repository are either official binaries built by the original application
72 developers, or are binaries built from source by the admin of f-droid.org
73 using the tools on https://gitlab.com/u/fdroid.
79 def fill_config_defaults(thisconfig):
80 for k, v in default_config.items():
81 if k not in thisconfig:
84 # Expand paths (~users and $vars)
85 for k in ['sdk_path', 'ndk_path', 'ant', 'mvn3', 'gradle', 'keystore', 'repo_icon']:
88 v = os.path.expanduser(v)
89 v = os.path.expandvars(v)
92 thisconfig[k + '_orig'] = orig
95 def read_config(opts, config_file='config.py'):
96 """Read the repository config
98 The config is read from config_file, which is in the current directory when
99 any of the repo management commands are used.
101 global config, options, env
103 if config is not None:
105 if not os.path.isfile(config_file):
106 logging.critical("Missing config file - is this a repo directory?")
113 logging.debug("Reading %s" % config_file)
114 execfile(config_file, config)
116 # smartcardoptions must be a list since its command line args for Popen
117 if 'smartcardoptions' in config:
118 config['smartcardoptions'] = config['smartcardoptions'].split(' ')
119 elif 'keystore' in config and config['keystore'] == 'NONE':
120 # keystore='NONE' means use smartcard, these are required defaults
121 config['smartcardoptions'] = ['-storetype', 'PKCS11', '-providerName',
122 'SunPKCS11-OpenSC', '-providerClass',
123 'sun.security.pkcs11.SunPKCS11',
124 '-providerArg', 'opensc-fdroid.cfg']
126 if any(k in config for k in ["keystore", "keystorepass", "keypass"]):
127 st = os.stat(config_file)
128 if st.st_mode & stat.S_IRWXG or st.st_mode & stat.S_IRWXO:
129 logging.warn("unsafe permissions on {0} (should be 0600)!".format(config_file))
131 fill_config_defaults(config)
133 # There is no standard, so just set up the most common environment
136 for n in ['ANDROID_HOME', 'ANDROID_SDK']:
137 env[n] = config['sdk_path']
138 for n in ['ANDROID_NDK', 'NDK']:
139 env[n] = config['ndk_path']
141 for k in ["keystorepass", "keypass"]:
143 write_password_file(k)
145 for k in ["repo_description", "archive_description"]:
147 config[k] = clean_description(config[k])
149 if 'serverwebroot' in config:
150 if isinstance(config['serverwebroot'], basestring):
151 roots = [config['serverwebroot']]
152 elif all(isinstance(item, basestring) for item in config['serverwebroot']):
153 roots = config['serverwebroot']
155 raise TypeError('only accepts strings, lists, and tuples')
157 for rootstr in roots:
158 # since this is used with rsync, where trailing slashes have
159 # meaning, ensure there is always a trailing slash
160 if rootstr[-1] != '/':
162 rootlist.append(rootstr.replace('//', '/'))
163 config['serverwebroot'] = rootlist
168 def find_sdk_tools_cmd(cmd):
169 '''find a working path to a tool from the Android SDK'''
172 if config is not None and 'sdk_path' in config and os.path.exists(config['sdk_path']):
173 # try to find a working path to this command, in all the recent possible paths
174 if 'build_tools' in config:
175 build_tools = os.path.join(config['sdk_path'], 'build-tools')
176 # if 'build_tools' was manually set and exists, check only that one
177 configed_build_tools = os.path.join(build_tools, config['build_tools'])
178 if os.path.exists(configed_build_tools):
179 tooldirs.append(configed_build_tools)
181 # no configed version, so hunt known paths for it
182 for f in sorted(os.listdir(build_tools), reverse=True):
183 if os.path.isdir(os.path.join(build_tools, f)):
184 tooldirs.append(os.path.join(build_tools, f))
185 tooldirs.append(build_tools)
186 sdk_tools = os.path.join(config['sdk_path'], 'tools')
187 if os.path.exists(sdk_tools):
188 tooldirs.append(sdk_tools)
189 sdk_platform_tools = os.path.join(config['sdk_path'], 'platform-tools')
190 if os.path.exists(sdk_platform_tools):
191 tooldirs.append(sdk_platform_tools)
192 tooldirs.append('/usr/bin')
194 if os.path.isfile(os.path.join(d, cmd)):
195 return os.path.join(d, cmd)
196 # did not find the command, exit with error message
197 ensure_build_tools_exists(config)
200 def test_sdk_exists(thisconfig):
201 if 'sdk_path' not in thisconfig:
202 if 'aapt' in thisconfig and os.path.isfile(thisconfig['aapt']):
205 logging.error("'sdk_path' not set in config.py!")
207 if thisconfig['sdk_path'] == default_config['sdk_path']:
208 logging.error('No Android SDK found!')
209 logging.error('You can use ANDROID_HOME to set the path to your SDK, i.e.:')
210 logging.error('\texport ANDROID_HOME=/opt/android-sdk')
212 if not os.path.exists(thisconfig['sdk_path']):
213 logging.critical('Android SDK path "' + thisconfig['sdk_path'] + '" does not exist!')
215 if not os.path.isdir(thisconfig['sdk_path']):
216 logging.critical('Android SDK path "' + thisconfig['sdk_path'] + '" is not a directory!')
218 for d in ['build-tools', 'platform-tools', 'tools']:
219 if not os.path.isdir(os.path.join(thisconfig['sdk_path'], d)):
220 logging.critical('Android SDK path "%s" does not contain "%s/"!' % (
221 thisconfig['sdk_path'], d))
226 def ensure_build_tools_exists(thisconfig):
227 if not test_sdk_exists(thisconfig):
229 build_tools = os.path.join(thisconfig['sdk_path'], 'build-tools')
230 versioned_build_tools = os.path.join(build_tools, thisconfig['build_tools'])
231 if not os.path.isdir(versioned_build_tools):
232 logging.critical('Android Build Tools path "'
233 + versioned_build_tools + '" does not exist!')
237 def write_password_file(pwtype, password=None):
239 writes out passwords to a protected file instead of passing passwords as
240 command line argments
242 filename = '.fdroid.' + pwtype + '.txt'
243 fd = os.open(filename, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0600)
245 os.write(fd, config[pwtype])
247 os.write(fd, password)
249 config[pwtype + 'file'] = filename
252 # Given the arguments in the form of multiple appid:[vc] strings, this returns
253 # a dictionary with the set of vercodes specified for each package.
254 def read_pkg_args(args, allow_vercodes=False):
261 if allow_vercodes and ':' in p:
262 package, vercode = p.split(':')
264 package, vercode = p, None
265 if package not in vercodes:
266 vercodes[package] = [vercode] if vercode else []
268 elif vercode and vercode not in vercodes[package]:
269 vercodes[package] += [vercode] if vercode else []
274 # On top of what read_pkg_args does, this returns the whole app metadata, but
275 # limiting the builds list to the builds matching the vercodes specified.
276 def read_app_args(args, allapps, allow_vercodes=False):
278 vercodes = read_pkg_args(args, allow_vercodes)
284 for appid, app in allapps.iteritems():
285 if appid in vercodes:
288 if len(apps) != len(vercodes):
291 logging.critical("No such package: %s" % p)
292 raise FDroidException("Found invalid app ids in arguments")
294 raise FDroidException("No packages specified")
297 for appid, app in apps.iteritems():
301 app['builds'] = [b for b in app['builds'] if b['vercode'] in vc]
302 if len(app['builds']) != len(vercodes[appid]):
304 allvcs = [b['vercode'] for b in app['builds']]
305 for v in vercodes[appid]:
307 logging.critical("No such vercode %s for app %s" % (v, appid))
310 raise FDroidException("Found invalid vercodes for some apps")
315 def has_extension(filename, extension):
316 name, ext = os.path.splitext(filename)
317 ext = ext.lower()[1:]
318 return ext == extension
323 def clean_description(description):
324 'Remove unneeded newlines and spaces from a block of description text'
326 # this is split up by paragraph to make removing the newlines easier
327 for paragraph in re.split(r'\n\n', description):
328 paragraph = re.sub('\r', '', paragraph)
329 paragraph = re.sub('\n', ' ', paragraph)
330 paragraph = re.sub(' {2,}', ' ', paragraph)
331 paragraph = re.sub('^\s*(\w)', r'\1', paragraph)
332 returnstring += paragraph + '\n\n'
333 return returnstring.rstrip('\n')
336 def apknameinfo(filename):
338 filename = os.path.basename(filename)
339 if apk_regex is None:
340 apk_regex = re.compile(r"^(.+)_([0-9]+)\.apk$")
341 m = apk_regex.match(filename)
343 result = (m.group(1), m.group(2))
344 except AttributeError:
345 raise FDroidException("Invalid apk name: %s" % filename)
349 def getapkname(app, build):
350 return "%s_%s.apk" % (app['id'], build['vercode'])
353 def getsrcname(app, build):
354 return "%s_%s_src.tar.gz" % (app['id'], build['vercode'])
361 return app['Auto Name']
366 return '%s (%s)' % (app['Current Version'], app['Current Version Code'])
369 def getvcs(vcstype, remote, local):
371 return vcs_git(remote, local)
372 if vcstype == 'git-svn':
373 return vcs_gitsvn(remote, local)
375 return vcs_hg(remote, local)
377 return vcs_bzr(remote, local)
378 if vcstype == 'srclib':
379 if local != os.path.join('build', 'srclib', remote):
380 raise VCSException("Error: srclib paths are hard-coded!")
381 return getsrclib(remote, os.path.join('build', 'srclib'), raw=True)
383 raise VCSException("Deprecated vcs type 'svn' - please use 'git-svn' instead")
384 raise VCSException("Invalid vcs type " + vcstype)
387 def getsrclibvcs(name):
388 if name not in metadata.srclibs:
389 raise VCSException("Missing srclib " + name)
390 return metadata.srclibs[name]['Repo Type']
394 def __init__(self, remote, local):
396 # svn, git-svn and bzr may require auth
398 if self.repotype() in ('git-svn', 'bzr'):
400 self.username, remote = remote.split('@')
401 if ':' not in self.username:
402 raise VCSException("Password required with username")
403 self.username, self.password = self.username.split(':')
407 self.clone_failed = False
408 self.refreshed = False
414 # Take the local repository to a clean version of the given revision, which
415 # is specificed in the VCS's native format. Beforehand, the repository can
416 # be dirty, or even non-existent. If the repository does already exist
417 # locally, it will be updated from the origin, but only once in the
418 # lifetime of the vcs object.
419 # None is acceptable for 'rev' if you know you are cloning a clean copy of
420 # the repo - otherwise it must specify a valid revision.
421 def gotorevision(self, rev):
423 if self.clone_failed:
424 raise VCSException("Downloading the repository already failed once, not trying again.")
426 # The .fdroidvcs-id file for a repo tells us what VCS type
427 # and remote that directory was created from, allowing us to drop it
428 # automatically if either of those things changes.
429 fdpath = os.path.join(self.local, '..',
430 '.fdroidvcs-' + os.path.basename(self.local))
431 cdata = self.repotype() + ' ' + self.remote
434 if os.path.exists(self.local):
435 if os.path.exists(fdpath):
436 with open(fdpath, 'r') as f:
437 fsdata = f.read().strip()
443 "Repository details for %s changed - deleting" % (
447 logging.info("Repository details for %s missing - deleting" % (
450 shutil.rmtree(self.local)
455 self.gotorevisionx(rev)
456 except FDroidException, e:
459 # If necessary, write the .fdroidvcs file.
460 if writeback and not self.clone_failed:
461 with open(fdpath, 'w') as f:
467 # Derived classes need to implement this. It's called once basic checking
468 # has been performend.
469 def gotorevisionx(self, rev):
470 raise VCSException("This VCS type doesn't define gotorevisionx")
472 # Initialise and update submodules
473 def initsubmodules(self):
474 raise VCSException('Submodules not supported for this vcs type')
476 # Get a list of all known tags
478 raise VCSException('gettags not supported for this vcs type')
480 # Get a list of latest number tags
481 def latesttags(self, number):
482 raise VCSException('latesttags not supported for this vcs type')
484 # Get current commit reference (hash, revision, etc)
486 raise VCSException('getref not supported for this vcs type')
488 # Returns the srclib (name, path) used in setting up the current
499 # If the local directory exists, but is somehow not a git repository, git
500 # will traverse up the directory tree until it finds one that is (i.e.
501 # fdroidserver) and then we'll proceed to destroy it! This is called as
504 p = SilentPopen(['git', 'rev-parse', '--show-toplevel'], cwd=self.local)
505 result = p.output.rstrip()
506 if not result.endswith(self.local):
507 raise VCSException('Repository mismatch')
509 def gotorevisionx(self, rev):
510 if not os.path.exists(self.local):
512 p = FDroidPopen(['git', 'clone', self.remote, self.local])
513 if p.returncode != 0:
514 self.clone_failed = True
515 raise VCSException("Git clone failed", p.output)
519 # Discard any working tree changes
520 p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local)
521 if p.returncode != 0:
522 raise VCSException("Git reset failed", p.output)
523 # Remove untracked files now, in case they're tracked in the target
524 # revision (it happens!)
525 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
526 if p.returncode != 0:
527 raise VCSException("Git clean failed", p.output)
528 if not self.refreshed:
529 # Get latest commits and tags from remote
530 p = FDroidPopen(['git', 'fetch', 'origin'], cwd=self.local)
531 if p.returncode != 0:
532 raise VCSException("Git fetch failed", p.output)
533 p = SilentPopen(['git', 'fetch', '--prune', '--tags', 'origin'], cwd=self.local)
534 if p.returncode != 0:
535 raise VCSException("Git fetch failed", p.output)
536 # Recreate origin/HEAD as git clone would do it, in case it disappeared
537 p = SilentPopen(['git', 'remote', 'set-head', 'origin', '--auto'], cwd=self.local)
538 if p.returncode != 0:
539 lines = p.output.splitlines()
540 if 'Multiple remote HEAD branches' not in lines[0]:
541 raise VCSException("Git remote set-head failed", p.output)
542 branch = lines[1].split(' ')[-1]
543 p2 = SilentPopen(['git', 'remote', 'set-head', 'origin', branch], cwd=self.local)
544 if p2.returncode != 0:
545 raise VCSException("Git remote set-head failed", p.output + '\n' + p2.output)
546 self.refreshed = True
547 # origin/HEAD is the HEAD of the remote, e.g. the "default branch" on
548 # a github repo. Most of the time this is the same as origin/master.
549 rev = rev or 'origin/HEAD'
550 p = SilentPopen(['git', 'checkout', '-f', rev], cwd=self.local)
551 if p.returncode != 0:
552 raise VCSException("Git checkout of '%s' failed" % rev, p.output)
553 # Get rid of any uncontrolled files left behind
554 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
555 if p.returncode != 0:
556 raise VCSException("Git clean failed", p.output)
558 def initsubmodules(self):
560 submfile = os.path.join(self.local, '.gitmodules')
561 if not os.path.isfile(submfile):
562 raise VCSException("No git submodules available")
564 # fix submodules not accessible without an account and public key auth
565 with open(submfile, 'r') as f:
566 lines = f.readlines()
567 with open(submfile, 'w') as f:
569 if 'git@github.com' in line:
570 line = line.replace('git@github.com:', 'https://github.com/')
574 ['git', 'reset', '--hard'],
575 ['git', 'clean', '-dffx'],
577 p = SilentPopen(['git', 'submodule', 'foreach', '--recursive'] + cmd, cwd=self.local)
578 if p.returncode != 0:
579 raise VCSException("Git submodule reset failed", p.output)
580 p = SilentPopen(['git', 'submodule', 'sync'], cwd=self.local)
581 if p.returncode != 0:
582 raise VCSException("Git submodule sync failed", p.output)
583 p = FDroidPopen(['git', 'submodule', 'update', '--init', '--force', '--recursive'], cwd=self.local)
584 if p.returncode != 0:
585 raise VCSException("Git submodule update failed", p.output)
589 p = SilentPopen(['git', 'tag'], cwd=self.local)
590 return p.output.splitlines()
592 def latesttags(self, alltags, number):
594 p = SilentPopen(['echo "' + '\n'.join(alltags) + '" | '
595 + 'xargs -I@ git log --format=format:"%at @%n" -1 @ | '
596 + 'sort -n | awk \'{print $2}\''],
597 cwd=self.local, shell=True)
598 return p.output.splitlines()[-number:]
601 class vcs_gitsvn(vcs):
606 # Damn git-svn tries to use a graphical password prompt, so we have to
607 # trick it into taking the password from stdin
609 if self.username is None:
611 return ('echo "%s" | DISPLAY="" ' % self.password, ' --username "%s"' % self.username)
613 # If the local directory exists, but is somehow not a git repository, git
614 # will traverse up the directory tree until it finds one that is (i.e.
615 # fdroidserver) and then we'll proceed to destory it! This is called as
618 p = SilentPopen(['git', 'rev-parse', '--show-toplevel'], cwd=self.local)
619 result = p.output.rstrip()
620 if not result.endswith(self.local):
621 raise VCSException('Repository mismatch')
623 def gotorevisionx(self, rev):
624 if not os.path.exists(self.local):
626 gitsvn_cmd = '%sgit svn clone%s' % self.userargs()
627 if ';' in self.remote:
628 remote_split = self.remote.split(';')
629 for i in remote_split[1:]:
630 if i.startswith('trunk='):
631 gitsvn_cmd += ' -T %s' % i[6:]
632 elif i.startswith('tags='):
633 gitsvn_cmd += ' -t %s' % i[5:]
634 elif i.startswith('branches='):
635 gitsvn_cmd += ' -b %s' % i[9:]
636 p = SilentPopen([gitsvn_cmd + " %s %s" % (remote_split[0], self.local)], shell=True)
637 if p.returncode != 0:
638 self.clone_failed = True
639 raise VCSException("Git svn clone failed", p.output)
641 p = SilentPopen([gitsvn_cmd + " %s %s" % (self.remote, self.local)], shell=True)
642 if p.returncode != 0:
643 self.clone_failed = True
644 raise VCSException("Git svn clone failed", p.output)
648 # Discard any working tree changes
649 p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local)
650 if p.returncode != 0:
651 raise VCSException("Git reset failed", p.output)
652 # Remove untracked files now, in case they're tracked in the target
653 # revision (it happens!)
654 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
655 if p.returncode != 0:
656 raise VCSException("Git clean failed", p.output)
657 if not self.refreshed:
658 # Get new commits, branches and tags from repo
659 p = SilentPopen(['%sgit svn fetch %s' % self.userargs()], cwd=self.local, shell=True)
660 if p.returncode != 0:
661 raise VCSException("Git svn fetch failed")
662 p = SilentPopen(['%sgit svn rebase %s' % self.userargs()], cwd=self.local, shell=True)
663 if p.returncode != 0:
664 raise VCSException("Git svn rebase failed", p.output)
665 self.refreshed = True
667 rev = rev or 'master'
669 nospaces_rev = rev.replace(' ', '%20')
670 # Try finding a svn tag
671 for treeish in ['origin/', '']:
672 p = SilentPopen(['git', 'checkout', treeish + 'tags/' + nospaces_rev],
674 if p.returncode == 0:
676 if p.returncode != 0:
677 # No tag found, normal svn rev translation
678 # Translate svn rev into git format
679 rev_split = rev.split('/')
682 for treeish in ['origin/', '']:
683 if len(rev_split) > 1:
684 treeish += rev_split[0]
685 svn_rev = rev_split[1]
688 # if no branch is specified, then assume trunk (i.e. 'master' branch):
692 svn_rev = svn_rev if svn_rev[0] == 'r' else 'r' + svn_rev
694 p = SilentPopen(['git', 'svn', 'find-rev', '--before', svn_rev, treeish],
696 git_rev = p.output.rstrip()
698 if p.returncode == 0 and git_rev:
701 if p.returncode != 0 or not git_rev:
702 # Try a plain git checkout as a last resort
703 p = SilentPopen(['git', 'checkout', rev], cwd=self.local)
704 if p.returncode != 0:
705 raise VCSException("No git treeish found and direct git checkout of '%s' failed" % rev, p.output)
707 # Check out the git rev equivalent to the svn rev
708 p = SilentPopen(['git', 'checkout', git_rev], cwd=self.local)
709 if p.returncode != 0:
710 raise VCSException("Git checkout of '%s' failed" % rev, p.output)
712 # Get rid of any uncontrolled files left behind
713 p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
714 if p.returncode != 0:
715 raise VCSException("Git clean failed", p.output)
719 for treeish in ['origin/', '']:
720 d = os.path.join(self.local, '.git', 'svn', 'refs', 'remotes', treeish, 'tags')
726 p = SilentPopen(['git', 'svn', 'find-rev', 'HEAD'], cwd=self.local)
727 if p.returncode != 0:
729 return p.output.strip()
737 def gotorevisionx(self, rev):
738 if not os.path.exists(self.local):
739 p = SilentPopen(['hg', 'clone', self.remote, self.local])
740 if p.returncode != 0:
741 self.clone_failed = True
742 raise VCSException("Hg clone failed", p.output)
744 p = SilentPopen(['hg status -uS | xargs rm -rf'], cwd=self.local, shell=True)
745 if p.returncode != 0:
746 raise VCSException("Hg clean failed", p.output)
747 if not self.refreshed:
748 p = SilentPopen(['hg', 'pull'], cwd=self.local)
749 if p.returncode != 0:
750 raise VCSException("Hg pull failed", p.output)
751 self.refreshed = True
753 rev = rev or 'default'
756 p = SilentPopen(['hg', 'update', '-C', rev], cwd=self.local)
757 if p.returncode != 0:
758 raise VCSException("Hg checkout of '%s' failed" % rev, p.output)
759 p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local)
760 # Also delete untracked files, we have to enable purge extension for that:
761 if "'purge' is provided by the following extension" in p.output:
762 with open(os.path.join(self.local, '.hg', 'hgrc'), "a") as myfile:
763 myfile.write("\n[extensions]\nhgext.purge=\n")
764 p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local)
765 if p.returncode != 0:
766 raise VCSException("HG purge failed", p.output)
767 elif p.returncode != 0:
768 raise VCSException("HG purge failed", p.output)
771 p = SilentPopen(['hg', 'tags', '-q'], cwd=self.local)
772 return p.output.splitlines()[1:]
780 def gotorevisionx(self, rev):
781 if not os.path.exists(self.local):
782 p = SilentPopen(['bzr', 'branch', self.remote, self.local])
783 if p.returncode != 0:
784 self.clone_failed = True
785 raise VCSException("Bzr branch failed", p.output)
787 p = SilentPopen(['bzr', 'clean-tree', '--force', '--unknown', '--ignored'], cwd=self.local)
788 if p.returncode != 0:
789 raise VCSException("Bzr revert failed", p.output)
790 if not self.refreshed:
791 p = SilentPopen(['bzr', 'pull'], cwd=self.local)
792 if p.returncode != 0:
793 raise VCSException("Bzr update failed", p.output)
794 self.refreshed = True
796 revargs = list(['-r', rev] if rev else [])
797 p = SilentPopen(['bzr', 'revert'] + revargs, cwd=self.local)
798 if p.returncode != 0:
799 raise VCSException("Bzr revert of '%s' failed" % rev, p.output)
802 p = SilentPopen(['bzr', 'tags'], cwd=self.local)
803 return [tag.split(' ')[0].strip() for tag in
804 p.output.splitlines()]
807 def retrieve_string(app_dir, string, xmlfiles=None):
810 os.path.join(app_dir, 'res'),
811 os.path.join(app_dir, 'src', 'main'),
816 for res_dir in res_dirs:
817 for r, d, f in os.walk(res_dir):
818 if os.path.basename(r) == 'values':
819 xmlfiles += [os.path.join(r, x) for x in f if x.endswith('.xml')]
822 if string.startswith('@string/'):
823 string_search = re.compile(r'.*name="' + string[8:] + '".*?>"?([^<]+?)"?<.*').search
824 elif string.startswith('&') and string.endswith(';'):
825 string_search = re.compile(r'.*<!ENTITY.*' + string[1:-1] + '.*?"([^"]+?)".*>').search
827 if string_search is not None:
828 for xmlfile in xmlfiles:
829 for line in file(xmlfile):
830 matches = string_search(line)
832 return retrieve_string(app_dir, matches.group(1), xmlfiles)
835 return string.replace("\\'", "'")
838 # Return list of existing files that will be used to find the highest vercode
839 def manifest_paths(app_dir, flavours):
841 possible_manifests = \
842 [os.path.join(app_dir, 'AndroidManifest.xml'),
843 os.path.join(app_dir, 'src', 'main', 'AndroidManifest.xml'),
844 os.path.join(app_dir, 'src', 'AndroidManifest.xml'),
845 os.path.join(app_dir, 'build.gradle')]
847 for flavour in flavours:
850 possible_manifests.append(
851 os.path.join(app_dir, 'src', flavour, 'AndroidManifest.xml'))
853 return [path for path in possible_manifests if os.path.isfile(path)]
856 # Retrieve the package name. Returns the name, or None if not found.
857 def fetch_real_name(app_dir, flavours):
858 app_search = re.compile(r'.*<application.*').search
859 name_search = re.compile(r'.*android:label="([^"]+)".*').search
861 for f in manifest_paths(app_dir, flavours):
862 if not has_extension(f, 'xml'):
864 logging.debug("fetch_real_name: Checking manifest at " + f)
870 matches = name_search(line)
872 stringname = matches.group(1)
873 logging.debug("fetch_real_name: using string " + stringname)
874 result = retrieve_string(app_dir, stringname)
876 result = result.strip()
881 # Retrieve the version name
882 def version_name(original, app_dir, flavours):
883 for f in manifest_paths(app_dir, flavours):
884 if not has_extension(f, 'xml'):
886 string = retrieve_string(app_dir, original)
892 def get_library_references(root_dir):
894 proppath = os.path.join(root_dir, 'project.properties')
895 if not os.path.isfile(proppath):
897 with open(proppath) as f:
898 for line in f.readlines():
899 if not line.startswith('android.library.reference.'):
901 path = line.split('=')[1].strip()
902 relpath = os.path.join(root_dir, path)
903 if not os.path.isdir(relpath):
905 logging.debug("Found subproject at %s" % path)
906 libraries.append(path)
910 def ant_subprojects(root_dir):
911 subprojects = get_library_references(root_dir)
912 for subpath in subprojects:
913 subrelpath = os.path.join(root_dir, subpath)
914 for p in get_library_references(subrelpath):
915 relp = os.path.normpath(os.path.join(subpath, p))
916 if relp not in subprojects:
917 subprojects.insert(0, relp)
921 def remove_debuggable_flags(root_dir):
922 # Remove forced debuggable flags
923 logging.debug("Removing debuggable flags from %s" % root_dir)
924 for root, dirs, files in os.walk(root_dir):
925 if 'AndroidManifest.xml' in files:
926 path = os.path.join(root, 'AndroidManifest.xml')
927 p = SilentPopen(['sed', '-i', 's/android:debuggable="[^"]*"//g', path])
928 if p.returncode != 0:
929 raise BuildException("Failed to remove debuggable flags of %s" % path)
932 # Extract some information from the AndroidManifest.xml at the given path.
933 # Returns (version, vercode, package), any or all of which might be None.
934 # All values returned are strings.
935 def parse_androidmanifests(paths, ignoreversions=None):
938 return (None, None, None)
940 vcsearch = re.compile(r'.*:versionCode="([0-9]+?)".*').search
941 vnsearch = re.compile(r'.*:versionName="([^"]+?)".*').search
942 psearch = re.compile(r'.*package="([^"]+)".*').search
944 vcsearch_g = re.compile(r'.*versionCode *=* *["\']*([0-9]+)["\']*').search
945 vnsearch_g = re.compile(r'.*versionName *=* *(["\'])((?:(?=(\\?))\3.)*?)\1.*').search
946 psearch_g = re.compile(r'.*packageName *=* *["\']([^"]+)["\'].*').search
948 ignoresearch = re.compile(ignoreversions).search if ignoreversions else None
956 gradle = has_extension(path, 'gradle')
959 # Remember package name, may be defined separately from version+vercode
960 package = max_package
962 for line in file(path):
965 matches = psearch_g(line)
967 matches = psearch(line)
969 package = matches.group(1)
972 matches = vnsearch_g(line)
974 matches = vnsearch(line)
976 version = matches.group(2 if gradle else 1)
979 matches = vcsearch_g(line)
981 matches = vcsearch(line)
983 vercode = matches.group(1)
985 # Always grab the package name and version name in case they are not
986 # together with the highest version code
987 if max_package is None and package is not None:
988 max_package = package
989 if max_version is None and version is not None:
990 max_version = version
992 if max_vercode is None or (vercode is not None and vercode > max_vercode):
993 if not ignoresearch or not ignoresearch(version):
994 if version is not None:
995 max_version = version
996 if vercode is not None:
997 max_vercode = vercode
998 if package is not None:
999 max_package = package
1001 max_version = "Ignore"
1003 if max_version is None:
1004 max_version = "Unknown"
1006 return (max_version, max_vercode, max_package)
1009 class FDroidException(Exception):
1010 def __init__(self, value, detail=None):
1012 self.detail = detail
1014 def get_wikitext(self):
1015 ret = repr(self.value) + "\n"
1019 txt = self.detail[-8192:] if len(self.detail) > 8192 else self.detail
1027 ret += "\n==== detail begin ====\n%s\n==== detail end ====" % self.detail.strip()
1031 class VCSException(FDroidException):
1035 class BuildException(FDroidException):
1039 # Get the specified source library.
1040 # Returns the path to it. Normally this is the path to be used when referencing
1041 # it, which may be a subdirectory of the actual project. If you want the base
1042 # directory of the project, pass 'basepath=True'.
1043 def getsrclib(spec, srclib_dir, srclibpaths=[], subdir=None,
1044 basepath=False, raw=False, prepare=True, preponly=False):
1052 name, ref = spec.split('@')
1054 number, name = name.split(':', 1)
1056 name, subdir = name.split('/', 1)
1058 if name not in metadata.srclibs:
1059 raise VCSException('srclib ' + name + ' not found.')
1061 srclib = metadata.srclibs[name]
1063 sdir = os.path.join(srclib_dir, name)
1066 vcs = getvcs(srclib["Repo Type"], srclib["Repo"], sdir)
1067 vcs.srclib = (name, number, sdir)
1069 vcs.gotorevision(ref)
1076 libdir = os.path.join(sdir, subdir)
1077 elif srclib["Subdir"]:
1078 for subdir in srclib["Subdir"]:
1079 libdir_candidate = os.path.join(sdir, subdir)
1080 if os.path.exists(libdir_candidate):
1081 libdir = libdir_candidate
1087 if srclib["Srclibs"]:
1089 for lib in srclib["Srclibs"].replace(';', ',').split(','):
1091 for t in srclibpaths:
1096 raise VCSException('Missing recursive srclib %s for %s' % (
1098 place_srclib(libdir, n, s_tuple[2])
1101 remove_signing_keys(sdir)
1102 remove_debuggable_flags(sdir)
1106 if srclib["Prepare"]:
1107 cmd = replace_config_vars(srclib["Prepare"])
1109 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=libdir)
1110 if p.returncode != 0:
1111 raise BuildException("Error running prepare command for srclib %s"
1117 return (name, number, libdir)
1120 # Prepare the source code for a particular build
1121 # 'vcs' - the appropriate vcs object for the application
1122 # 'app' - the application details from the metadata
1123 # 'build' - the build details from the metadata
1124 # 'build_dir' - the path to the build directory, usually
1126 # 'srclib_dir' - the path to the source libraries directory, usually
1128 # 'extlib_dir' - the path to the external libraries directory, usually
1130 # Returns the (root, srclibpaths) where:
1131 # 'root' is the root directory, which may be the same as 'build_dir' or may
1132 # be a subdirectory of it.
1133 # 'srclibpaths' is information on the srclibs being used
1134 def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=False):
1136 # Optionally, the actual app source can be in a subdirectory
1138 root_dir = os.path.join(build_dir, build['subdir'])
1140 root_dir = build_dir
1142 # Get a working copy of the right revision
1143 logging.info("Getting source for revision " + build['commit'])
1144 vcs.gotorevision(build['commit'])
1146 # Initialise submodules if requred
1147 if build['submodules']:
1148 logging.info("Initialising submodules")
1149 vcs.initsubmodules()
1151 # Check that a subdir (if we're using one) exists. This has to happen
1152 # after the checkout, since it might not exist elsewhere
1153 if not os.path.exists(root_dir):
1154 raise BuildException('Missing subdir ' + root_dir)
1156 # Run an init command if one is required
1158 cmd = replace_config_vars(build['init'])
1159 logging.info("Running 'init' commands in %s" % root_dir)
1161 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
1162 if p.returncode != 0:
1163 raise BuildException("Error running init command for %s:%s" %
1164 (app['id'], build['version']), p.output)
1166 # Apply patches if any
1168 logging.info("Applying patches")
1169 for patch in build['patch']:
1170 patch = patch.strip()
1171 logging.info("Applying " + patch)
1172 patch_path = os.path.join('metadata', app['id'], patch)
1173 p = FDroidPopen(['patch', '-p1', '-i', os.path.abspath(patch_path)], cwd=build_dir)
1174 if p.returncode != 0:
1175 raise BuildException("Failed to apply patch %s" % patch_path)
1177 # Get required source libraries
1179 if build['srclibs']:
1180 logging.info("Collecting source libraries")
1181 for lib in build['srclibs']:
1182 srclibpaths.append(getsrclib(lib, srclib_dir, srclibpaths,
1185 for name, number, libpath in srclibpaths:
1186 place_srclib(root_dir, int(number) if number else None, libpath)
1188 basesrclib = vcs.getsrclib()
1189 # If one was used for the main source, add that too.
1191 srclibpaths.append(basesrclib)
1193 # Update the local.properties file
1194 localprops = [os.path.join(build_dir, 'local.properties')]
1196 localprops += [os.path.join(root_dir, 'local.properties')]
1197 for path in localprops:
1199 if os.path.isfile(path):
1200 logging.info("Updating local.properties file at %s" % path)
1206 logging.info("Creating local.properties file at %s" % path)
1207 # Fix old-fashioned 'sdk-location' by copying
1208 # from sdk.dir, if necessary
1209 if build['oldsdkloc']:
1210 sdkloc = re.match(r".*^sdk.dir=(\S+)$.*", props,
1211 re.S | re.M).group(1)
1212 props += "sdk-location=%s\n" % sdkloc
1214 props += "sdk.dir=%s\n" % config['sdk_path']
1215 props += "sdk-location=%s\n" % config['sdk_path']
1216 if config['ndk_path']:
1218 props += "ndk.dir=%s\n" % config['ndk_path']
1219 props += "ndk-location=%s\n" % config['ndk_path']
1220 # Add java.encoding if necessary
1221 if build['encoding']:
1222 props += "java.encoding=%s\n" % build['encoding']
1228 if build['type'] == 'gradle':
1229 flavours = build['gradle']
1231 version_regex = re.compile(r".*'com\.android\.tools\.build:gradle:([^\.]+\.[^\.]+).*'.*")
1232 gradlepluginver = None
1234 gradle_files = [os.path.join(root_dir, 'build.gradle')]
1236 # Parent dir build.gradle
1237 parent_dir = os.path.normpath(os.path.join(root_dir, '..'))
1238 if parent_dir.startswith(build_dir):
1239 gradle_files.append(os.path.join(parent_dir, 'build.gradle'))
1241 for path in gradle_files:
1244 if not os.path.isfile(path):
1246 with open(path) as f:
1248 match = version_regex.match(line)
1250 gradlepluginver = match.group(1)
1254 build['gradlepluginver'] = LooseVersion(gradlepluginver)
1256 logging.warn("Could not fetch the gradle plugin version, defaulting to 0.11")
1257 build['gradlepluginver'] = LooseVersion('0.11')
1260 n = build["target"].split('-')[1]
1261 SilentPopen(['sed', '-i',
1262 's@compileSdkVersion *[0-9]*@compileSdkVersion ' + n + '@g',
1266 # Remove forced debuggable flags
1267 remove_debuggable_flags(root_dir)
1269 # Insert version code and number into the manifest if necessary
1270 if build['forceversion']:
1271 logging.info("Changing the version name")
1272 for path in manifest_paths(root_dir, flavours):
1273 if not os.path.isfile(path):
1275 if has_extension(path, 'xml'):
1276 p = SilentPopen(['sed', '-i',
1277 's/android:versionName="[^"]*"/android:versionName="'
1278 + build['version'] + '"/g',
1280 if p.returncode != 0:
1281 raise BuildException("Failed to amend manifest")
1282 elif has_extension(path, 'gradle'):
1283 p = SilentPopen(['sed', '-i',
1284 's/versionName *=* *"[^"]*"/versionName = "'
1285 + build['version'] + '"/g',
1287 if p.returncode != 0:
1288 raise BuildException("Failed to amend build.gradle")
1289 if build['forcevercode']:
1290 logging.info("Changing the version code")
1291 for path in manifest_paths(root_dir, flavours):
1292 if not os.path.isfile(path):
1294 if has_extension(path, 'xml'):
1295 p = SilentPopen(['sed', '-i',
1296 's/android:versionCode="[^"]*"/android:versionCode="'
1297 + build['vercode'] + '"/g',
1299 if p.returncode != 0:
1300 raise BuildException("Failed to amend manifest")
1301 elif has_extension(path, 'gradle'):
1302 p = SilentPopen(['sed', '-i',
1303 's/versionCode *=* *[0-9]*/versionCode = '
1304 + build['vercode'] + '/g',
1306 if p.returncode != 0:
1307 raise BuildException("Failed to amend build.gradle")
1309 # Delete unwanted files
1311 logging.info("Removing specified files")
1312 for part in getpaths(build_dir, build, 'rm'):
1313 dest = os.path.join(build_dir, part)
1314 logging.info("Removing {0}".format(part))
1315 if os.path.lexists(dest):
1316 if os.path.islink(dest):
1317 SilentPopen(['unlink ' + dest], shell=True)
1319 SilentPopen(['rm -rf ' + dest], shell=True)
1321 logging.info("...but it didn't exist")
1323 remove_signing_keys(build_dir)
1325 # Add required external libraries
1326 if build['extlibs']:
1327 logging.info("Collecting prebuilt libraries")
1328 libsdir = os.path.join(root_dir, 'libs')
1329 if not os.path.exists(libsdir):
1331 for lib in build['extlibs']:
1333 logging.info("...installing extlib {0}".format(lib))
1334 libf = os.path.basename(lib)
1335 libsrc = os.path.join(extlib_dir, lib)
1336 if not os.path.exists(libsrc):
1337 raise BuildException("Missing extlib file {0}".format(libsrc))
1338 shutil.copyfile(libsrc, os.path.join(libsdir, libf))
1340 # Run a pre-build command if one is required
1341 if build['prebuild']:
1342 logging.info("Running 'prebuild' commands in %s" % root_dir)
1344 cmd = replace_config_vars(build['prebuild'])
1346 # Substitute source library paths into prebuild commands
1347 for name, number, libpath in srclibpaths:
1348 libpath = os.path.relpath(libpath, root_dir)
1349 cmd = cmd.replace('$$' + name + '$$', libpath)
1351 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
1352 if p.returncode != 0:
1353 raise BuildException("Error running prebuild command for %s:%s" %
1354 (app['id'], build['version']), p.output)
1356 # Generate (or update) the ant build file, build.xml...
1357 if build['update'] and build['update'] != ['no'] and build['type'] == 'ant':
1358 parms = ['android', 'update', 'lib-project']
1359 lparms = ['android', 'update', 'project']
1362 parms += ['-t', build['target']]
1363 lparms += ['-t', build['target']]
1364 if build['update'] == ['auto']:
1365 update_dirs = ant_subprojects(root_dir) + ['.']
1367 update_dirs = build['update']
1369 for d in update_dirs:
1370 subdir = os.path.join(root_dir, d)
1372 logging.debug("Updating main project")
1373 cmd = parms + ['-p', d]
1375 logging.debug("Updating subproject %s" % d)
1376 cmd = lparms + ['-p', d]
1377 p = SdkToolsPopen(cmd, cwd=root_dir)
1378 # Check to see whether an error was returned without a proper exit
1379 # code (this is the case for the 'no target set or target invalid'
1381 if p.returncode != 0 or p.output.startswith("Error: "):
1382 raise BuildException("Failed to update project at %s" % d, p.output)
1383 # Clean update dirs via ant
1385 logging.info("Cleaning subproject %s" % d)
1386 p = FDroidPopen(['ant', 'clean'], cwd=subdir)
1388 return (root_dir, srclibpaths)
1391 # Split and extend via globbing the paths from a field
1392 def getpaths(build_dir, build, field):
1394 for p in build[field]:
1396 full_path = os.path.join(build_dir, p)
1397 full_path = os.path.normpath(full_path)
1398 paths += [r[len(build_dir) + 1:] for r in glob.glob(full_path)]
1402 # Scan the source code in the given directory (and all subdirectories)
1403 # and return the number of fatal problems encountered
1404 def scan_source(build_dir, root_dir, thisbuild):
1408 # Common known non-free blobs (always lower case):
1410 re.compile(r'flurryagent', re.IGNORECASE),
1411 re.compile(r'paypal.*mpl', re.IGNORECASE),
1412 re.compile(r'google.*analytics', re.IGNORECASE),
1413 re.compile(r'admob.*sdk.*android', re.IGNORECASE),
1414 re.compile(r'google.*ad.*view', re.IGNORECASE),
1415 re.compile(r'google.*admob', re.IGNORECASE),
1416 re.compile(r'google.*play.*services', re.IGNORECASE),
1417 re.compile(r'crittercism', re.IGNORECASE),
1418 re.compile(r'heyzap', re.IGNORECASE),
1419 re.compile(r'jpct.*ae', re.IGNORECASE),
1420 re.compile(r'youtube.*android.*player.*api', re.IGNORECASE),
1421 re.compile(r'bugsense', re.IGNORECASE),
1422 re.compile(r'crashlytics', re.IGNORECASE),
1423 re.compile(r'ouya.*sdk', re.IGNORECASE),
1424 re.compile(r'libspen23', re.IGNORECASE),
1427 scanignore = getpaths(build_dir, thisbuild, 'scanignore')
1428 scandelete = getpaths(build_dir, thisbuild, 'scandelete')
1431 ms = magic.open(magic.MIME_TYPE)
1433 except AttributeError:
1437 for i in scanignore:
1438 if fd.startswith(i):
1443 for i in scandelete:
1444 if fd.startswith(i):
1448 def removeproblem(what, fd, fp):
1449 logging.info('Removing %s at %s' % (what, fd))
1452 def warnproblem(what, fd):
1453 logging.warn('Found %s at %s' % (what, fd))
1455 def handleproblem(what, fd, fp):
1457 logging.info('Ignoring %s at %s' % (what, fd))
1459 removeproblem(what, fd, fp)
1461 logging.error('Found %s at %s' % (what, fd))
1465 # Iterate through all files in the source code
1466 for r, d, f in os.walk(build_dir, topdown=True):
1468 # It's topdown, so checking the basename is enough
1469 for ignoredir in ('.hg', '.git', '.svn', '.bzr'):
1475 # Path (relative) to the file
1476 fp = os.path.join(r, curfile)
1477 fd = fp[len(build_dir) + 1:]
1480 mime = magic.from_file(fp, mime=True) if ms is None else ms.file(fp)
1481 except UnicodeError:
1482 warnproblem('malformed magic number', fd)
1484 if mime == 'application/x-sharedlib':
1485 count += handleproblem('shared library', fd, fp)
1487 elif mime == 'application/x-archive':
1488 count += handleproblem('static library', fd, fp)
1490 elif mime == 'application/x-executable':
1491 count += handleproblem('binary executable', fd, fp)
1493 elif mime == 'application/x-java-applet':
1494 count += handleproblem('Java compiled class', fd, fp)
1499 'application/java-archive',
1500 'application/octet-stream',
1504 if has_extension(fp, 'apk'):
1505 removeproblem('APK file', fd, fp)
1507 elif has_extension(fp, 'jar'):
1509 if any(suspect.match(curfile) for suspect in usual_suspects):
1510 count += handleproblem('usual supect', fd, fp)
1512 warnproblem('JAR file', fd)
1514 elif has_extension(fp, 'zip'):
1515 warnproblem('ZIP file', fd)
1518 warnproblem('unknown compressed or binary file', fd)
1520 elif has_extension(fp, 'java'):
1521 for line in file(fp):
1522 if 'DexClassLoader' in line:
1523 count += handleproblem('DexClassLoader', fd, fp)
1528 # Presence of a jni directory without buildjni=yes might
1529 # indicate a problem (if it's not a problem, explicitly use
1530 # buildjni=no to bypass this check)
1531 if (os.path.exists(os.path.join(root_dir, 'jni')) and
1532 not thisbuild['buildjni']):
1533 logging.error('Found jni directory, but buildjni is not enabled. Set it to \'no\' to ignore.')
1542 self.path = os.path.join('stats', 'known_apks.txt')
1544 if os.path.exists(self.path):
1545 for line in file(self.path):
1546 t = line.rstrip().split(' ')
1548 self.apks[t[0]] = (t[1], None)
1550 self.apks[t[0]] = (t[1], time.strptime(t[2], '%Y-%m-%d'))
1551 self.changed = False
1553 def writeifchanged(self):
1555 if not os.path.exists('stats'):
1557 f = open(self.path, 'w')
1559 for apk, app in self.apks.iteritems():
1561 line = apk + ' ' + appid
1563 line += ' ' + time.strftime('%Y-%m-%d', added)
1565 for line in sorted(lst):
1566 f.write(line + '\n')
1569 # Record an apk (if it's new, otherwise does nothing)
1570 # Returns the date it was added.
1571 def recordapk(self, apk, app):
1572 if apk not in self.apks:
1573 self.apks[apk] = (app, time.gmtime(time.time()))
1575 _, added = self.apks[apk]
1578 # Look up information - given the 'apkname', returns (app id, date added/None).
1579 # Or returns None for an unknown apk.
1580 def getapp(self, apkname):
1581 if apkname in self.apks:
1582 return self.apks[apkname]
1585 # Get the most recent 'num' apps added to the repo, as a list of package ids
1586 # with the most recent first.
1587 def getlatest(self, num):
1589 for apk, app in self.apks.iteritems():
1593 if apps[appid] > added:
1597 sortedapps = sorted(apps.iteritems(), key=operator.itemgetter(1))[-num:]
1598 lst = [app for app, _ in sortedapps]
1603 def isApkDebuggable(apkfile, config):
1604 """Returns True if the given apk file is debuggable
1606 :param apkfile: full path to the apk to check"""
1608 p = SdkToolsPopen(['aapt', 'dump', 'xmltree', apkfile, 'AndroidManifest.xml'])
1609 if p.returncode != 0:
1610 logging.critical("Failed to get apk manifest information")
1612 for line in p.output.splitlines():
1613 if 'android:debuggable' in line and not line.endswith('0x0'):
1618 class AsynchronousFileReader(threading.Thread):
1620 Helper class to implement asynchronous reading of a file
1621 in a separate thread. Pushes read lines on a queue to
1622 be consumed in another thread.
1625 def __init__(self, fd, queue):
1626 assert isinstance(queue, Queue.Queue)
1627 assert callable(fd.readline)
1628 threading.Thread.__init__(self)
1633 '''The body of the tread: read lines and put them on the queue.'''
1634 for line in iter(self._fd.readline, ''):
1635 self._queue.put(line)
1638 '''Check whether there is no more content to expect.'''
1639 return not self.is_alive() and self._queue.empty()
1647 def SdkToolsPopen(commands, cwd=None, shell=False):
1649 if cmd not in config:
1650 config[cmd] = find_sdk_tools_cmd(commands[0])
1651 return FDroidPopen([config[cmd]] + commands[1:],
1652 cwd=cwd, shell=shell, output=False)
1655 def SilentPopen(commands, cwd=None, shell=False):
1656 return FDroidPopen(commands, cwd=cwd, shell=shell, output=False)
1659 def FDroidPopen(commands, cwd=None, shell=False, output=True):
1661 Run a command and capture the possibly huge output.
1663 :param commands: command and argument list like in subprocess.Popen
1664 :param cwd: optionally specifies a working directory
1665 :returns: A PopenResult.
1671 cwd = os.path.normpath(cwd)
1672 logging.debug("Directory: %s" % cwd)
1673 logging.debug("> %s" % ' '.join(commands))
1675 result = PopenResult()
1678 p = subprocess.Popen(commands, cwd=cwd, shell=shell, env=env,
1679 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
1681 raise BuildException("OSError while trying to execute " + ' '.join(commands) + ': ' + str(e))
1683 stdout_queue = Queue.Queue()
1684 stdout_reader = AsynchronousFileReader(p.stdout, stdout_queue)
1685 stdout_reader.start()
1687 # Check the queue for output (until there is no more to get)
1688 while not stdout_reader.eof():
1689 while not stdout_queue.empty():
1690 line = stdout_queue.get()
1691 if output and options.verbose:
1692 # Output directly to console
1693 sys.stderr.write(line)
1695 result.output += line
1699 result.returncode = p.wait()
1703 def remove_signing_keys(build_dir):
1704 comment = re.compile(r'[ ]*//')
1705 signing_configs = re.compile(r'^[\t ]*signingConfigs[ \t]*{[ \t]*$')
1707 re.compile(r'^[\t ]*signingConfig [^ ]*$'),
1708 re.compile(r'.*android\.signingConfigs\.[^{]*$'),
1709 re.compile(r'.*variant\.outputFile = .*'),
1710 re.compile(r'.*\.readLine\(.*'),
1712 for root, dirs, files in os.walk(build_dir):
1713 if 'build.gradle' in files:
1714 path = os.path.join(root, 'build.gradle')
1716 with open(path, "r") as o:
1717 lines = o.readlines()
1722 with open(path, "w") as o:
1724 if comment.match(line):
1728 opened += line.count('{')
1729 opened -= line.count('}')
1732 if signing_configs.match(line):
1737 if any(s.match(line) for s in line_matches):
1745 logging.info("Cleaned build.gradle of keysigning configs at %s" % path)
1748 'project.properties',
1750 'default.properties',
1753 if propfile in files:
1754 path = os.path.join(root, propfile)
1756 with open(path, "r") as o:
1757 lines = o.readlines()
1761 with open(path, "w") as o:
1763 if any(line.startswith(s) for s in ('key.store', 'key.alias')):
1770 logging.info("Cleaned %s of keysigning configs at %s" % (propfile, path))
1773 def replace_config_vars(cmd):
1774 cmd = cmd.replace('$$SDK$$', config['sdk_path'])
1775 cmd = cmd.replace('$$NDK$$', config['ndk_path'])
1776 cmd = cmd.replace('$$MVN3$$', config['mvn3'])
1780 def place_srclib(root_dir, number, libpath):
1783 relpath = os.path.relpath(libpath, root_dir)
1784 proppath = os.path.join(root_dir, 'project.properties')
1787 if os.path.isfile(proppath):
1788 with open(proppath, "r") as o:
1789 lines = o.readlines()
1791 with open(proppath, "w") as o:
1794 if line.startswith('android.library.reference.%d=' % number):
1795 o.write('android.library.reference.%d=%s\n' % (number, relpath))
1800 o.write('android.library.reference.%d=%s\n' % (number, relpath))
1803 def compare_apks(apk1, apk2, tmp_dir):
1806 Returns None if the apk content is the same (apart from the signing key),
1807 otherwise a string describing what's different, or what went wrong when
1808 trying to do the comparison.
1811 thisdir = os.path.join(tmp_dir, 'this_apk')
1812 thatdir = os.path.join(tmp_dir, 'that_apk')
1813 for d in [thisdir, thatdir]:
1814 if os.path.exists(d):
1818 if subprocess.call(['jar', 'xf',
1819 os.path.abspath(apk1)],
1821 return("Failed to unpack " + apk1)
1822 if subprocess.call(['jar', 'xf',
1823 os.path.abspath(apk2)],
1825 return("Failed to unpack " + apk2)
1827 p = FDroidPopen(['diff', '-r', 'this_apk', 'that_apk'], cwd=tmp_dir,
1829 lines = p.output.splitlines()
1830 if len(lines) != 1 or 'META-INF' not in lines[0]:
1831 return("Unexpected diff output - " + p.output)
1833 # If we get here, it seems like they're the same!