Added '0install config' sub-command
[zeroinstall.git] / 0store
blobf53016b90a209b1c8e5032cc52d4a953a324a50a
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
12 ## PATH ##
14 from optparse import OptionParser
15 import logging
16 from zeroinstall import SafeException
18 from zeroinstall.zerostore import cli, BadDigest
20 parser = OptionParser(usage="usage: %prog " +
21 '\n %prog '.join([c.__doc__ for c in cli.commands]))
22 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
23 parser.add_option("-V", "--version", help="display version information", action='store_true')
24 parser.disable_interspersed_args()
26 (options, args) = parser.parse_args()
28 if options.verbose:
29 logger = logging.getLogger()
30 if options.verbose == 1:
31 logger.setLevel(logging.INFO)
32 else:
33 logger.setLevel(logging.DEBUG)
34 hdlr = logging.StreamHandler()
35 fmt = logging.Formatter("%(levelname)s:%(message)s")
36 hdlr.setFormatter(fmt)
37 logger.addHandler(hdlr)
39 if options.version:
40 import zeroinstall
41 print "0store (zero-install) " + zeroinstall.version
42 print "Copyright (C) 2009 Thomas Leonard"
43 print "This program comes with ABSOLUTELY NO WARRANTY,"
44 print "to the extent permitted by law."
45 print "You may redistribute copies of this program"
46 print "under the terms of the GNU Lesser General Public License."
47 print "For more information about these matters, see the file named COPYING."
48 sys.exit(0)
50 if len(args) < 1:
51 parser.print_help()
52 sys.exit(1)
54 try:
55 cli.init_stores()
57 pattern = args[0].lower()
58 matches = [c for c in cli.commands if c.__name__[3:].startswith(pattern)]
59 if len(matches) == 0:
60 parser.print_help()
61 sys.exit(1)
62 if len(matches) > 1:
63 raise SafeException("What do you mean by '%s'?\n%s" %
64 (pattern, '\n'.join(['- ' + x.__name__[3:] for x in matches])))
65 matches[0](args[1:])
66 except KeyboardInterrupt, ex:
67 print >>sys.stderr, "Interrupted"
68 sys.exit(1)
69 except OSError, ex:
70 if options.verbose: raise
71 print >>sys.stderr, str(ex)
72 sys.exit(1)
73 except IOError, ex:
74 if options.verbose: raise
75 print >>sys.stderr, str(ex)
76 sys.exit(1)
77 except cli.UsageError, ex:
78 print >>sys.stderr, str(ex)
79 print >>sys.stderr, "usage: " + sys.argv[0] + " " + matches[0].__doc__
80 sys.exit(1)
81 except SafeException, ex:
82 if options.verbose: raise
83 print >>sys.stderr, str(ex)
84 sys.exit(1)