chiark / gitweb /
Merge branch 'bug-fixes-for-v0.2.1' of https://gitlab.com/eighthave/fdroidserver
[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 optparse import OptionParser, OptionError
25 import logging
26
27 import common
28 from common import FDroidPopen
29
30 options = None
31 config = None
32
33
34 def devices():
35     p = FDroidPopen(["adb", "devices"])
36     if p.returncode != 0:
37         raise Exception("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 = OptionParser(usage="Usage: %prog [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
53     parser.add_option("-v", "--verbose", action="store_true", default=False,
54                       help="Spew out even more information than normal")
55     parser.add_option("-q", "--quiet", action="store_true", default=False,
56                       help="Restrict output to warnings and errors")
57     parser.add_option("-a", "--all", action="store_true", default=False,
58                       help="Install all signed applications available")
59     (options, args) = parser.parse_args()
60
61     if not args and not options.all:
62         raise OptionError("If you really want to install all the signed apps, use --all", "all")
63
64     config = common.read_config(options)
65
66     output_dir = 'repo'
67     if not os.path.isdir(output_dir):
68         logging.info("No signed output directory - nothing to do")
69         sys.exit(0)
70
71     if args:
72
73         vercodes = common.read_pkg_args(args, True)
74         apks = {appid: None for appid in vercodes}
75
76         # Get the signed apk with the highest vercode
77         for apkfile in sorted(glob.glob(os.path.join(output_dir, '*.apk'))):
78
79             appid, vercode = common.apknameinfo(apkfile)
80             if appid not in apks:
81                 continue
82             if vercodes[appid] and vercode not in vercodes[appid]:
83                 continue
84             apks[appid] = apkfile
85
86         for appid, apk in apks.iteritems():
87             if not apk:
88                 raise Exception("No signed apk available for %s" % appid)
89
90     else:
91
92         apks = {common.apknameinfo(apkfile)[0]: apkfile for apkfile in
93                 sorted(glob.glob(os.path.join(output_dir, '*.apk')))}
94
95     for appid, apk in apks.iteritems():
96         # Get device list each time to avoid device not found errors
97         devs = devices()
98         if not devs:
99             raise Exception("No attached devices found")
100         logging.info("Installing %s..." % apk)
101         for dev in devs:
102             logging.info("Installing %s on %s..." % (apk, dev))
103             p = FDroidPopen(["adb", "-s", dev, "install", apk])
104             fail = ""
105             for line in p.output.splitlines():
106                 if line.startswith("Failure"):
107                     fail = line[9:-1]
108             if not fail:
109                 continue
110
111             if fail == "INSTALL_FAILED_ALREADY_EXISTS":
112                 logging.warn("%s is already installed on %s." % (apk, dev))
113             else:
114                 raise Exception("Failed to install %s on %s: %s" % (
115                     apk, dev, fail))
116
117     logging.info("\nFinished")
118
119 if __name__ == "__main__":
120     main()