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
This commit is contained in:
Hans-Christoph Steiner 2016-06-20 14:01:56 +02:00
parent 1210dc7769
commit d6c9a8466b

View file

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