chiark / gitweb /
Make text formatting faster via StringIO
[fdroidserver.git] / fdroidserver / rewritemeta.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # rewritemeta.py - part of the FDroid server tools
5 # This cleans up the original .txt metadata file format.
6 # Copyright (C) 2010-12, Ciaran Gultnieks, ciaran@ciarang.com
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Affero General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 from argparse import ArgumentParser
22 import os
23 import logging
24 try:
25     from cStringIO import StringIO
26 except:
27     from StringIO import StringIO
28
29 import common
30 import metadata
31
32 config = None
33 options = None
34
35
36 def main():
37
38     global config, options
39
40     # Parse command line...
41     parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
42     common.setup_global_opts(parser)
43     parser.add_argument("-l", "--list", action="store_true", default=False,
44                         help="List files that would be reformatted")
45     parser.add_argument("-t", "--to", default=None,
46                         help="Rewrite to a specific format")
47     parser.add_argument("appid", nargs='*', help="app-id in the form APPID")
48     options = parser.parse_args()
49
50     config = common.read_config(options)
51
52     # Get all apps...
53     allapps = metadata.read_metadata(xref=True)
54     apps = common.read_app_args(options.appid, allapps, False)
55
56     if options.list and options.to is not None:
57         parser.error("Cannot use --list and --to at the same time")
58
59     supported = ['txt', 'yaml']
60
61     if options.to is not None and options.to not in supported:
62         parser.error("Must give a valid format to --to")
63
64     for appid, app in apps.iteritems():
65         metadatapath = app.metadatapath
66         base, ext = common.get_extension(metadatapath)
67         if not options.to and ext not in supported:
68             logging.info("Ignoring %s file at '%s'" % (ext, metadatapath))
69             continue
70
71         to_ext = ext
72         if options.to is not None:
73             to_ext = options.to
74
75         if options.list:
76             s = StringIO()
77             # TODO: currently reading entire file again, should reuse first
78             # read in metadata.py
79             with open(metadatapath, 'r') as f:
80                 cur_content = f.read()
81             metadata.write_txt_metadata(s, app)
82             content = s.getvalue()
83             s.close()
84             if content != cur_content:
85                 print(metadatapath)
86             continue
87
88         with open(base + '.' + to_ext, 'w') as f:
89             metadata.write_metadata(to_ext, f, app)
90
91         if ext != to_ext:
92             os.remove(metadatapath)
93
94     logging.debug("Finished.")
95
96 if __name__ == "__main__":
97     main()