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