download_file.py: sync with version from Gerrit 2.11

Change-Id: I7738ddcbc65832149f80ca0eeb1250865c87acdd
diff --git a/tools/download_file.py b/tools/download_file.py
index ab8647d..88ab41a 100755
--- a/tools/download_file.py
+++ b/tools/download_file.py
@@ -28,6 +28,7 @@
 CACHE_DIR = path.join(GERRIT_HOME, 'buck-cache')
 LOCAL_PROPERTIES = 'local.properties'
 
+
 def hashfile(p):
   d = sha1()
   with open(p, 'rb') as f:
@@ -38,6 +39,7 @@
       d.update(b)
   return d.hexdigest()
 
+
 def safe_mkdirs(d):
   if path.isdir(d):
     return
@@ -47,6 +49,7 @@
     if not path.isdir(d):
       raise err
 
+
 def download_properties(root_dir):
   """ Get the download properties.
 
@@ -73,14 +76,16 @@
       pass
   return p
 
+
 def cache_entry(args):
   if args.v:
     h = args.v
   else:
-    h = sha1(args.u).hexdigest()
+    h = sha1(args.u.encode('utf-8')).hexdigest()
   name = '%s-%s' % (path.basename(args.o), h)
   return path.join(CACHE_DIR, name)
 
+
 opts = OptionParser()
 opts.add_option('-o', help='local output file')
 opts.add_option('-u', help='URL to download')
@@ -137,30 +142,24 @@
   exclude += args.x
 if args.exclude_java_sources:
   try:
-    zf = ZipFile(cache_ent, 'r')
-    try:
+    with ZipFile(cache_ent, 'r') as zf:
       for n in zf.namelist():
         if n.endswith('.java'):
           exclude.append(n)
-    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:
+    with ZipFile(cache_ent, 'r') as zf:
       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)
+    print('error opening %s: %s' % (cache_ent, err), file=stderr)
     exit(1)
 
 safe_mkdirs(path.dirname(args.o))