Added '0install add-feed' and '0install remove-feed' sub-commands
[zeroinstall.git] / zeroinstall / cmd / __init__.py
blob0cb78ffc9a1913fefbfab5bdb399dc892cd46b0c
1 """
2 The B{0install} command-line interface.
3 """
5 # Copyright (C) 2011, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from zeroinstall import _
9 import os, sys
10 from optparse import OptionParser
11 import logging
13 from zeroinstall import SafeException, NeedDownload
14 from zeroinstall.injector import model, autopolicy, selections
15 from zeroinstall.injector.iface_cache import iface_cache
17 valid_commands = ['select', 'download', 'run', 'update',
18 'config', 'import', 'list', 'add-feed', 'remove-feed']
20 class UsageError(Exception): pass
22 def _ensure_standard_fds():
23 "Ensure stdin, stdout and stderr FDs exist, to avoid confusion."
24 for std in (0, 1, 2):
25 try:
26 os.fstat(std)
27 except OSError:
28 fd = os.open('/dev/null', os.O_RDONLY)
29 if fd != std:
30 os.dup2(fd, std)
31 os.close(fd)
33 def _no_command(command_args):
34 """Handle --help and --version"""
35 parser = OptionParser(usage=_("usage: %prog COMMAND\n\nTry --help with one of these:") +
36 "\n\n0install " + '\n0install '.join(valid_commands))
37 parser.add_option("-V", "--version", help=_("display version information"), action='store_true')
39 (options, args) = parser.parse_args(command_args)
40 if options.version:
41 import zeroinstall
42 print "0install (zero-install) " + zeroinstall.version
43 print "Copyright (C) 2011 Thomas Leonard"
44 print _("This program comes with ABSOLUTELY NO WARRANTY,"
45 "\nto the extent permitted by law."
46 "\nYou may redistribute copies of this program"
47 "\nunder the terms of the GNU Lesser General Public License."
48 "\nFor more information about these matters, see the file named COPYING.")
49 sys.exit(0)
50 parser.print_help()
51 sys.exit(2)
53 def main(command_args):
54 """Act as if 0install was run with the given arguments.
55 @arg command_args: array of arguments (e.g. C{sys.argv[1:]})
56 @type command_args: [str]
57 """
58 _ensure_standard_fds()
60 # The first non-option argument is the command name (or "help" if none is found).
61 command = None
62 for arg in command_args:
63 if not arg.startswith('-'):
64 command = arg
65 break
66 elif arg == '--':
67 break
69 verbose = False
70 try:
71 if command is None:
72 return _no_command(command_args)
74 if command not in valid_commands:
75 raise SafeException(_("Unknown sub-command '%s': try --help") % command)
77 # Configure a parser for the given command
78 module_name = command.replace('-', '_')
79 cmd = __import__('zeroinstall.cmd.' + module_name, globals(), locals(), [module_name], 0)
80 parser = OptionParser(usage=_("usage: %%prog %s [OPTIONS] %s") % (command, cmd.syntax))
82 parser.add_option("-c", "--console", help=_("never use GUI"), action='store_false', dest='gui')
83 parser.add_option("", "--dry-run", help=_("just print what would be executed"), action='store_true')
84 parser.add_option("-g", "--gui", help=_("show graphical policy editor"), action='store_true')
85 parser.add_option("-v", "--verbose", help=_("more verbose output"), action='count')
86 parser.add_option("", "--with-store", help=_("add an implementation cache"), action='append', metavar='DIR')
88 cmd.add_options(parser)
89 (options, args) = parser.parse_args(command_args)
90 assert args[0] == command
92 if options.verbose:
93 logger = logging.getLogger()
94 if options.verbose == 1:
95 logger.setLevel(logging.INFO)
96 else:
97 logger.setLevel(logging.DEBUG)
98 import zeroinstall
99 logging.info(_("Running 0install %(version)s %(args)s; Python %(python_version)s"), {'version': zeroinstall.version, 'args': repr(command_args), 'python_version': sys.version})
101 args = args[1:]
103 if options.with_store:
104 from zeroinstall import zerostore
105 for x in options.with_store:
106 iface_cache.stores.stores.append(zerostore.Store(os.path.abspath(x)))
107 logging.info(_("Stores search path is now %s"), iface_cache.stores.stores)
109 cmd.handle(options, args)
110 except UsageError:
111 parser.print_help()
112 sys.exit(1)
113 except SafeException, ex:
114 if verbose: raise
115 try:
116 print >>sys.stderr, unicode(ex)
117 except:
118 print >>sys.stderr, repr(ex)
119 sys.exit(1)
120 return