Made a proper Config object
[zeroinstall/zeroinstall-afb.git] / zeroinstall / injector / cli.py
blobba573b08baf6fb9093038c71507aa96ede475a1d
1 """
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
5 install time.
6 """
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, policy, autopolicy, selections
15 from zeroinstall.cmd import UsageError
17 #def program_log(msg): os.access('MARK: 0launch: ' + msg, os.F_OK)
18 #import __main__
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]
26 """
27 # Ensure stdin, stdout and stderr FDs exist, to avoid confusion
28 for std in (0, 1, 2):
29 try:
30 os.fstat(std)
31 except OSError:
32 fd = os.open('/dev/null', os.O_RDONLY)
33 if fd != std:
34 os.dup2(fd, std)
35 os.close(fd)
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)
71 if options.verbose:
72 logger = logging.getLogger()
73 if options.verbose == 1:
74 logger.setLevel(logging.INFO)
75 else:
76 logger.setLevel(logging.DEBUG)
77 import zeroinstall
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
94 try:
95 if options.list:
96 from zeroinstall.cmd import list
97 list.handle(options, args)
98 elif options.version:
99 import zeroinstall
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)
111 elif options.feed:
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
116 if not options.show:
117 options.quiet = True
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)
122 else:
123 if len(args) < 1:
124 if options.gui:
125 from zeroinstall import helpers
126 return helpers.get_selections_gui(None, [])
127 else:
128 raise UsageError()
129 else:
130 from zeroinstall.cmd import run
131 run.handle(options, args)
132 except NeedDownload, ex:
133 # This only happens for dry runs
134 print ex
135 except UsageError:
136 parser.print_help()
137 sys.exit(1)
138 except SafeException, ex:
139 if options.verbose: raise
140 try:
141 print >>sys.stderr, unicode(ex)
142 except:
143 print >>sys.stderr, repr(ex)
144 sys.exit(1)