Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 87 lines (73 sloc) 2.36 KB
#!/usr/bin/env python
from __future__ import print_function
import locale
from logging import warn
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error:
warn('Error setting locale (eg. Invalid locale)')
import os, sys
## PATH ##
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) 2009 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 as ex:
print("Interrupted", file=sys.stderr)
sys.exit(1)
except OSError as ex:
if options.verbose: raise
print(str(ex), file=sys.stderr)
sys.exit(1)
except IOError as ex:
if options.verbose: raise
print(str(ex), file=sys.stderr)
sys.exit(1)
except cli.UsageError as ex:
print(str(ex), file=sys.stderr)
print("usage: " + sys.argv[0] + " " + matches[0].__doc__, file=sys.stderr)
sys.exit(1)
except SafeException as ex:
if options.verbose: raise
print(str(ex), file=sys.stderr)
sys.exit(1)