chiark / gitweb /
new build-tools_r24.0.1.zip sha256, Google keeps changing it
[fdroidserver.git] / fdroidserver / install.py
1 #!/usr/bin/env python3
2 #
3 # install.py - part of the FDroid server tools
4 # Copyright (C) 2013, Ciaran Gultnieks, ciaran@ciarang.com
5 # Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
6 #
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.
11 #
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.
16 #
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/>.
19
20 import sys
21 import os
22 import glob
23 from argparse import ArgumentParser
24 import logging
25
26 from . import common
27 from .common import SdkToolsPopen, FDroidException
28
29 options = None
30 config = None
31
32
33 def devices():
34     p = SdkToolsPopen(['adb', "devices"])
35     if p.returncode != 0:
36         raise FDroidException("An error occured when finding devices: %s" % p.output)
37     lines = [l for l in p.output.splitlines() if not l.startswith('* ')]
38     if len(lines) < 3:
39         return []
40     lines = lines[1:-1]
41     return [l.split()[0] for l in lines]
42
43
44 def main():
45
46     global options, config
47
48     # Parse command line...
49     parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
50     common.setup_global_opts(parser)
51     parser.add_argument("appid", nargs='*', help="app-id with optional versioncode in the form APPID[:VERCODE]")
52     parser.add_argument("-a", "--all", action="store_true", default=False,
53                         help="Install all signed applications available")
54     options = parser.parse_args()
55
56     if not options.appid and not options.all:
57         parser.error("option %s: If you really want to install all the signed apps, use --all" % "all")
58
59     config = common.read_config(options)
60
61     output_dir = 'repo'
62     if not os.path.isdir(output_dir):
63         logging.info("No signed output directory - nothing to do")
64         sys.exit(0)
65
66     if options.appid:
67
68         vercodes = common.read_pkg_args(options.appid, True)
69         apks = {appid: None for appid in vercodes}
70
71         # Get the signed apk with the highest vercode
72         for apkfile in sorted(glob.glob(os.path.join(output_dir, '*.apk'))):
73
74             try:
75                 appid, vercode = common.apknameinfo(apkfile)
76             except FDroidException:
77                 continue
78             if appid not in apks:
79                 continue
80             if vercodes[appid] and vercode not in vercodes[appid]:
81                 continue
82             apks[appid] = apkfile
83
84         for appid, apk in apks.items():
85             if not apk:
86                 raise FDroidException("No signed apk available for %s" % appid)
87
88     else:
89
90         apks = {common.apknameinfo(apkfile)[0]: apkfile for apkfile in
91                 sorted(glob.glob(os.path.join(output_dir, '*.apk')))}
92
93     for appid, apk in apks.items():
94         # Get device list each time to avoid device not found errors
95         devs = devices()
96         if not devs:
97             raise FDroidException("No attached devices found")
98         logging.info("Installing %s..." % apk)
99         for dev in devs:
100             logging.info("Installing %s on %s..." % (apk, dev))
101             p = SdkToolsPopen(['adb', "-s", dev, "install", apk])
102             fail = ""
103             for line in p.output.splitlines():
104                 if line.startswith("Failure"):
105                     fail = line[9:-1]
106             if not fail:
107                 continue
108
109             if fail == "INSTALL_FAILED_ALREADY_EXISTS":
110                 logging.warn("%s is already installed on %s." % (apk, dev))
111             else:
112                 raise FDroidException("Failed to install %s on %s: %s" % (
113                     apk, dev, fail))
114
115     logging.info("\nFinished")
116
117 if __name__ == "__main__":
118     main()