Buck: extend the tool chain to support unsigning JARs

After switching to Eclipse Maven repository for pulling JGit lib, we
have the problem that according to Eclipse release train the JARs have
to be signed. That collids with our jgit patch for diff deserialization.

To rectify that, add `unsign` parameter to maven_jar() function.

Change-Id: Ib7bfa5d16f980a64b887d61a4b4ec325e6ffb0a1
diff --git a/lib/jgit/BUCK b/lib/jgit/BUCK
index 9f8a5c9..3e481bf 100644
--- a/lib/jgit/BUCK
+++ b/lib/jgit/BUCK
@@ -10,6 +10,7 @@
   src_sha1 = 'b4e3d9c9c3da39b72acf72bd913ce9dbee88a9d4',
   license = 'jgit',
   repository = REPO,
+  unsign = True,
   deps = [':ewah'],
   exclude = [
     'META-INF/eclipse.inf',
diff --git a/lib/maven.defs b/lib/maven.defs
index d49fed6..54840e8 100644
--- a/lib/maven.defs
+++ b/lib/maven.defs
@@ -33,6 +33,7 @@
     license,
     exclude = [],
     exclude_java_sources = False,
+    unsign = False,
     deps = [],
     sha1 = '', bin_sha1 = '', src_sha1 = '',
     repository = MAVEN_CENTRAL,
@@ -73,6 +74,8 @@
     cmd.extend(['-x', x])
   if exclude_java_sources:
     cmd.append('--exclude_java_sources')
+  if unsign:
+    cmd.append('--unsign')
 
   genrule(
     name = name + '__download_bin',
diff --git a/tools/download_file.py b/tools/download_file.py
index 2fcce3f..8d76a40 100755
--- a/tools/download_file.py
+++ b/tools/download_file.py
@@ -109,6 +109,7 @@
 opts.add_option('-v', help='expected content SHA-1')
 opts.add_option('-x', action='append', help='file to delete from ZIP')
 opts.add_option('--exclude_java_sources', action='store_true')
+opts.add_option('--unsign', action='store_true')
 args, _ = opts.parse_args()
 
 root_dir = args.o
@@ -166,7 +167,22 @@
     finally:
       zf.close()
   except (BadZipfile, LargeZipFile) as err:
-    print("error opening %s: %s"  % (cache_ent, err), file=stderr)
+    print('error opening %s: %s'  % (cache_ent, err), file=stderr)
+    exit(1)
+
+if args.unsign:
+  try:
+    zf = ZipFile(cache_ent, 'r')
+    try:
+      for n in zf.namelist():
+        if (n.endswith('.RSA')
+            or n.endswith('.SF')
+            or n.endswith('.LIST')):
+          exclude.append(n)
+    finally:
+      zf.close()
+  except (BadZipfile, LargeZipFile) as err:
+    print('error opening %s: %s'  % (cache_ent, err), file=stderr)
     exit(1)
 
 safe_mkdirs(path.dirname(args.o))
@@ -174,7 +190,7 @@
   try:
     shutil.copyfile(cache_ent, args.o)
   except (shutil.Error, IOError) as err:
-    print("error copying to %s: %s" % (args.o, err), file=stderr)
+    print('error copying to %s: %s' % (args.o, err), file=stderr)
     exit(1)
   try:
     check_call(['zip', '-d', args.o] + exclude)
@@ -188,5 +204,5 @@
     try:
       shutil.copyfile(cache_ent, args.o)
     except (shutil.Error, IOError) as err:
-      print("error copying to %s: %s" % (args.o, err), file=stderr)
+      print('error copying to %s: %s' % (args.o, err), file=stderr)
       exit(1)