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