1 """
2 The B{0launch} command-line interface.
3
4 This code is here, rather than in B{0launch} itself, simply so that it gets byte-compiled at
5 install time.
6 """
7
8 from __future__ import print_function
9
10 from zeroinstall import _
11 import os, sys
12 from optparse import OptionParser
13 import logging
14
15 from zeroinstall import SafeException, NeedDownload
16 from zeroinstall.injector.config import load_config
17 from zeroinstall.cmd import UsageError
18
19
20
21
22
23
24 -def main(command_args, config = None):
25 """Act as if 0launch was run with the given arguments.
26 @arg command_args: array of arguments (e.g. C{sys.argv[1:]})
27 @type command_args: [str]
28 """
29
30 for std in (0, 1, 2):
31 try:
32 os.fstat(std)
33 except OSError:
34 fd = os.open('/dev/null', os.O_RDONLY)
35 if fd != std:
36 os.dup2(fd, std)
37 os.close(fd)
38
39 parser = OptionParser(usage=_("usage: %prog [options] interface [args]\n"
40 " %prog --list [search-term]\n"
41 " %prog --import [signed-interface-files]\n"
42 " %prog --feed [interface]"))
43 parser.add_option("", "--before", help=_("choose a version before this"), metavar='VERSION')
44 parser.add_option("", "--command", help=_("command to select"), metavar='COMMAND')
45 parser.add_option("-c", "--console", help=_("never use GUI"), action='store_false', dest='gui')
46 parser.add_option("", "--cpu", help=_("target CPU type"), metavar='CPU')
47 parser.add_option("-d", "--download-only", help=_("fetch but don't run"), action='store_true')
48 parser.add_option("-D", "--dry-run", help=_("just print actions"), action='store_true')
49 parser.add_option("-f", "--feed", help=_("add or remove a feed"), action='store_true')
50 parser.add_option("", "--get-selections", help=_("write selected versions as XML"), action='store_true', dest='xml')
51 parser.add_option("-g", "--gui", help=_("show graphical policy editor"), action='store_true')
52 parser.add_option("-i", "--import", help=_("import from files, not from the network"), action='store_true')
53 parser.add_option("-l", "--list", help=_("list all known interfaces"), action='store_true')
54 parser.add_option("-m", "--main", help=_("name of the file to execute"))
55 parser.add_option("", "--message", help=_("message to display when interacting with user"))
56 parser.add_option("", "--not-before", help=_("minimum version to choose"), metavar='VERSION')
57 parser.add_option("", "--os", help=_("target operation system type"), metavar='OS')
58 parser.add_option("-o", "--offline", help=_("try to avoid using the network"), action='store_true')
59 parser.add_option("-r", "--refresh", help=_("refresh all used interfaces"), action='store_true')
60 parser.add_option("", "--select-only", help=_("only download the feeds"), action='store_true')
61 parser.add_option("", "--set-selections", help=_("run versions specified in XML file"), metavar='FILE')
62 parser.add_option("", "--show", help=_("show where components are installed"), action='store_true')
63 parser.add_option("-s", "--source", help=_("select source code"), action='store_true')
64 parser.add_option("-v", "--verbose", help=_("more verbose output"), action='count')
65 parser.add_option("-V", "--version", help=_("display version information"), action='store_true')
66 parser.add_option("", "--with-store", help=_("add an implementation cache"), action='append', metavar='DIR')
67 parser.add_option("-w", "--wrapper", help=_("execute program using a debugger, etc"), metavar='COMMAND')
68 parser.disable_interspersed_args()
69
70 (options, args) = parser.parse_args(command_args)
71
72 if options.verbose:
73 logger = logging.getLogger()
74 if options.verbose == 1:
75 logger.setLevel(logging.INFO)
76 else:
77 logger.setLevel(logging.DEBUG)
78 import zeroinstall
79 logging.info(_("Running 0launch %(version)s %(args)s; Python %(python_version)s"), {'version': zeroinstall.version, 'args': repr(args), 'python_version': sys.version})
80
81 if options.select_only or options.show:
82 options.download_only = True
83
84 if config is None:
85 config = load_config()
86 config.handler.dry_run = bool(options.dry_run)
87
88 if options.with_store:
89 from zeroinstall import zerostore
90 for x in options.with_store:
91 config.stores.stores.append(zerostore.Store(os.path.abspath(x)))
92 logging.info(_("Stores search path is now %s"), config.stores.stores)
93
94 if options.set_selections:
95 args = [options.set_selections] + args
96
97 try:
98 if options.list:
99 from zeroinstall.cmd import list
100 list.handle(config, options, args)
101 elif options.version:
102 import zeroinstall
103 print("0launch (zero-install) " + zeroinstall.version)
104 print("Copyright (C) 2010 Thomas Leonard")
105 print(_("This program comes with ABSOLUTELY NO WARRANTY,"
106 "\nto the extent permitted by law."
107 "\nYou may redistribute copies of this program"
108 "\nunder the terms of the GNU Lesser General Public License."
109 "\nFor more information about these matters, see the file named COPYING."))
110 elif getattr(options, 'import'):
111
112 cmd = __import__('zeroinstall.cmd.import', globals(), locals(), ["import"], 0)
113 cmd.handle(config, options, args)
114 elif options.feed:
115 from zeroinstall.cmd import add_feed
116 add_feed.handle(config, options, args, add_ok = True, remove_ok = True)
117 elif options.select_only:
118 from zeroinstall.cmd import select
119 if not options.show:
120 options.quiet = True
121 select.handle(config, options, args)
122 elif options.download_only or options.xml or options.show:
123 from zeroinstall.cmd import download
124 download.handle(config, options, args)
125 else:
126 if len(args) < 1:
127 if options.gui:
128 from zeroinstall import helpers
129 return helpers.get_selections_gui(None, [])
130 else:
131 raise UsageError()
132 else:
133 from zeroinstall.cmd import run
134 run.handle(config, options, args)
135 except NeedDownload as ex:
136
137 print(ex)
138 except KeyboardInterrupt:
139 logging.info("KeyboardInterrupt")
140 sys.exit(1)
141 except UsageError:
142 parser.print_help()
143 sys.exit(1)
144 except SafeException as ex:
145 if options.verbose: raise
146 try:
147 print(unicode(ex), file=sys.stderr)
148 except:
149 print(repr(ex), file=sys.stderr)
150 sys.exit(1)
151