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
27 from . import metadata
33 def proper_format(app):
35 # TODO: currently reading entire file again, should reuse first
37 with open(app.metadatapath, 'r', encoding='utf8') as f:
38 cur_content = f.read()
39 metadata.write_txt(s, app)
40 content = s.getvalue()
42 return content == cur_content
47 global config, options
49 supported = ['txt', 'yml']
51 # Parse command line...
52 parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
53 common.setup_global_opts(parser)
54 parser.add_argument("-l", "--list", action="store_true", default=False,
55 help=_("List files that would be reformatted"))
56 parser.add_argument("-t", "--to", default=None,
57 help=_("Rewrite to a specific format: ") + ', '.join(supported))
58 parser.add_argument("appid", nargs='*', help=_("applicationId in the form APPID"))
59 metadata.add_metadata_arguments(parser)
60 options = parser.parse_args()
61 metadata.warnings_action = options.W
63 config = common.read_config(options)
66 allapps = metadata.read_metadata(xref=True)
67 apps = common.read_app_args(options.appid, allapps, False)
69 if options.list and options.to is not None:
70 parser.error(_("Cannot use --list and --to at the same time"))
72 if options.to is not None and options.to not in supported:
73 parser.error(_("Unsupported metadata format, use: --to [{supported}]")
74 .format(supported=' '.join(supported)))
76 for appid, app in apps.items():
77 path = app.metadatapath
78 base, ext = common.get_extension(path)
79 if not options.to and ext not in supported:
80 logging.info(_("Ignoring {ext} file at '{path}'").format(ext=ext, path=path))
82 elif options.to is not None:
83 logging.info(_("Rewriting '{appid}' to '{path}'").format(appid=appid, path=options.to))
85 logging.info(_("Rewriting '{appid}'").format(appid=appid))
88 if options.to is not None:
92 if not proper_format(app):
97 for build in app.builds:
98 new = metadata.Build()
99 for k in metadata.build_flags:
101 if v is None or v is False or v == [] or v == '':
104 newbuilds.append(new)
105 app.builds = newbuilds
107 metadata.write_metadata(base + '.' + to_ext, app)
112 logging.debug(_("Finished"))
115 if __name__ == "__main__":