From 911994fc99c0908cc25c3cc0c5ce2f99e0d163dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 24 Sep 2015 22:27:38 -0700 Subject: [PATCH] Make write_metadata take a writer This will let rewritemeta report format issues without writing to disk. --- fdroidserver/checkupdates.py | 3 ++- fdroidserver/import.py | 3 ++- fdroidserver/metadata.py | 6 ++---- fdroidserver/rewritemeta.py | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/fdroidserver/checkupdates.py b/fdroidserver/checkupdates.py index c57cd8be..a6b00130 100644 --- a/fdroidserver/checkupdates.py +++ b/fdroidserver/checkupdates.py @@ -505,7 +505,8 @@ def checkupdates_app(app, first=True): if commitmsg: metadatapath = os.path.join('metadata', app['id'] + '.txt') - metadata.write_metadata(metadatapath, app) + with open(metadatapath, 'w') as f: + metadata.write_metadata(f, app) if options.commit: logging.info("Commiting update for " + metadatapath) gitcmd = ["git", "commit", "-m", commitmsg] diff --git a/fdroidserver/import.py b/fdroidserver/import.py index ce19bd54..9841c7a1 100644 --- a/fdroidserver/import.py +++ b/fdroidserver/import.py @@ -248,7 +248,8 @@ def main(): f.write(app['Repo Type'] + ' ' + app['Repo']) metadatapath = os.path.join('metadata', package + '.txt') - metadata.write_metadata(metadatapath, app) + with open(metadatapath, 'w') as f: + metadata.write_metadata(f, app) logging.info("Wrote " + metadatapath) diff --git a/fdroidserver/metadata.py b/fdroidserver/metadata.py index e5ae6716..530f1350 100644 --- a/fdroidserver/metadata.py +++ b/fdroidserver/metadata.py @@ -1040,9 +1040,9 @@ def parse_txt_metadata(apps, metadatapath): # Write a metadata file. # -# 'dest' - The path to the output file +# 'mf' - Writer interface (file, StringIO, ...) # 'app' - The app data -def write_metadata(dest, app): +def write_metadata(mf, app): def writecomments(key): written = 0 @@ -1073,7 +1073,6 @@ def write_metadata(dest, app): if value: writefield(field, value) - mf = open(dest, 'w') writefield_nonempty('Disabled') if app['AntiFeatures']: writefield('AntiFeatures') @@ -1161,4 +1160,3 @@ def write_metadata(dest, app): writefield('No Source Since') mf.write('\n') writecomments(None) - mf.close() diff --git a/fdroidserver/rewritemeta.py b/fdroidserver/rewritemeta.py index 47e478a4..ecf685be 100644 --- a/fdroidserver/rewritemeta.py +++ b/fdroidserver/rewritemeta.py @@ -18,7 +18,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import os from argparse import ArgumentParser import logging import common @@ -46,13 +45,14 @@ def main(): for appid, app in apps.iteritems(): metadatapath = app['metadatapath'] - ext = os.path.splitext(metadatapath)[1][1:] - if ext == 'txt': - logging.debug("Rewriting " + metadatapath) - metadata.write_metadata(metadatapath, app) - else: + ext = common.get_extension(metadatapath) + if ext not in ['txt']: logging.info("Ignoring %s file at '%s'" % (ext.upper(), metadatapath)) + continue + logging.debug("Rewriting " + metadatapath) + with open(metadatapath, 'w') as f: + metadata.write_metadata(f, app) logging.debug("Finished.")