Enable easy signing of apks with apk_utils.py

Also, update to python3

Change-Id: Ifb990d734112baf5fae34d10c3ff4cdf7fde3739
diff --git a/tools/apk_utils.py b/tools/apk_utils.py
old mode 100644
new mode 100755
index 9155657..c3c616b
--- a/tools/apk_utils.py
+++ b/tools/apk_utils.py
@@ -1,12 +1,40 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
 # for details. All rights reserved. Use of this source code is governed by a
 # BSD-style license that can be found in the LICENSE file.
 
+import optparse
 import os
 import subprocess
+import sys
 import utils
 
+USAGE = 'usage: %prog [options] <apk>'
+
+def parse_options():
+  parser = optparse.OptionParser(usage=USAGE)
+  parser.add_option('--keystore',
+                    help='keystore file (default ~/.android/app.keystore)',
+                    default='~/.android/app.keystore')
+  parser.add_option('--sign',
+                    help='Sign the passed in apk.',
+                    default=False,
+                    action='store_true')
+  parser.add_option('--use_apksigner',
+                    help='Use apksigner to sign.',
+                    default=False,
+                    action='store_true')
+  parser.add_option('--output',
+                    help='Where to put the signed apk.)',
+                    default=None)
+
+  (options, args) = parser.parse_args()
+  if len(args) != 1:
+    parser.error('Expected <apk> argument, got: ' + ' '.join(args))
+  apk = args[0]
+  return (options, apk)
+
+
 def sign(unsigned_apk, signed_apk, keystore, quiet=False, logging=True):
   utils.Print('Signing (ignore the warnings)', quiet=quiet)
   cmd = ['zip', '-d', unsigned_apk, 'META-INF/*']
@@ -37,3 +65,22 @@
     unsigned_apk
   ]
   utils.RunCmd(cmd, quiet=quiet, logging=logging)
+
+
+def main():
+  (options, apk) = parse_options()
+  if options.sign:
+    if not options.output:
+      print('When signing you must specify an output apk')
+      return 1
+    if not options.keystore:
+      print('When signing you must specify a keystore')
+      return 1
+    if options.use_apksigner:
+      sign_with_apksigner(apk, options.output, options.keystore)
+    else:
+      sign(apk, options.output, options.keystore)
+  return 0
+
+if __name__ == '__main__':
+  sys.exit(main())
diff --git a/tools/utils.py b/tools/utils.py
index 0b5b03e..a55ea58 100644
--- a/tools/utils.py
+++ b/tools/utils.py
@@ -169,13 +169,12 @@
   logger = ProgressLogger(quiet=quiet) if logging else None
   failed = False
   while True:
-    line = process.stdout.readline()
-    if line != b'':
+    line = process.stdout.readline().decode('utf-8')
+    if line != '':
       stripped = line.rstrip()
       stdout.append(stripped)
       if logger:
         logger.log(stripped)
-
       # TODO(christofferqa): r8 should fail with non-zero exit code.
       if ('AssertionError:' in stripped
           or 'CompilationError:' in stripped