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',
19 class UsageError(Exception): pass
21 def _ensure_standard_fds():
22 "Ensure stdin, stdout and stderr FDs exist, to avoid confusion."
27 fd
= os
.open('/dev/null', os
.O_RDONLY
)
32 def _no_command(command_args
):
33 """Handle --help and --version"""
34 parser
= OptionParser(usage
=_("usage: %prog COMMAND\n\nTry --help with one of these:") +
35 "\n\n0install " + '\n0install '.join(valid_commands
))
36 parser
.add_option("-V", "--version", help=_("display version information"), action
='store_true')
38 (options
, args
) = parser
.parse_args(command_args
)
41 print "0install (zero-install) " + zeroinstall
.version
42 print "Copyright (C) 2011 Thomas Leonard"
43 print _("This program comes with ABSOLUTELY NO WARRANTY,"
44 "\nto the extent permitted by law."
45 "\nYou may redistribute copies of this program"
46 "\nunder the terms of the GNU Lesser General Public License."
47 "\nFor more information about these matters, see the file named COPYING.")
52 def main(command_args
, config
= None):
53 """Act as if 0install was run with the given arguments.
54 @arg command_args: array of arguments (e.g. C{sys.argv[1:]})
55 @type command_args: [str]
57 _ensure_standard_fds()
60 from zeroinstall
.injector
.config
import load_config
61 config
= load_config()
63 # The first non-option argument is the command name (or "help" if none is found).
65 for i
, arg
in enumerate(command_args
):
66 if not arg
.startswith('-'):
76 return _no_command(command_args
)
78 if command
not in valid_commands
:
79 raise SafeException(_("Unknown sub-command '%s': try --help") % command
)
81 # Configure a parser for the given command
82 module_name
= command
.replace('-', '_')
83 cmd
= __import__('zeroinstall.cmd.' + module_name
, globals(), locals(), [module_name
], 0)
84 parser
= OptionParser(usage
=_("usage: %%prog %s [OPTIONS] %s") % (command
, cmd
.syntax
))
86 parser
.add_option("-c", "--console", help=_("never use GUI"), action
='store_false', dest
='gui')
87 parser
.add_option("", "--dry-run", help=_("just print what would be executed"), action
='store_true')
88 parser
.add_option("-g", "--gui", help=_("show graphical policy editor"), action
='store_true')
89 parser
.add_option("-v", "--verbose", help=_("more verbose output"), action
='count')
90 parser
.add_option("", "--with-store", help=_("add an implementation cache"), action
='append', metavar
='DIR')
92 cmd
.add_options(parser
)
93 (options
, args
) = parser
.parse_args(command_args
)
96 logger
= logging
.getLogger()
97 if options
.verbose
== 1:
98 logger
.setLevel(logging
.INFO
)
100 logger
.setLevel(logging
.DEBUG
)
102 logging
.info(_("Running 0install %(version)s %(args)s; Python %(python_version)s"), {'version': zeroinstall
.version
, 'args': repr(command_args
), 'python_version': sys
.version
})
104 if options
.with_store
:
105 from zeroinstall
import zerostore
106 for x
in options
.with_store
:
107 config
.stores
.stores
.append(zerostore
.Store(os
.path
.abspath(x
)))
108 logging
.info(_("Stores search path is now %s"), config
.stores
.stores
)
110 config
.handler
.dry_run
= bool(options
.dry_run
)
112 cmd
.handle(config
, options
, args
)
116 except SafeException
as ex
:
119 print >>sys
.stderr
, unicode(ex
)
121 print >>sys
.stderr
, repr(ex
)