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