chiark / gitweb /
Merge branch 'ndk' into 'master'
[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 common
26 from . import metadata
27
28 config = None
29 options = None
30
31
32 def proper_format(app):
33     s = io.StringIO()
34     # TODO: currently reading entire file again, should reuse first
35     # read in metadata.py
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()
40     s.close()
41     return content == cur_content
42
43
44 def main():
45
46     global config, options
47
48     supported = ['txt', 'yml']
49
50     # Parse command line...
51     parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
52     common.setup_global_opts(parser)
53     parser.add_argument("-l", "--list", action="store_true", default=False,
54                         help="List files that would be reformatted")
55     parser.add_argument("-t", "--to", default=None,
56                         help="Rewrite to a specific format: " + ', '.join(supported))
57     parser.add_argument("appid", nargs='*', help="app-id in the form APPID")
58     metadata.add_metadata_arguments(parser)
59     options = parser.parse_args()
60     metadata.warnings_action = options.W
61
62     config = common.read_config(options)
63
64     # Get all apps...
65     allapps = metadata.read_metadata(xref=True)
66     apps = common.read_app_args(options.appid, allapps, False)
67
68     if options.list and options.to is not None:
69         parser.error("Cannot use --list and --to at the same time")
70
71     if options.to is not None and options.to not in supported:
72         parser.error("Unsupported metadata format, use: --to [" + ' '.join(supported) + "]")
73
74     for appid, app in apps.items():
75         path = app.metadatapath
76         base, ext = common.get_extension(path)
77         if not options.to and ext not in supported:
78             logging.info("Ignoring %s file at '%s'" % (ext, path))
79             continue
80         else:
81             logging.info("rewriting '%s' to %s" % (appid, options.to))
82
83         to_ext = ext
84         if options.to is not None:
85             to_ext = options.to
86
87         if options.list:
88             if not proper_format(app):
89                 print(path)
90             continue
91
92         newbuilds = []
93         for build in app.builds:
94             new = metadata.Build()
95             for k in metadata.build_flags:
96                 v = build[k]
97                 if v is None or v is False or v == [] or v == '':
98                     continue
99                 new[k] = v
100             newbuilds.append(new)
101         app.builds = newbuilds
102
103         metadata.write_metadata(base + '.' + to_ext, app)
104
105         if ext != to_ext:
106             os.remove(path)
107
108     logging.debug("Finished.")
109
110
111 if __name__ == "__main__":
112     main()