chiark / gitweb /
remove XML files from bash completion, they are not supported anymore
[fdroidserver.git] / fdroidserver / rewritemeta.py
1 #!/usr/bin/env python3
2 #
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
6 #
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.
11 #
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.
16 #
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/>.
19
20 from argparse import ArgumentParser
21 import os
22 import logging
23 import io
24
25 from . import _
26 from . import common
27 from . import metadata
28
29 config = None
30 options = None
31
32
33 def proper_format(app):
34     s = io.StringIO()
35     # TODO: currently reading entire file again, should reuse first
36     # read in metadata.py
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()
41     s.close()
42     return content == cur_content
43
44
45 def main():
46
47     global config, options
48
49     supported = ['txt', 'yml']
50
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
62
63     config = common.read_config(options)
64
65     # Get all apps...
66     allapps = metadata.read_metadata(xref=True)
67     apps = common.read_app_args(options.appid, allapps, False)
68
69     if options.list and options.to is not None:
70         parser.error(_("Cannot use --list and --to at the same time"))
71
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)))
75
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))
81             continue
82         elif options.to is not None:
83             logging.info(_("Rewriting '{appid}' to '{path}'").format(appid=appid, path=options.to))
84         else:
85             logging.info(_("Rewriting '{appid}'").format(appid=appid))
86
87         to_ext = ext
88         if options.to is not None:
89             to_ext = options.to
90
91         if options.list:
92             if not proper_format(app):
93                 print(path)
94             continue
95
96         newbuilds = []
97         for build in app.builds:
98             new = metadata.Build()
99             for k in metadata.build_flags:
100                 v = build[k]
101                 if v is None or v is False or v == [] or v == '':
102                     continue
103                 new[k] = v
104             newbuilds.append(new)
105         app.builds = newbuilds
106
107         metadata.write_metadata(base + '.' + to_ext, app)
108
109         if ext != to_ext:
110             os.remove(path)
111
112     logging.debug(_("Finished"))
113
114
115 if __name__ == "__main__":
116     main()