Ran idioms filter on code
[zeroinstall.git] / 0store
blobc7bc2550692c362e4e817361c234f3ba11ada46a
1 #!/usr/bin/env python
3 from __future__ import print_function
4 import locale
5 from logging import warn
6 try:
7 locale.setlocale(locale.LC_ALL, '')
8 except locale.Error:
9 warn('Error setting locale (eg. Invalid locale)')
11 import os, sys
13 ## PATH ##
15 from optparse import OptionParser
16 import logging
17 from zeroinstall import SafeException
19 from zeroinstall.zerostore import cli, BadDigest
21 parser = OptionParser(usage="usage: %prog " +
22 '\n %prog '.join([c.__doc__ for c in cli.commands]))
23 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
24 parser.add_option("-V", "--version", help="display version information", action='store_true')
25 parser.disable_interspersed_args()
27 (options, args) = parser.parse_args()
29 if options.verbose:
30 logger = logging.getLogger()
31 if options.verbose == 1:
32 logger.setLevel(logging.INFO)
33 else:
34 logger.setLevel(logging.DEBUG)
35 hdlr = logging.StreamHandler()
36 fmt = logging.Formatter("%(levelname)s:%(message)s")
37 hdlr.setFormatter(fmt)
38 logger.addHandler(hdlr)
40 if options.version:
41 import zeroinstall
42 print("0store (zero-install) " + zeroinstall.version)
43 print("Copyright (C) 2009 Thomas Leonard")
44 print("This program comes with ABSOLUTELY NO WARRANTY,")
45 print("to the extent permitted by law.")
46 print("You may redistribute copies of this program")
47 print("under the terms of the GNU Lesser General Public License.")
48 print("For more information about these matters, see the file named COPYING.")
49 sys.exit(0)
51 if len(args) < 1:
52 parser.print_help()
53 sys.exit(1)
55 try:
56 cli.init_stores()
58 pattern = args[0].lower()
59 matches = [c for c in cli.commands if c.__name__[3:].startswith(pattern)]
60 if len(matches) == 0:
61 parser.print_help()
62 sys.exit(1)
63 if len(matches) > 1:
64 raise SafeException("What do you mean by '%s'?\n%s" %
65 (pattern, '\n'.join(['- ' + x.__name__[3:] for x in matches])))
66 matches[0](args[1:])
67 except KeyboardInterrupt as ex:
68 print("Interrupted", file=sys.stderr)
69 sys.exit(1)
70 except OSError as ex:
71 if options.verbose: raise
72 print(str(ex), file=sys.stderr)
73 sys.exit(1)
74 except IOError as ex:
75 if options.verbose: raise
76 print(str(ex), file=sys.stderr)
77 sys.exit(1)
78 except cli.UsageError as ex:
79 print(str(ex), file=sys.stderr)
80 print("usage: " + sys.argv[0] + " " + matches[0].__doc__, file=sys.stderr)
81 sys.exit(1)
82 except SafeException as ex:
83 if options.verbose: raise
84 print(str(ex), file=sys.stderr)
85 sys.exit(1)