chiark / gitweb /
Apply some autopep8-python2 suggestions
[fdroidserver.git] / fdroid
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # fdroid.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
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 import sys
22 import logging
23
24 from fdroidserver.common import FDroidException
25 from optparse import OptionError
26
27 commands = {
28     "build": "Build a package from source",
29     "init": "Quickly start a new repository",
30     "publish": "Sign and place packages in the repo",
31     "gpgsign": "Add gpg signatures for packages in repo",
32     "update": "Update repo information for new packages",
33     "verify": "Verify the integrity of downloaded packages",
34     "checkupdates": "Check for updates to applications",
35     "import": "Add a new application from its source code",
36     "install": "Install built packages on devices",
37     "readmeta": "Read all the metadata files and exit",
38     "rewritemeta": "Rewrite all the metadata files",
39     "lint": "Warn about possible metadata errors",
40     "scanner": "Scan the source code of a package",
41     "stats": "Update the stats of the repo",
42     "server": "Interact with the repo HTTP server",
43 }
44
45
46 def print_help():
47     print "usage: fdroid [-h|--help] <command> [<args>]"
48     print
49     print "Valid commands are:"
50     for cmd, summary in commands.items():
51         print "   " + cmd + ' ' * (15 - len(cmd)) + summary
52     print
53
54
55 def main():
56
57     if len(sys.argv) <= 1:
58         print_help()
59         sys.exit(0)
60
61     command = sys.argv[1]
62     if command not in commands:
63         if command in ('-h', '--help'):
64             print_help()
65             sys.exit(0)
66         else:
67             print "Command '%s' not recognised.\n" % command
68             print_help()
69             sys.exit(1)
70
71     verbose = any(s in sys.argv for s in ['-v', '--verbose'])
72     quiet = any(s in sys.argv for s in ['-q', '--quiet'])
73
74     if verbose:
75         logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
76     elif quiet:
77         logging.basicConfig(format='%(message)s', level=logging.WARN)
78     else:
79         logging.basicConfig(format='%(message)s', level=logging.INFO)
80
81     if verbose and quiet:
82         logging.critical("Specifying --verbose and --quiet and the same time is silly")
83         sys.exit(1)
84
85     # Trick optparse into displaying the right usage when --help is used.
86     sys.argv[0] += ' ' + command
87
88     del sys.argv[1]
89     mod = __import__('fdroidserver.' + command, None, None, [command])
90
91     try:
92         mod.main()
93     # These are ours, contain a proper message and are "expected"
94     except FDroidException, e:
95         if verbose:
96             raise
97         else:
98             logging.critical(str(e))
99         sys.exit(1)
100     except OptionError, e:
101         logging.critical(str(e))
102         sys.exit(1)
103     except KeyboardInterrupt:
104         print('')
105         sys.exit(1)
106     # These should only be unexpected crashes due to bugs in the code
107     # str(e) often doesn't contain a reason, so just show the backtrace
108     except Exception, e:
109         logging.critical("Unknown exception found!")
110         raise
111     sys.exit(0)
112
113 if __name__ == "__main__":
114     main()