3 # rewritemeta.py - part of the FDroid server tools
4 # This cleans up the original .txt metadata file format.
5 # Copyright (C) 2010-12, Ciaran Gultnieks, ciaran@ciarang.com
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from argparse import ArgumentParser
26 from . import metadata
32 def proper_format(app):
34 # TODO: currently reading entire file again, should reuse first
36 with open(app.metadatapath, 'r', encoding='utf8') as f:
37 cur_content = f.read()
38 metadata.write_txt(s, app)
39 content = s.getvalue()
41 return content == cur_content
46 global config, options
48 # Parse command line...
49 parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
50 common.setup_global_opts(parser)
51 parser.add_argument("-l", "--list", action="store_true", default=False,
52 help="List files that would be reformatted")
53 parser.add_argument("-t", "--to", default=None,
54 help="Rewrite to a specific format")
55 parser.add_argument("appid", nargs='*', help="app-id in the form APPID")
56 options = parser.parse_args()
58 config = common.read_config(options)
61 allapps = metadata.read_metadata(xref=True)
62 apps = common.read_app_args(options.appid, allapps, False)
64 if options.list and options.to is not None:
65 parser.error("Cannot use --list and --to at the same time")
67 supported = ['txt', 'yml']
69 if options.to is not None and options.to not in supported:
70 parser.error("Must give a valid format to --to")
72 for appid, app in apps.items():
73 base, ext = common.get_extension(app.metadatapath)
74 if not options.to and ext not in supported:
75 logging.info("Ignoring %s file at '%s'" % (ext, app.metadatapath))
79 if options.to is not None:
83 if not proper_format(app):
84 print(app.metadatapath)
87 metadata.write_metadata(base + '.' + to_ext, app)
90 os.remove(app.metadatapath)
92 logging.debug("Finished.")
94 if __name__ == "__main__":