chiark / gitweb /
dd0cde10f6fee933e49401688771aa238fc713bc
[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=_("app-id 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 [%s]") % ' '.join(supported))
74
75     for appid, app in apps.items():
76         path = app.metadatapath
77         base, ext = common.get_extension(path)
78         if not options.to and ext not in supported:
79             logging.info(_("Ignoring %s file at '%s'") % (ext, path))
80             continue
81         elif options.to is not None:
82             logging.info(_("rewriting '%s' to %s") % (appid, options.to))
83         else:
84             logging.info(_("rewriting '%s'") % (appid))
85
86         to_ext = ext
87         if options.to is not None:
88             to_ext = options.to
89
90         if options.list:
91             if not proper_format(app):
92                 print(path)
93             continue
94
95         newbuilds = []
96         for build in app.builds:
97             new = metadata.Build()
98             for k in metadata.build_flags:
99                 v = build[k]
100                 if v is None or v is False or v == [] or v == '':
101                     continue
102                 new[k] = v
103             newbuilds.append(new)
104         app.builds = newbuilds
105
106         metadata.write_metadata(base + '.' + to_ext, app)
107
108         if ext != to_ext:
109             os.remove(path)
110
111     logging.debug(_("Finished"))
112
113
114 if __name__ == "__main__":
115     main()