chiark / gitweb /
Merge branch 'replace_optparse_with_argparse' into 'master'
[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 = p.output.splitlines()
39     if lines[0].startswith('* daemon not running'):
40         lines = lines[2:]
41     if len(lines) < 3:
42         return []
43     lines = lines[1:-1]
44     return [l.split()[0] for l in lines]
45
46
47 def main():
48
49     global options, config
50
51     # Parse command line...
52     parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
53     parser.add_argument("appid", nargs='*', help="app-id with optional versioncode in the form APPID[:VERCODE]")
54     parser.add_argument("-v", "--verbose", action="store_true", default=False,
55                         help="Spew out even more information than normal")
56     parser.add_argument("-q", "--quiet", action="store_true", default=False,
57                         help="Restrict output to warnings and errors")
58     parser.add_argument("-a", "--all", action="store_true", default=False,
59                         help="Install all signed applications available")
60     options = parser.parse_args()
61
62     if not options.appid and not options.all:
63         parser.error("option %s: If you really want to install all the signed apps, use --all" % "all")
64
65     config = common.read_config(options)
66
67     output_dir = 'repo'
68     if not os.path.isdir(output_dir):
69         logging.info("No signed output directory - nothing to do")
70         sys.exit(0)
71
72     if options.appid:
73
74         vercodes = common.read_pkg_args(options.appid, True)
75         apks = {appid: None for appid in vercodes}
76
77         # Get the signed apk with the highest vercode
78         for apkfile in sorted(glob.glob(os.path.join(output_dir, '*.apk'))):
79
80             try:
81                 appid, vercode = common.apknameinfo(apkfile)
82             except FDroidException:
83                 continue
84             if appid not in apks:
85                 continue
86             if vercodes[appid] and vercode not in vercodes[appid]:
87                 continue
88             apks[appid] = apkfile
89
90         for appid, apk in apks.iteritems():
91             if not apk:
92                 raise FDroidException("No signed apk available for %s" % appid)
93
94     else:
95
96         apks = {common.apknameinfo(apkfile)[0]: apkfile for apkfile in
97                 sorted(glob.glob(os.path.join(output_dir, '*.apk')))}
98
99     for appid, apk in apks.iteritems():
100         # Get device list each time to avoid device not found errors
101         devs = devices()
102         if not devs:
103             raise FDroidException("No attached devices found")
104         logging.info("Installing %s..." % apk)
105         for dev in devs:
106             logging.info("Installing %s on %s..." % (apk, dev))
107             p = SdkToolsPopen(['adb', "-s", dev, "install", apk])
108             fail = ""
109             for line in p.output.splitlines():
110                 if line.startswith("Failure"):
111                     fail = line[9:-1]
112             if not fail:
113                 continue
114
115             if fail == "INSTALL_FAILED_ALREADY_EXISTS":
116                 logging.warn("%s is already installed on %s." % (apk, dev))
117             else:
118                 raise FDroidException("Failed to install %s on %s: %s" % (
119                     apk, dev, fail))
120
121     logging.info("\nFinished")
122
123 if __name__ == "__main__":
124     main()