Moved env init into setUp, so it doesn't affect other tests.
[zeroinstall.git] / 0store
blobc40c8932fb6161dddf0809c8ec2142c72043a171
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) 2005 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 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 print >>sys.stderr, str(ex)
60 sys.exit(1)
61 except IOError, ex:
62 print >>sys.stderr, str(ex)
63 sys.exit(1)
64 except cli.UsageError, ex:
65 print >>sys.stderr, str(ex)
66 print >>sys.stderr, "usage: " + sys.argv[0] + " " + matches[0].__doc__
67 sys.exit(1)
68 except SafeException, ex:
69 print >>sys.stderr, str(ex)
70 sys.exit(1)