chiark / gitweb /
rename 'app-id' to standard Android 'applicationId'
[fdroidserver.git] / fdroidserver / install.py
1 #!/usr/bin/env python3
2 #
3 # install.py - part of the FDroid server tools
4 # Copyright (C) 2013, Ciaran Gultnieks, ciaran@ciarang.com
5 # Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import sys
21 import os
22 import glob
23 from argparse import ArgumentParser
24 import logging
25
26 from . import _
27 from . import common
28 from .common import SdkToolsPopen
29 from .exception import FDroidException
30
31 options = None
32 config = None
33
34
35 def devices():
36     p = SdkToolsPopen(['adb', "devices"])
37     if p.returncode != 0:
38         raise FDroidException("An error occured when finding devices: %s" % p.output)
39     lines = [l for l in p.output.splitlines() if not l.startswith('* ')]
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 = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
52     common.setup_global_opts(parser)
53     parser.add_argument("appid", nargs='*', help=_("applicationId with optional versionCode in the form APPID[:VERCODE]"))
54     parser.add_argument("-a", "--all", action="store_true", default=False,
55                         help=_("Install all signed applications available"))
56     options = parser.parse_args()
57
58     if not options.appid and not options.all:
59         parser.error(_("option %s: 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 options.appid:
69
70         vercodes = common.read_pkg_args(options.appid, 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             try:
77                 appid, vercode = common.publishednameinfo(apkfile)
78             except FDroidException:
79                 continue
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.items():
87             if not apk:
88                 raise FDroidException(_("No signed apk available for %s") % appid)
89
90     else:
91
92         apks = {common.publishednameinfo(apkfile)[0]: apkfile for apkfile in
93                 sorted(glob.glob(os.path.join(output_dir, '*.apk')))}
94
95     for appid, apk in apks.items():
96         # Get device list each time to avoid device not found errors
97         devs = devices()
98         if not devs:
99             raise FDroidException(_("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 = SdkToolsPopen(['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 FDroidException(_("Failed to install %s on %s: %s") % (
115                     apk, dev, fail))
116
117     logging.info('\n' + _('Finished'))
118
119
120 if __name__ == "__main__":
121     main()