chiark / gitweb /
properly close opened images
authorHans-Christoph Steiner <hans@eds.org>
Mon, 20 Jun 2016 12:01:56 +0000 (14:01 +0200)
committerHans-Christoph Steiner <hans@eds.org>
Mon, 27 Jun 2016 12:00:49 +0000 (14:00 +0200)
This stops these errors:

fdroid/fdroidserver/fdroidserver/update.py:744: ResourceWarning: unclosed
 file <_io.BufferedReader
 name='repo/icons-320/info.guardianproject.urzip.100.png'>

fdroid/fdroidserver/fdroidserver/update.py:721: DeprecationWarning: The
 'warn' function is deprecated, use 'warning' instead

fdroidserver/update.py

index cb1cb10f0c9e0607cf6117753dd696671b193af1..e91ef4822ebbc3dae07a117f819ca225c2cdbe6e 100644 (file)
@@ -323,8 +323,10 @@ def resize_icon(iconpath, density):
     if not os.path.isfile(iconpath):
         return
 
+    fp = None
     try:
-        im = Image.open(iconpath)
+        fp = open(iconpath, 'rb')
+        im = Image.open(fp)
         size = dpi_to_px(density)
 
         if any(length > size for length in im.size):
@@ -337,6 +339,10 @@ def resize_icon(iconpath, density):
     except Exception as e:
         logging.error("Failed resizing {0} - {1}".format(iconpath, e))
 
+    finally:
+        if fp:
+            fp.close()
+
 
 def resize_all_icons(repodirs):
     """Resize all icons that exceed the max size
@@ -563,7 +569,7 @@ def scan_apks(apps, apkcache, repodir, knownapks, use_date_from_apk=False):
 
             # Check for debuggable apks...
             if common.isApkDebuggable(apkfile, config):
-                logging.warn('{0} is set to android:debuggable="true"'.format(apkfile))
+                logging.warning('{0} is set to android:debuggable="true"'.format(apkfile))
 
             # Get the signature (or md5 of, to be precise)...
             logging.debug('Getting signature of {0}'.format(apkfile))
@@ -657,17 +663,21 @@ def scan_apks(apps, apkcache, repodir, knownapks, use_date_from_apk=False):
                     get_icon_dir(repodir, last_density), iconfilename)
                 iconpath = os.path.join(
                     get_icon_dir(repodir, density), iconfilename)
+                fp = None
                 try:
-                    im = Image.open(last_iconpath)
-                except:
-                    logging.warn("Invalid image file at %s" % last_iconpath)
-                    continue
+                    fp = open(last_iconpath, 'rb')
+                    im = Image.open(fp)
 
-                size = dpi_to_px(density)
+                    size = dpi_to_px(density)
 
-                im.thumbnail((size, size), Image.ANTIALIAS)
-                im.save(iconpath, "PNG")
-                empty_densities.remove(density)
+                    im.thumbnail((size, size), Image.ANTIALIAS)
+                    im.save(iconpath, "PNG")
+                    empty_densities.remove(density)
+                except:
+                    logging.warning("Invalid image file at %s" % last_iconpath)
+                finally:
+                    if fp:
+                        fp.close()
 
             # Then just copy from the highest resolution available
             last_density = None