1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-08-17 20:00:10 +02:00

Simplify some file logic with "with"

This commit is contained in:
Daniel Martí 2015-08-28 18:37:23 -07:00
parent 2894786ec9
commit 6fe8d96e85
4 changed files with 48 additions and 63 deletions

View 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)

View 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':

View 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()
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:')

View 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: