2 # -*- coding: utf-8 -*-
4 # rewritemeta.py - part of the FDroid server tools
5 # This cleans up the original .txt metadata file format.
6 # Copyright (C) 2010-12, Ciaran Gultnieks, ciaran@ciarang.com
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from argparse import ArgumentParser
25 from cStringIO import StringIO
27 from StringIO import StringIO
36 def proper_format(app):
38 # TODO: currently reading entire file again, should reuse first
40 with open(app.metadatapath, 'r') as f:
41 cur_content = f.read()
42 metadata.write_txt_metadata(s, app)
43 content = s.getvalue()
45 return content == cur_content
50 global config, options
52 # Parse command line...
53 parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
54 common.setup_global_opts(parser)
55 parser.add_argument("-l", "--list", action="store_true", default=False,
56 help="List files that would be reformatted")
57 parser.add_argument("-t", "--to", default=None,
58 help="Rewrite to a specific format")
59 parser.add_argument("appid", nargs='*', help="app-id in the form APPID")
60 options = parser.parse_args()
62 config = common.read_config(options)
65 allapps = metadata.read_metadata(xref=True)
66 apps = common.read_app_args(options.appid, allapps, False)
68 if options.list and options.to is not None:
69 parser.error("Cannot use --list and --to at the same time")
71 supported = ['txt', 'yaml']
73 if options.to is not None and options.to not in supported:
74 parser.error("Must give a valid format to --to")
76 for appid, app in apps.iteritems():
77 base, ext = common.get_extension(app.metadatapath)
78 if not options.to and ext not in supported:
79 logging.info("Ignoring %s file at '%s'" % (ext, app.metadatapath))
83 if options.to is not None:
87 if not proper_format(app):
88 print app.metadatapath
91 with open(base + '.' + to_ext, 'w') as f:
92 metadata.write_metadata(to_ext, f, app)
95 os.remove(app.metadatapath)
97 logging.debug("Finished.")
99 if __name__ == "__main__":