chiark / gitweb /
Add basic yaml metadata writing
[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 sys
23 import logging
24 import StringIO
25
26 import common
27 import metadata
28
29 config = None
30 options = None
31
32
33 def main():
34
35     global config, options
36
37     # Parse command line...
38     parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
39     common.setup_global_opts(parser)
40     parser.add_argument("-l", "--list", action="store_true", default=False,
41                         help="List files that would be reformatted")
42     parser.add_argument("-t", "--to", default='txt',
43                         help="Rewrite to a specific format")
44     parser.add_argument("appid", nargs='*', help="app-id in the form APPID")
45     options = parser.parse_args()
46
47     config = common.read_config(options)
48
49     # Get all apps...
50     allapps = metadata.read_metadata(xref=True)
51     apps = common.read_app_args(options.appid, allapps, False)
52
53     if options.list and options.to:
54         parser.error("Cannot use --list and --to at the same time")
55
56     for appid, app in apps.iteritems():
57         metadatapath = app['metadatapath']
58         base, ext = common.get_extension(metadatapath)
59         if not options.to and ext not in ['txt', 'yaml']:
60             logging.info("Ignoring %s file at '%s'" % (ext, metadatapath))
61             continue
62         logging.debug("Rewriting " + metadatapath)
63         if options.list:
64             s = StringIO.StringIO()
65             # TODO: currently reading entire file again, should reuse first
66             # read in metadata.py
67             with open(metadatapath, 'r') as f:
68                 cur_content = f.read()
69             metadata.write_txt_metadata(s, app)
70             content = s.getvalue()
71             s.close()
72             if content != cur_content:
73                 print(metadatapath)
74             continue
75
76         if options.to == 'txt':
77             with open(base+'.txt', 'w') as f:
78                 metadata.write_txt_metadata(f, app)
79         elif options.to == 'yaml':
80             # with open(base+'.yaml', 'w') as f:
81                 # metadata.write_yaml_metadata(f, app)
82             metadata.write_yaml_metadata(sys.stdout, app)
83             break
84
85     logging.debug("Finished.")
86
87 if __name__ == "__main__":
88     main()