chiark / gitweb /
Merge branch 'master' 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 _
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     _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()
45     s.close()
46     return content == cur_content
47
48
49 def main():
50
51     global config, options
52
53     supported = ['txt', 'yml']
54
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
66
67     config = common.read_config(options)
68
69     # Get all apps...
70     allapps = metadata.read_metadata(xref=True)
71     apps = common.read_app_args(options.appid, allapps, False)
72
73     if options.list and options.to is not None:
74         parser.error(_("Cannot use --list and --to at the same time"))
75
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)))
79
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))
85             continue
86         elif options.to is not None:
87             logging.info(_("Rewriting '{appid}' to '{path}'").format(appid=appid, path=options.to))
88         else:
89             logging.info(_("Rewriting '{appid}'").format(appid=appid))
90
91         to_ext = ext
92         if options.to is not None:
93             to_ext = options.to
94
95         if options.list:
96             if not proper_format(app):
97                 print(path)
98             continue
99
100         newbuilds = []
101         for build in app.builds:
102             new = metadata.Build()
103             for k in metadata.build_flags:
104                 v = build[k]
105                 if v is None or v is False or v == [] or v == '':
106                     continue
107                 new[k] = v
108             newbuilds.append(new)
109         app.builds = newbuilds
110
111         metadata.write_metadata(base + '.' + to_ext, app)
112
113         if ext != to_ext:
114             os.remove(path)
115
116     logging.debug(_("Finished"))
117
118
119 if __name__ == "__main__":
120     main()