chiark / gitweb /
Switch all headers to python3
[fdroidserver.git] / fdroidserver / install.py
index 65f6c0464b478a5c723c369efb56a1284b968711..ebb66232a1b2a65d05c583a6bb0f01eb4e091029 100644 (file)
@@ -1,8 +1,8 @@
-#!/usr/bin/env python2
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
 #
-# verify.py - part of the FDroid server tools
+# install.py - part of the FDroid server tools
 # Copyright (C) 2013, Ciaran Gultnieks, ciaran@ciarang.com
+# Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 
 import sys
 import os
-from optparse import OptionParser
+import glob
+from argparse import ArgumentParser
+import logging
 
 import common
-from common import FDroidPopen
-import metadata
+from common import SdkToolsPopen, FDroidException
 
 options = None
 config = None
 
+
 def devices():
-    p = FDroidPopen(["adb", "devices"])
+    p = SdkToolsPopen(['adb', "devices"])
     if p.returncode != 0:
-        raise Exception("An error occured when finding devices: %s" % p.stderr)
-    devs = []
-    return [l.split()[0] for l in p.stdout.splitlines()[1:-1]]
+        raise FDroidException("An error occured when finding devices: %s" % p.output)
+    lines = [l for l in p.output.splitlines() if not l.startswith('* ')]
+    if len(lines) < 3:
+        return []
+    lines = lines[1:-1]
+    return [l.split()[0] for l in lines]
 
 
 def main():
@@ -41,56 +46,73 @@ def main():
     global options, config
 
     # Parse command line...
-    parser = OptionParser()
-    parser.add_option("-v", "--verbose", action="store_true", default=False,
-                      help="Spew out even more information than normal")
-    (options, args) = parser.parse_args()
+    parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
+    common.setup_global_opts(parser)
+    parser.add_argument("appid", nargs='*', help="app-id with optional versioncode in the form APPID[:VERCODE]")
+    parser.add_argument("-a", "--all", action="store_true", default=False,
+                        help="Install all signed applications available")
+    options = parser.parse_args()
+
+    if not options.appid and not options.all:
+        parser.error("option %s: If you really want to install all the signed apps, use --all" % "all")
 
     config = common.read_config(options)
 
     output_dir = 'repo'
     if not os.path.isdir(output_dir):
-        print "No signed output directory - nothing to do"
-        sys.exit(1)
-
-    # Get all apps...
-    allapps = metadata.read_metadata()
-
-    apps = common.read_app_args(args, options, allapps, True)
-
-    for app in apps:
-        last = None
-        for build in app['builds']:
-            apk = os.path.join(output_dir, common.getapkname(app, build))
-            if os.path.exists(apk):
-                last = build
-        if last is None:
-            raise Exception("No available signed apks for %s" % app['id'])
-
-    for app in apps:
-        build = app['builds'][0]
-        apk = os.path.join(output_dir, common.getapkname(app, build))
-        if not os.path.exists(apk):
-            raise Exception("No such signed apk: %s" % apk)
-            continue
+        logging.info("No signed output directory - nothing to do")
+        sys.exit(0)
+
+    if options.appid:
+
+        vercodes = common.read_pkg_args(options.appid, True)
+        apks = {appid: None for appid in vercodes}
+
+        # Get the signed apk with the highest vercode
+        for apkfile in sorted(glob.glob(os.path.join(output_dir, '*.apk'))):
+
+            try:
+                appid, vercode = common.apknameinfo(apkfile)
+            except FDroidException:
+                continue
+            if appid not in apks:
+                continue
+            if vercodes[appid] and vercode not in vercodes[appid]:
+                continue
+            apks[appid] = apkfile
+
+        for appid, apk in apks.iteritems():
+            if not apk:
+                raise FDroidException("No signed apk available for %s" % appid)
+
+    else:
+
+        apks = {common.apknameinfo(apkfile)[0]: apkfile for apkfile in
+                sorted(glob.glob(os.path.join(output_dir, '*.apk')))}
+
+    for appid, apk in apks.iteritems():
         # Get device list each time to avoid device not found errors
         devs = devices()
         if not devs:
-            raise Exception("No attached devices found")
-        print "Installing %s..." % apk
+            raise FDroidException("No attached devices found")
+        logging.info("Installing %s..." % apk)
         for dev in devs:
-            print "Installing %s on %s..." % (apk, dev)
-            p = FDroidPopen(["adb", "-s", dev, "install", apk ])
-            fail= ""
-            for line in p.stdout.splitlines():
+            logging.info("Installing %s on %s..." % (apk, dev))
+            p = SdkToolsPopen(['adb', "-s", dev, "install", apk])
+            fail = ""
+            for line in p.output.splitlines():
                 if line.startswith("Failure"):
                     fail = line[9:-1]
-            if fail:
-                raise Exception("Failed to install %s on %s: %s" % (
+            if not fail:
+                continue
+
+            if fail == "INSTALL_FAILED_ALREADY_EXISTS":
+                logging.warn("%s is already installed on %s." % (apk, dev))
+            else:
+                raise FDroidException("Failed to install %s on %s: %s" % (
                     apk, dev, fail))
 
-    print "\nFinished"
+    logging.info("\nFinished")
 
 if __name__ == "__main__":
     main()
-