chiark / gitweb /
af4a71bee4ef279cf9722c2385cee5e48f7c5634
[fdroidserver.git] / fdroidserver / install.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # install.py - part of the FDroid server tools
5 # Copyright (C) 2013, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Affero General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 import sys
22 import os
23 import glob
24 from argparse import ArgumentParser
25 import logging
26
27 import common
28 from common import SdkToolsPopen, FDroidException
29
30 options = None
31 config = None
32
33
34 def devices():
35     p = SdkToolsPopen(['adb', "devices"])
36     if p.returncode != 0:
37         raise FDroidException("An error occured when finding devices: %s" % p.output)
38     lines = [l for l in p.output.splitlines() if not l.startswith('* ')]
39     if len(lines) < 3:
40         return []
41     lines = lines[1:-1]
42     return [l.split()[0] for l in lines]
43
44
45 def main():
46
47     global options, config
48
49     # Parse command line...
50     parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
51     common.setup_global_opts(parser)
52     parser.add_argument("appid", nargs='*', help="app-id with optional versioncode in the form APPID[:VERCODE]")
53     parser.add_argument("-a", "--all", action="store_true", default=False,
54                         help="Install all signed applications available")
55     options = parser.parse_args()
56
57     if not options.appid and not options.all:
58         parser.error("option %s: If you really want to install all the signed apps, use --all" % "all")
59
60     config = common.read_config(options)
61
62     output_dir = 'repo'
63     if not os.path.isdir(output_dir):
64         logging.info("No signed output directory - nothing to do")
65         sys.exit(0)
66
67     if options.appid:
68
69         vercodes = common.read_pkg_args(options.appid, True)
70         apks = {appid: None for appid in vercodes}
71
72         # Get the signed apk with the highest vercode
73         for apkfile in sorted(glob.glob(os.path.join(output_dir, '*.apk'))):
74
75             try:
76                 appid, vercode = common.apknameinfo(apkfile)
77             except FDroidException:
78                 continue
79             if appid not in apks:
80                 continue
81             if vercodes[appid] and vercode not in vercodes[appid]:
82                 continue
83             apks[appid] = apkfile
84
85         for appid, apk in apks.iteritems():
86             if not apk:
87                 raise FDroidException("No signed apk available for %s" % appid)
88
89     else:
90
91         apks = {common.apknameinfo(apkfile)[0]: apkfile for apkfile in
92                 sorted(glob.glob(os.path.join(output_dir, '*.apk')))}
93
94     for appid, apk in apks.iteritems():
95         # Get device list each time to avoid device not found errors
96         devs = devices()
97         if not devs:
98             raise FDroidException("No attached devices found")
99         logging.info("Installing %s..." % apk)
100         for dev in devs:
101             logging.info("Installing %s on %s..." % (apk, dev))
102             p = SdkToolsPopen(['adb', "-s", dev, "install", apk])
103             fail = ""
104             for line in p.output.splitlines():
105                 if line.startswith("Failure"):
106                     fail = line[9:-1]
107             if not fail:
108                 continue
109
110             if fail == "INSTALL_FAILED_ALREADY_EXISTS":
111                 logging.warn("%s is already installed on %s." % (apk, dev))
112             else:
113                 raise FDroidException("Failed to install %s on %s: %s" % (
114                     apk, dev, fail))
115
116     logging.info("\nFinished")
117
118 if __name__ == "__main__":
119     main()