From 6fe8d96e852927b72d9418ebc43b303ab43ee24d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 28 Aug 2015 18:37:23 -0700 Subject: [PATCH] Simplify some file logic with "with" --- fdroidserver/build.py | 5 ++- fdroidserver/common.py | 10 +++--- fdroidserver/stats.py | 70 +++++++++++++++++++----------------------- fdroidserver/update.py | 26 +++++++--------- 4 files changed, 48 insertions(+), 63 deletions(-) diff --git a/fdroidserver/build.py b/fdroidserver/build.py index 0c2d67dd..e84cd45a 100644 --- a/fdroidserver/build.py +++ b/fdroidserver/build.py @@ -1097,9 +1097,8 @@ def main(): build_succeeded.append(app) wikilog = "Build succeeded" except BuildException as be: - logfile = open(os.path.join(log_dir, appid + '.log'), 'a+') - logfile.write(str(be)) - logfile.close() + with open(os.path.join(log_dir, appid + '.log'), 'a+') as f: + f.write(str(be)) print("Could not build app %s due to BuildException: %s" % (appid, be)) if options.stop: sys.exit(1) diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 5a983f5c..ddeb29ff 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -1247,9 +1247,8 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver= props = "" if os.path.isfile(path): logging.info("Updating local.properties file at %s" % path) - f = open(path, 'r') - props += f.read() - f.close() + with open(path, 'r') as f: + props += f.read() props += '\n' else: logging.info("Creating local.properties file at %s" % path) @@ -1269,9 +1268,8 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver= # Add java.encoding if necessary if build['encoding']: props += "java.encoding=%s\n" % build['encoding'] - f = open(path, 'w') - f.write(props) - f.close() + with open(path, 'w') as f: + f.write(props) flavours = [] if build['type'] == 'gradle': diff --git a/fdroidserver/stats.py b/fdroidserver/stats.py index 8004658e..b320f9db 100644 --- a/fdroidserver/stats.py +++ b/fdroidserver/stats.py @@ -200,22 +200,21 @@ def main(): count) alldownloads += count lst.append("ALL " + str(alldownloads)) - f = open('stats/total_downloads_app.txt', 'w') - f.write('# Total downloads by application, since October 2011\n') - for line in sorted(lst): - f.write(line + '\n') - f.close() + with open('stats/total_downloads_app.txt', 'w') as f: + f.write('# Total downloads by application, since October 2011\n') + for line in sorted(lst): + f.write(line + '\n') - f = open('stats/total_downloads_app_version.txt', 'w') - f.write('# Total downloads by application and version, ' - 'since October 2011\n') lst = [] for appver in appsvercount: count = appsvercount[appver] lst.append(appver + " " + str(count)) - for line in sorted(lst): - f.write(line + "\n") - f.close() + + with open('stats/total_downloads_app_version.txt', 'w') as f: + f.write('# Total downloads by application and version, ' + 'since October 2011\n') + for line in sorted(lst): + f.write(line + "\n") # Calculate and write stats for repo types... logging.info("Processing repo types...") @@ -225,10 +224,9 @@ def main(): if rtype == 'srclib': rtype = common.getsrclibvcs(app['Repo']) repotypes[rtype] += 1 - f = open('stats/repotypes.txt', 'w') - for rtype, count in repotypes.most_common(): - f.write(rtype + ' ' + str(count) + '\n') - f.close() + with open('stats/repotypes.txt', 'w') as f: + for rtype, count in repotypes.most_common(): + f.write(rtype + ' ' + str(count) + '\n') # Calculate and write stats for update check modes... logging.info("Processing update check modes...") @@ -240,20 +238,18 @@ def main(): if checkmode.startswith('Tags '): checkmode = checkmode[:4] ucms[checkmode] += 1 - f = open('stats/update_check_modes.txt', 'w') - for checkmode, count in ucms.most_common(): - f.write(checkmode + ' ' + str(count) + '\n') - f.close() + with open('stats/update_check_modes.txt', 'w') as f: + for checkmode, count in ucms.most_common(): + f.write(checkmode + ' ' + str(count) + '\n') logging.info("Processing categories...") ctgs = Counter() for app in metaapps: for category in app['Categories']: ctgs[category] += 1 - f = open('stats/categories.txt', 'w') - for category, count in ctgs.most_common(): - f.write(category + ' ' + str(count) + '\n') - f.close() + with open('stats/categories.txt', 'w') as f: + for category, count in ctgs.most_common(): + f.write(category + ' ' + str(count) + '\n') logging.info("Processing antifeatures...") afs = Counter() @@ -263,10 +259,9 @@ def main(): antifeatures = [a.strip() for a in app['AntiFeatures'].split(',')] for antifeature in antifeatures: afs[antifeature] += 1 - f = open('stats/antifeatures.txt', 'w') - for antifeature, count in afs.most_common(): - f.write(antifeature + ' ' + str(count) + '\n') - f.close() + with open('stats/antifeatures.txt', 'w') as f: + for antifeature, count in afs.most_common(): + f.write(antifeature + ' ' + str(count) + '\n') # Calculate and write stats for licenses... logging.info("Processing licenses...") @@ -274,26 +269,23 @@ def main(): for app in metaapps: license = app['License'] licenses[license] += 1 - f = open('stats/licenses.txt', 'w') - for license, count in licenses.most_common(): - f.write(license + ' ' + str(count) + '\n') - f.close() + with open('stats/licenses.txt', 'w') as f: + for license, count in licenses.most_common(): + f.write(license + ' ' + str(count) + '\n') # Write list of disabled apps... logging.info("Processing disabled apps...") disabled = [a['id'] for a in allmetaapps if a['Disabled']] - f = open('stats/disabled_apps.txt', 'w') - for appid in sorted(disabled): - f.write(appid + '\n') - f.close() + with open('stats/disabled_apps.txt', 'w') as f: + for appid in sorted(disabled): + f.write(appid + '\n') # Write list of latest apps added to the repo... logging.info("Processing latest apps...") latest = knownapks.getlatest(10) - f = open('stats/latestapps.txt', 'w') - for appid in latest: - f.write(appid + '\n') - f.close() + with open('stats/latestapps.txt', 'w') as f: + for appid in latest: + f.write(appid + '\n') if unknownapks: logging.info('\nUnknown apks:') diff --git a/fdroidserver/update.py b/fdroidserver/update.py index 3c3a95fd..291861a3 100644 --- a/fdroidserver/update.py +++ b/fdroidserver/update.py @@ -572,9 +572,8 @@ def scan_apks(apps, apkcache, repodir, knownapks): icondest = os.path.join(icon_dir, iconfilename) try: - iconfile = open(icondest, 'wb') - iconfile.write(apk.read(iconsrc)) - iconfile.close() + with open(icondest, 'wb') as f: + f.write(apk.read(iconsrc)) thisinfo['icons'][density] = iconfilename except: @@ -587,9 +586,8 @@ def scan_apks(apps, apkcache, repodir, knownapks): iconsrc = thisinfo['icons_src']['-1'] iconpath = os.path.join( get_icon_dir(repodir, None), iconfilename) - iconfile = open(iconpath, 'wb') - iconfile.write(apk.read(iconsrc)) - iconfile.close() + with open(iconpath, 'wb') as f: + f.write(apk.read(iconsrc)) try: im = Image.open(iconpath) dpi = px_to_dpi(im.size[0]) @@ -923,13 +921,13 @@ def make_index(apps, sortedids, apks, repodir, archive, categories): os.remove(siglinkname) os.symlink(sigfile_path, siglinkname) - of = open(os.path.join(repodir, 'index.xml'), 'wb') if options.pretty: output = doc.toprettyxml() else: output = doc.toxml() - of.write(output) - of.close() + + with open(os.path.join(repodir, 'index.xml'), 'wb') as f: + f.write(output) if 'repo_keyalias' in config: @@ -975,9 +973,8 @@ def make_index(apps, sortedids, apks, repodir, archive, categories): catdata = '' for cat in categories: catdata += cat + '\n' - f = open(os.path.join(repodir, 'categories.txt'), 'w') - f.write(catdata) - f.close() + with open(os.path.join(repodir, 'categories.txt'), 'w') as f: + f.write(catdata) def archive_old_apks(apps, apks, archapks, repodir, archivedir, defaultkeepversions): @@ -1278,9 +1275,8 @@ def main(): if app['icon'] is not None: data += app['icon'] + "\t" data += app['License'] + "\n" - f = open(os.path.join(repodirs[0], 'latestapps.dat'), 'w') - f.write(data) - f.close() + with open(os.path.join(repodirs[0], 'latestapps.dat'), 'w') as f: + f.write(data) if cachechanged: with open(apkcachefile, 'wb') as cf: