chiark / gitweb /
Also use adb via a full path
[fdroidserver.git] / fdroidserver / install.py
index 560d9979e633da5bc5fd2c337e54699b8ad7fffe..673efd6f1efd8b8b286e4988dddf47749cb359ea 100644 (file)
@@ -1,9 +1,9 @@
 #!/usr/bin/env python2
 # -*- coding: utf-8 -*-
 #
-# 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 Daniel Martí <mvdan@mvdan.cc>
+# 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
@@ -21,7 +21,8 @@
 import sys
 import os
 import glob
-from optparse import OptionParser
+from optparse import OptionParser, OptionError
+import logging
 
 import common
 from common import FDroidPopen
@@ -29,11 +30,18 @@ from common import FDroidPopen
 options = None
 config = None
 
+
 def devices():
-    p = FDroidPopen(["adb", "devices"])
+    p = FDroidPopen([config['adb'], "devices"])
     if p.returncode != 0:
-        raise Exception("An error occured when finding devices: %s" % p.stderr)
-    return [l.split()[0] for l in p.stdout.splitlines()[1:-1]]
+        raise Exception("An error occured when finding devices: %s" % p.output)
+    lines = p.output.splitlines()
+    if lines[0].startswith('* daemon not running'):
+        lines = lines[2:]
+    if len(lines) < 3:
+        return []
+    lines = lines[1:-1]
+    return [l.split()[0] for l in lines]
 
 
 def main():
@@ -41,24 +49,29 @@ def main():
     global options, config
 
     # Parse command line...
-    parser = OptionParser()
+    parser = OptionParser(usage="Usage: %prog [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
     parser.add_option("-v", "--verbose", action="store_true", default=False,
                       help="Spew out even more information than normal")
+    parser.add_option("-q", "--quiet", action="store_true", default=False,
+                      help="Restrict output to warnings and errors")
     parser.add_option("-a", "--all", action="store_true", default=False,
                       help="Install all signed applications available")
     (options, args) = parser.parse_args()
 
+    if not args and not options.all:
+        raise OptionError("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)
+        logging.info("No signed output directory - nothing to do")
+        sys.exit(0)
 
     if args:
 
         vercodes = common.read_pkg_args(args, True)
-        apks = { appid : None for appid in vercodes }
+        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'))):
@@ -74,39 +87,34 @@ def main():
             if not apk:
                 raise Exception("No signed apk available for %s" % appid)
 
-    elif options.all:
+    else:
 
-        for apkfile in sorted(glob.glob(os.path.join(output_dir, '*.apk'))):
+        apks = {common.apknameinfo(apkfile)[0]: apkfile for apkfile in
+                sorted(glob.glob(os.path.join(output_dir, '*.apk')))}
 
-            appid, vercode = common.apknameinfo(apkfile)
-            apks[appid] = apkfile
-
-    else:
-        print "If you really want to install all the signed apps, use --all"
-        sys.exit(0)
-    
     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
+        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 = FDroidPopen([config['adb'], "-s", dev, "install", apk])
+            fail = ""
+            for line in p.output.splitlines():
                 if line.startswith("Failure"):
                     fail = line[9:-1]
-            if fail:
-                if fail == "INSTALL_FAILED_ALREADY_EXISTS":
-                    print "%s is already installed on %s." % (apk, dev)
-                else:
-                    raise Exception("Failed to install %s on %s: %s" % (
-                        apk, dev, fail))
+            if not fail:
+                continue
+
+            if fail == "INSTALL_FAILED_ALREADY_EXISTS":
+                logging.warn("%s is already installed on %s." % (apk, dev))
+            else:
+                raise Exception("Failed to install %s on %s: %s" % (
+                    apk, dev, fail))
 
-    print "\nFinished"
+    logging.info("\nFinished")
 
 if __name__ == "__main__":
     main()
-