#!/usr/bin/env python import os, sys from optparse import OptionParser import logging from zeroinstall import SafeException from zeroinstall.zerostore import cli, BadDigest parser = OptionParser(usage="usage: %prog " + '\n %prog '.join([c.__doc__ for c in cli.commands])) parser.add_option("-v", "--verbose", help="more verbose output", action='count') parser.add_option("-V", "--version", help="display version information", action='store_true') parser.disable_interspersed_args() (options, args) = parser.parse_args() if options.verbose: logger = logging.getLogger() if options.verbose == 1: logger.setLevel(logging.INFO) else: logger.setLevel(logging.DEBUG) hdlr = logging.StreamHandler() fmt = logging.Formatter("%(levelname)s:%(message)s") hdlr.setFormatter(fmt) logger.addHandler(hdlr) if options.version: import zeroinstall print "0store (zero-install) " + zeroinstall.version print "Copyright (C) 2007 Thomas Leonard" print "This program comes with ABSOLUTELY NO WARRANTY," print "to the extent permitted by law." print "You may redistribute copies of this program" print "under the terms of the GNU Lesser General Public License." print "For more information about these matters, see the file named COPYING." sys.exit(0) if len(args) < 1: parser.print_help() sys.exit(1) try: cli.init_stores() pattern = args[0].lower() matches = [c for c in cli.commands if c.__name__[3:].startswith(pattern)] if len(matches) == 0: parser.print_help() sys.exit(1) if len(matches) > 1: raise SafeException("What do you mean by '%s'?\n%s" % (pattern, '\n'.join(['- ' + x.__name__[3:] for x in matches]))) matches[0](args[1:]) except KeyboardInterrupt, ex: print >>sys.stderr, "Interrupted" sys.exit(1) except OSError, ex: if options.verbose: raise print >>sys.stderr, str(ex) sys.exit(1) except IOError, ex: if options.verbose: raise print >>sys.stderr, str(ex) sys.exit(1) except cli.UsageError, ex: print >>sys.stderr, str(ex) print >>sys.stderr, "usage: " + sys.argv[0] + " " + matches[0].__doc__ sys.exit(1) except SafeException, ex: if options.verbose: raise print >>sys.stderr, str(ex) sys.exit(1)