chiark / gitweb /
Switch all headers to python3
[fdroidserver.git] / fdroidserver / install.py
index 50bf59c5b69a4cafc109af1042e35fc6d73f63d5..ebb66232a1b2a65d05c583a6bb0f01eb4e091029 100644 (file)
@@ -1,5 +1,4 @@
-#!/usr/bin/env python2
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
 #
 # install.py - part of the FDroid server tools
 # Copyright (C) 2013, Ciaran Gultnieks, ciaran@ciarang.com
 import sys
 import os
 import glob
-from optparse import OptionParser, OptionError
+from argparse import ArgumentParser
 import logging
 
 import common
-from common import FDroidPopen
+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.output)
-    lines = p.output.splitlines()
-    if lines[0].startswith('* daemon not running'):
-        lines = lines[2:]
+        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]
@@ -49,17 +46,15 @@ def main():
     global options, config
 
     # Parse command line...
-    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")
+    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)
 
@@ -68,15 +63,18 @@ def main():
         logging.info("No signed output directory - nothing to do")
         sys.exit(0)
 
-    if args:
+    if options.appid:
 
-        vercodes = common.read_pkg_args(args, True)
+        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'))):
 
-            appid, vercode = common.apknameinfo(apkfile)
+            try:
+                appid, vercode = common.apknameinfo(apkfile)
+            except FDroidException:
+                continue
             if appid not in apks:
                 continue
             if vercodes[appid] and vercode not in vercodes[appid]:
@@ -85,7 +83,7 @@ def main():
 
         for appid, apk in apks.iteritems():
             if not apk:
-                raise Exception("No signed apk available for %s" % appid)
+                raise FDroidException("No signed apk available for %s" % appid)
 
     else:
 
@@ -96,11 +94,11 @@ def main():
         # Get device list each time to avoid device not found errors
         devs = devices()
         if not devs:
-            raise Exception("No attached devices found")
+            raise FDroidException("No attached devices found")
         logging.info("Installing %s..." % apk)
         for dev in devs:
             logging.info("Installing %s on %s..." % (apk, dev))
-            p = FDroidPopen(["adb", "-s", dev, "install", apk])
+            p = SdkToolsPopen(['adb', "-s", dev, "install", apk])
             fail = ""
             for line in p.output.splitlines():
                 if line.startswith("Failure"):
@@ -111,7 +109,7 @@ def main():
             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" % (
+                raise FDroidException("Failed to install %s on %s: %s" % (
                     apk, dev, fail))
 
     logging.info("\nFinished")