chiark / gitweb /
Adapt verify
[fdroidserver.git] / fdroidserver / install.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # verify.py - part of the FDroid server tools
5 # Copyright (C) 2013, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013 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
25
26 import common
27 from common import FDroidPopen
28
29 options = None
30 config = None
31
32 def devices():
33     p = FDroidPopen(["adb", "devices"])
34     if p.returncode != 0:
35         raise Exception("An error occured when finding devices: %s" % p.stderr)
36     return [l.split()[0] for l in p.stdout.splitlines()[1:-1]]
37
38
39 def main():
40
41     global options, config
42
43     # Parse command line...
44     parser = OptionParser()
45     parser.add_option("-v", "--verbose", action="store_true", default=False,
46                       help="Spew out even more information than normal")
47     parser.add_option("-a", "--all", action="store_true", default=False,
48                       help="Install all signed applications available")
49     (options, args) = parser.parse_args()
50
51     config = common.read_config(options)
52
53     output_dir = 'repo'
54     if not os.path.isdir(output_dir):
55         print "No signed output directory - nothing to do"
56         sys.exit(1)
57
58     if args:
59
60         vercodes = common.read_pkg_args(args, True)
61         apks = { appid : None for appid in vercodes }
62
63         # Get the signed apk with the highest vercode
64         for apkfile in sorted(glob.glob(os.path.join(output_dir, '*.apk'))):
65
66             appid, vercode = common.apknameinfo(apkfile)
67             if appid not in apks:
68                 continue
69             if vercodes[appid] and vercode not in vercodes[appid]:
70                 continue
71             apks[appid] = apkfile
72
73         for appid, apk in apks.iteritems():
74             if not apk:
75                 raise Exception("No signed apk available for %s" % appid)
76
77     elif options.all:
78
79         for apkfile in sorted(glob.glob(os.path.join(output_dir, '*.apk'))):
80
81             appid, vercode = common.apknameinfo(apkfile)
82             apks[appid] = apkfile
83
84     else:
85         print "If you really want to install all the signed apps, use --all"
86         sys.exit(0)
87     
88     for appid, apk in apks.iteritems():
89         # Get device list each time to avoid device not found errors
90         devs = devices()
91         if not devs:
92             raise Exception("No attached devices found")
93         print "Installing %s..." % apk
94         for dev in devs:
95             print "Installing %s on %s..." % (apk, dev)
96             p = FDroidPopen(["adb", "-s", dev, "install", apk ])
97             fail= ""
98             for line in p.stdout.splitlines():
99                 if line.startswith("Failure"):
100                     fail = line[9:-1]
101             if fail:
102                 if fail == "INSTALL_FAILED_ALREADY_EXISTS":
103                     print "%s is already installed on %s." % (apk, dev)
104                 else:
105                     raise Exception("Failed to install %s on %s: %s" % (
106                         apk, dev, fail))
107
108     print "\nFinished"
109
110 if __name__ == "__main__":
111     main()
112