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 _ignored, extension = common.get_extension(app.metadatapath)
40 if extension == 'yml':
41 metadata.write_yaml(s, app)
42 elif extension == 'txt':
43 metadata.write_txt(s, app)
44 content = s.getvalue()
46 return content == cur_content
51 global config, options
53 supported = ['txt', 'yml']
55 # Parse command line...
56 parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
57 common.setup_global_opts(parser)
58 parser.add_argument("-l", "--list", action="store_true", default=False,
59 help=_("List files that would be reformatted"))
60 parser.add_argument("-t", "--to", default=None,
61 help=_("Rewrite to a specific format: ") + ', '.join(supported))
62 parser.add_argument("appid", nargs='*', help=_("applicationId in the form APPID"))
63 metadata.add_metadata_arguments(parser)
64 options = parser.parse_args()
65 metadata.warnings_action = options.W
67 config = common.read_config(options)
70 allapps = metadata.read_metadata(xref=True)
71 apps = common.read_app_args(options.appid, allapps, False)
73 if options.list and options.to is not None:
74 parser.error(_("Cannot use --list and --to at the same time"))
76 if options.to is not None and options.to not in supported:
77 parser.error(_("Unsupported metadata format, use: --to [{supported}]")
78 .format(supported=' '.join(supported)))
80 for appid, app in apps.items():
81 path = app.metadatapath
82 base, ext = common.get_extension(path)
83 if not options.to and ext not in supported:
84 logging.info(_("Ignoring {ext} file at '{path}'").format(ext=ext, path=path))
86 elif options.to is not None:
87 logging.info(_("Rewriting '{appid}' to '{path}'").format(appid=appid, path=options.to))
89 logging.info(_("Rewriting '{appid}'").format(appid=appid))
92 if options.to is not None:
96 if not proper_format(app):
101 for build in app.builds:
102 new = metadata.Build()
103 for k in metadata.build_flags:
105 if v is None or v is False or v == [] or v == '':
108 newbuilds.append(new)
109 app.builds = newbuilds
111 metadata.write_metadata(base + '.' + to_ext, app)
116 logging.debug(_("Finished"))
119 if __name__ == "__main__":