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