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