chiark / gitweb /
Switch all headers to python3
[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 try:
24     from cStringIO import StringIO
25 except:
26     from StringIO import StringIO
27
28 import common
29 import metadata
30
31 config = None
32 options = None
33
34
35 def proper_format(app):
36     s = StringIO()
37     # TODO: currently reading entire file again, should reuse first
38     # read in metadata.py
39     with open(app.metadatapath, 'r') as f:
40         cur_content = f.read()
41     metadata.write_txt_metadata(s, app)
42     content = s.getvalue()
43     s.close()
44     return content == cur_content
45
46
47 def main():
48
49     global config, options
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")
58     parser.add_argument("appid", nargs='*', help="app-id in the form APPID")
59     options = parser.parse_args()
60
61     config = common.read_config(options)
62
63     # Get all apps...
64     allapps = metadata.read_metadata(xref=True)
65     apps = common.read_app_args(options.appid, allapps, False)
66
67     if options.list and options.to is not None:
68         parser.error("Cannot use --list and --to at the same time")
69
70     supported = ['txt', 'yaml']
71
72     if options.to is not None and options.to not in supported:
73         parser.error("Must give a valid format to --to")
74
75     for appid, app in apps.iteritems():
76         base, ext = common.get_extension(app.metadatapath)
77         if not options.to and ext not in supported:
78             logging.info("Ignoring %s file at '%s'" % (ext, app.metadatapath))
79             continue
80
81         to_ext = ext
82         if options.to is not None:
83             to_ext = options.to
84
85         if options.list:
86             if not proper_format(app):
87                 print app.metadatapath
88             continue
89
90         with open(base + '.' + to_ext, 'w') as f:
91             metadata.write_metadata(to_ext, f, app)
92
93         if ext != to_ext:
94             os.remove(app.metadatapath)
95
96     logging.debug("Finished.")
97
98 if __name__ == "__main__":
99     main()