2 The B{0launch} command-line interface.
4 This code is here, rather than in B{0launch} itself, simply so that it gets byte-compiled at
8 from zeroinstall
import _
10 from optparse
import OptionParser
13 from zeroinstall
import SafeException
, NeedDownload
14 from zeroinstall
.injector
import model
, policy
, autopolicy
, selections
15 from zeroinstall
.cmd
import UsageError
17 #def program_log(msg): os.access('MARK: 0launch: ' + msg, os.F_OK)
19 #__main__.__builtins__.program_log = program_log
20 #program_log('0launch ' + ' '.join((sys.argv[1:])))
22 def main(command_args
):
23 """Act as if 0launch was run with the given arguments.
24 @arg command_args: array of arguments (e.g. C{sys.argv[1:]})
25 @type command_args: [str]
27 # Ensure stdin, stdout and stderr FDs exist, to avoid confusion
32 fd
= os
.open('/dev/null', os
.O_RDONLY
)
37 parser
= OptionParser(usage
=_("usage: %prog [options] interface [args]\n"
38 " %prog --list [search-term]\n"
39 " %prog --import [signed-interface-files]\n"
40 " %prog --feed [interface]"))
41 parser
.add_option("", "--before", help=_("choose a version before this"), metavar
='VERSION')
42 parser
.add_option("", "--command", help=_("command to select"), metavar
='COMMAND')
43 parser
.add_option("-c", "--console", help=_("never use GUI"), action
='store_false', dest
='gui')
44 parser
.add_option("", "--cpu", help=_("target CPU type"), metavar
='CPU')
45 parser
.add_option("-d", "--download-only", help=_("fetch but don't run"), action
='store_true')
46 parser
.add_option("-D", "--dry-run", help=_("just print actions"), action
='store_true')
47 parser
.add_option("-f", "--feed", help=_("add or remove a feed"), action
='store_true')
48 parser
.add_option("", "--get-selections", help=_("write selected versions as XML"), action
='store_true', dest
='xml')
49 parser
.add_option("-g", "--gui", help=_("show graphical policy editor"), action
='store_true')
50 parser
.add_option("-i", "--import", help=_("import from files, not from the network"), action
='store_true')
51 parser
.add_option("-l", "--list", help=_("list all known interfaces"), action
='store_true')
52 parser
.add_option("-m", "--main", help=_("name of the file to execute"))
53 parser
.add_option("", "--message", help=_("message to display when interacting with user"))
54 parser
.add_option("", "--not-before", help=_("minimum version to choose"), metavar
='VERSION')
55 parser
.add_option("", "--os", help=_("target operation system type"), metavar
='OS')
56 parser
.add_option("-o", "--offline", help=_("try to avoid using the network"), action
='store_true')
57 parser
.add_option("-r", "--refresh", help=_("refresh all used interfaces"), action
='store_true')
58 parser
.add_option("", "--select-only", help=_("only download the feeds"), action
='store_true')
59 parser
.add_option("", "--set-selections", help=_("run versions specified in XML file"), metavar
='FILE')
60 parser
.add_option("", "--show", help=_("show where components are installed"), action
='store_true')
61 parser
.add_option("-s", "--source", help=_("select source code"), action
='store_true')
62 parser
.add_option("", "--systray", help=_("download in the background"), action
='store_true')
63 parser
.add_option("-v", "--verbose", help=_("more verbose output"), action
='count')
64 parser
.add_option("-V", "--version", help=_("display version information"), action
='store_true')
65 parser
.add_option("", "--with-store", help=_("add an implementation cache"), action
='append', metavar
='DIR')
66 parser
.add_option("-w", "--wrapper", help=_("execute program using a debugger, etc"), metavar
='COMMAND')
67 parser
.disable_interspersed_args()
69 (options
, args
) = parser
.parse_args(command_args
)
72 logger
= logging
.getLogger()
73 if options
.verbose
== 1:
74 logger
.setLevel(logging
.INFO
)
76 logger
.setLevel(logging
.DEBUG
)
78 logging
.info(_("Running 0launch %(version)s %(args)s; Python %(python_version)s"), {'version': zeroinstall
.version
, 'args': repr(args
), 'python_version': sys
.version
})
80 if options
.select_only
or options
.show
:
81 options
.download_only
= True
83 config
= policy
.load_config()
85 if options
.with_store
:
86 from zeroinstall
import zerostore
87 for x
in options
.with_store
:
88 config
.stores
.stores
.append(zerostore
.Store(os
.path
.abspath(x
)))
89 logging
.info(_("Stores search path is now %s"), config
.stores
.stores
)
91 if options
.set_selections
:
92 args
= [options
.set_selections
] + args
96 from zeroinstall
.cmd
import list
97 list.handle(options
, args
)
100 print "0launch (zero-install) " + zeroinstall
.version
101 print "Copyright (C) 2010 Thomas Leonard"
102 print _("This program comes with ABSOLUTELY NO WARRANTY,"
103 "\nto the extent permitted by law."
104 "\nYou may redistribute copies of this program"
105 "\nunder the terms of the GNU Lesser General Public License."
106 "\nFor more information about these matters, see the file named COPYING.")
107 elif getattr(options
, 'import'):
108 # (import is a keyword)
109 cmd
= __import__('zeroinstall.cmd.import', globals(), locals(), ["import"], 0)
110 cmd
.handle(options
, args
)
112 from zeroinstall
.cmd
import add_feed
113 add_feed
.handle(options
, args
, add_ok
= True, remove_ok
= True)
114 elif options
.select_only
:
115 from zeroinstall
.cmd
import select
118 select
.handle(options
, args
)
119 elif options
.download_only
or options
.xml
or options
.show
:
120 from zeroinstall
.cmd
import download
121 download
.handle(options
, args
)
125 from zeroinstall
import helpers
126 return helpers
.get_selections_gui(None, [])
130 from zeroinstall
.cmd
import run
131 run
.handle(options
, args
)
132 except NeedDownload
, ex
:
133 # This only happens for dry runs
138 except SafeException
, ex
:
139 if options
.verbose
: raise
141 print >>sys
.stderr
, unicode(ex
)
143 print >>sys
.stderr
, repr(ex
)