~ubuntu-branches/ubuntu/karmic/zeroinstall-injector/karmic

« back to all changes in this revision

Viewing changes to 0store

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Leonard
  • Date: 2007-01-23 21:50:46 UTC
  • Revision ID: james.westby@ubuntu.com-20070123215046-3ya2x81i99m5ya8r
Tags: upstream-0.25
ImportĀ upstreamĀ versionĀ 0.25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import os, sys
 
3
from optparse import OptionParser
 
4
import logging
 
5
from zeroinstall import SafeException
 
6
 
 
7
from zeroinstall.zerostore import cli, BadDigest
 
8
 
 
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()
 
14
 
 
15
(options, args) = parser.parse_args()
 
16
 
 
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)
 
27
 
 
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)
 
38
 
 
39
if len(args) < 1:
 
40
        parser.print_help()
 
41
        sys.exit(1)
 
42
 
 
43
try:
 
44
        cli.init_stores()
 
45
 
 
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)