chiark / gitweb /
51d82c37e21b283f89baec9291b03ee4e4e53a5a
[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 proper_format(app):
37     s = StringIO()
38     # TODO: currently reading entire file again, should reuse first
39     # read in metadata.py
40     with open(app.metadatapath, 'r') as f:
41         cur_content = f.read()
42     metadata.write_txt_metadata(s, app)
43     content = s.getvalue()
44     s.close()
45     return content == cur_content
46
47
48 def main():
49
50     global config, options
51
52     # Parse command line...
53     parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
54     common.setup_global_opts(parser)
55     parser.add_argument("-l", "--list", action="store_true", default=False,
56                         help="List files that would be reformatted")
57     parser.add_argument("-t", "--to", default=None,
58                         help="Rewrite to a specific format")
59     parser.add_argument("appid", nargs='*', help="app-id in the form APPID")
60     options = parser.parse_args()
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     supported = ['txt', 'yaml']
72
73     if options.to is not None and options.to not in supported:
74         parser.error("Must give a valid format to --to")
75
76     for appid, app in apps.iteritems():
77         base, ext = common.get_extension(app.metadatapath)
78         if not options.to and ext not in supported:
79             logging.info("Ignoring %s file at '%s'" % (ext, app.metadatapath))
80             continue
81
82         to_ext = ext
83         if options.to is not None:
84             to_ext = options.to
85
86         if options.list:
87             if not proper_format(app):
88                 print app.metadatapath
89             continue
90
91         with open(base + '.' + to_ext, 'w') as f:
92             metadata.write_metadata(to_ext, f, app)
93
94         if ext != to_ext:
95             os.remove(app.metadatapath)
96
97     logging.debug("Finished.")
98
99 if __name__ == "__main__":
100     main()