chiark / gitweb /
remove dependency on wget for 'build' and 'verify'
[fdroidserver.git] / fdroidserver / verify.py
index d81d40737cd73f9932b1becefe1e604004ee363e..fd0464ebddf78c2ca7107e857bd0b18a23463902 100644 (file)
 
 import sys
 import os
-import shutil
-import subprocess
 import glob
 from optparse import OptionParser
+import logging
 
 import common
-from common import BuildException
+from common import FDroidException
 
 options = None
 config = None
 
+
 def main():
 
     global options, config
 
-    options, args = parse_commandline()
-
     # 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("-p", "--package", default=None,
-                      help="Verify only the specified package")
+    parser.add_option("-q", "--quiet", action="store_true", default=False,
+                      help="Restrict output to warnings and errors")
     (options, args) = parser.parse_args()
 
     config = common.read_config(options)
 
     tmp_dir = 'tmp'
     if not os.path.isdir(tmp_dir):
-        print "Creating temporary directory"
+        logging.info("Creating temporary directory")
         os.makedirs(tmp_dir)
 
     unsigned_dir = 'unsigned'
     if not os.path.isdir(unsigned_dir):
-        print "No unsigned directory - nothing to do"
+        logging.error("No unsigned directory - nothing to do")
         sys.exit(0)
 
     verified = 0
     notverified = 0
 
+    vercodes = common.read_pkg_args(args, True)
+
     for apkfile in sorted(glob.glob(os.path.join(unsigned_dir, '*.apk'))):
 
         apkfilename = os.path.basename(apkfile)
-        i = apkfilename.rfind('_')
-        if i == -1:
-            raise BuildException("Invalid apk name")
-        appid = apkfilename[:i]
-
-        if not options.package or options.package == appid:
-
-            try:
-
-                print "Processing " + apkfilename
-
-                remoteapk = os.path.join(tmp_dir, apkfilename)
-                if os.path.exists(remoteapk):
-                    os.remove(remoteapk)
-                url = 'https://f-droid.org/repo/' + apkfilename
-                print "...retrieving " + url
-                p = subprocess.Popen(['wget', url],
-                    cwd=tmp_dir,
-                    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-                out = p.communicate()[0]
-                if p.returncode != 0:
-                    raise Exception("Failed to get " + apkfilename)
-
-                thisdir = os.path.join(tmp_dir, 'this_apk')
-                thatdir = os.path.join(tmp_dir, 'that_apk')
-                for d in [thisdir, thatdir]:
-                    if os.path.exists(d):
-                        shutil.rmtree(d)
-                    os.mkdir(d)
-
-                if subprocess.call(['jar', 'xf',
-                    os.path.join("..", "..", unsigned_dir, apkfilename)],
-                    cwd=thisdir) != 0:
-                    raise Exception("Failed to unpack local build of " + apkfilename)
-                if subprocess.call(['jar', 'xf', os.path.join("..", "..", remoteapk)],
-                    cwd=thatdir) != 0:
-                    raise Exception("Failed to unpack remote build of " + apkfilename)
-
-                p = subprocess.Popen(['diff', '-r', 'this_apk', 'that_apk'],
-                    cwd=tmp_dir, stdout=subprocess.PIPE)
-                out = p.communicate()[0]
-                lines = out.splitlines()
-                if len(lines) != 1 or lines[0].find('META-INF') == -1:
-                    raise Exception("Unexpected diff output - " + out)
-
-                print "...successfully verified"
-                verified += 1
-
-            except Exception, e:
-                print "...NOT verified - {0}".format(e)
-                notverified += 1
-
-    print "\nFinished"
-    print "{0} successfully verified".format(verified)
-    print "{0} NOT verified".format(notverified)
+        appid, vercode = common.apknameinfo(apkfile)
 
-if __name__ == "__main__":
-    main()
+        if vercodes and appid not in vercodes:
+            continue
+        if vercodes[appid] and vercode not in vercodes[appid]:
+            continue
+
+        try:
+
+            logging.info("Processing " + apkfilename)
 
+            remoteapk = os.path.join(tmp_dir, apkfilename)
+            if os.path.exists(remoteapk):
+                os.remove(remoteapk)
+            url = 'https://f-droid.org/repo/' + apkfilename
+            logging.info("...retrieving " + url)
+            common.download_file(url, dldir=tmp_dir)
 
+            compare_result = common.compare_apks(
+                os.path.join(unsigned_dir, apkfilename),
+                remoteapk,
+                tmp_dir)
+            if compare_result:
+                raise FDroidException(compare_result)
+
+            logging.info("...successfully verified")
+            verified += 1
+
+        except FDroidException, e:
+            logging.info("...NOT verified - {0}".format(e))
+            notverified += 1
+
+    logging.info("Finished")
+    logging.info("{0} successfully verified".format(verified))
+    logging.info("{0} NOT verified".format(notverified))
+
+if __name__ == "__main__":
+    main()