chiark / gitweb /
Simplify some file logic with "with"
authorDaniel Martí <mvdan@mvdan.cc>
Sat, 29 Aug 2015 01:37:23 +0000 (18:37 -0700)
committerDaniel Martí <mvdan@mvdan.cc>
Sat, 29 Aug 2015 01:37:23 +0000 (18:37 -0700)
fdroidserver/build.py
fdroidserver/common.py
fdroidserver/stats.py
fdroidserver/update.py

index 0c2d67dd8475558d8b88e07a9ad56fc596c6581e..e84cd45a42ef98eccd7b16286744501eba42ec2e 100644 (file)
@@ -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)
index 5a983f5ce363893e950839a9f4ac45b412402ecf..ddeb29ff2889ada0e2f3923a3f05a5aaaa7c3a37 100644 (file)
@@ -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':
index 8004658e783a8e3d6adc1824350aae9137e46d18..b320f9dbcbd470ced610f8a4f4c02c1df8605b94 100644 (file)
@@ -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()
-
-        f = open('stats/total_downloads_app_version.txt', 'w')
-        f.write('# Total downloads by application and version, '
-                'since October 2011\n')
+        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')
+
         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:')
index 3c3a95fd2a165d07dc18c38550d95b68027ac77b..291861a3cda1d8a39497863b9c5aaddd01414ae9 100644 (file)
@@ -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: