2 The B{0install} command-line interface.
5 # Copyright (C) 2011, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from zeroinstall
import _
10 from optparse
import OptionParser
13 from zeroinstall
import SafeException
15 valid_commands
= ['select', 'download', 'run', 'update',
16 'config', 'import', 'list', 'add-feed', 'remove-feed', 'list-feeds']
18 class UsageError(Exception): pass
20 def _ensure_standard_fds():
21 "Ensure stdin, stdout and stderr FDs exist, to avoid confusion."
26 fd
= os
.open('/dev/null', os
.O_RDONLY
)
31 def _no_command(command_args
):
32 """Handle --help and --version"""
33 parser
= OptionParser(usage
=_("usage: %prog COMMAND\n\nTry --help with one of these:") +
34 "\n\n0install " + '\n0install '.join(valid_commands
))
35 parser
.add_option("-V", "--version", help=_("display version information"), action
='store_true')
37 (options
, args
) = parser
.parse_args(command_args
)
40 print "0install (zero-install) " + zeroinstall
.version
41 print "Copyright (C) 2011 Thomas Leonard"
42 print _("This program comes with ABSOLUTELY NO WARRANTY,"
43 "\nto the extent permitted by law."
44 "\nYou may redistribute copies of this program"
45 "\nunder the terms of the GNU Lesser General Public License."
46 "\nFor more information about these matters, see the file named COPYING.")
51 def main(command_args
, config
= None):
52 """Act as if 0install was run with the given arguments.
53 @arg command_args: array of arguments (e.g. C{sys.argv[1:]})
54 @type command_args: [str]
56 _ensure_standard_fds()
59 from zeroinstall
.injector
import policy
, handler
61 h
= handler
.ConsoleHandler()
64 config
= policy
.load_config(h
)
66 # The first non-option argument is the command name (or "help" if none is found).
68 for i
, arg
in enumerate(command_args
):
69 if not arg
.startswith('-'):
79 return _no_command(command_args
)
81 if command
not in valid_commands
:
82 raise SafeException(_("Unknown sub-command '%s': try --help") % command
)
84 # Configure a parser for the given command
85 module_name
= command
.replace('-', '_')
86 cmd
= __import__('zeroinstall.cmd.' + module_name
, globals(), locals(), [module_name
], 0)
87 parser
= OptionParser(usage
=_("usage: %%prog %s [OPTIONS] %s") % (command
, cmd
.syntax
))
89 parser
.add_option("-c", "--console", help=_("never use GUI"), action
='store_false', dest
='gui')
90 parser
.add_option("", "--dry-run", help=_("just print what would be executed"), action
='store_true')
91 parser
.add_option("-g", "--gui", help=_("show graphical policy editor"), action
='store_true')
92 parser
.add_option("-v", "--verbose", help=_("more verbose output"), action
='count')
93 parser
.add_option("", "--with-store", help=_("add an implementation cache"), action
='append', metavar
='DIR')
95 cmd
.add_options(parser
)
96 (options
, args
) = parser
.parse_args(command_args
)
99 logger
= logging
.getLogger()
100 if options
.verbose
== 1:
101 logger
.setLevel(logging
.INFO
)
103 logger
.setLevel(logging
.DEBUG
)
105 logging
.info(_("Running 0install %(version)s %(args)s; Python %(python_version)s"), {'version': zeroinstall
.version
, 'args': repr(command_args
), 'python_version': sys
.version
})
107 if options
.with_store
:
108 from zeroinstall
import zerostore
109 for x
in options
.with_store
:
110 config
.stores
.stores
.append(zerostore
.Store(os
.path
.abspath(x
)))
111 logging
.info(_("Stores search path is now %s"), config
.stores
.stores
)
113 config
.handler
.dry_run
= bool(options
.dry_run
)
115 cmd
.handle(config
, options
, args
)
119 except SafeException
, ex
:
122 print >>sys
.stderr
, unicode(ex
)
124 print >>sys
.stderr
, repr(ex
)