From d6c9a8466b19ebe100601fc1028dfc9e228263c2 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 20 Jun 2016 14:01:56 +0200 Subject: [PATCH] properly close opened images 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 | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/fdroidserver/update.py b/fdroidserver/update.py index cb1cb10f..e91ef482 100644 --- a/fdroidserver/update.py +++ b/fdroidserver/update.py @@ -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 -- 2.30.2