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