Removed local_interfaces configuration directory. Instead, the user_overrides files...
[zeroinstall.git] / 0launch
blobe69bdb4de4f44a18028445fcdb1fd416f81ab738
1 #!/usr/bin/env python
2 import os, sys
3 from optparse import OptionParser
4 import logging
6 from zeroinstall.injector import model, download, autopolicy, run, namespaces
8 parser = OptionParser(usage="usage: %prog [options] interface [args]\n"
9 " %prog --list [search-term]\n"
10 " %prog --import [interface-files]")
11 parser.add_option("-c", "--console", help="never use GUI", action='store_false', dest='gui')
12 parser.add_option("-d", "--download-only", help="fetch but don't run", action='store_true')
13 parser.add_option("-D", "--dry-run", help="just print actions", action='store_true')
14 parser.add_option("-g", "--gui", help="show graphical policy editor", action='store_true')
15 parser.add_option("-i", "--import", help="import from files, not from the network", action='store_true')
16 parser.add_option("-l", "--list", help="list all known interfaces", action='store_true')
17 parser.add_option("-r", "--refresh", help="refresh all used interfaces", action='store_true')
18 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
19 parser.add_option("-V", "--version", help="display version information", action='store_true')
20 parser.disable_interspersed_args()
22 (options, args) = parser.parse_args()
24 if options.verbose:
25 logger = logging.getLogger()
26 if options.verbose == 1:
27 logger.setLevel(logging.INFO)
28 else:
29 logger.setLevel(logging.DEBUG)
31 if options.list:
32 if len(args) == 0:
33 match = None
34 elif len(args) == 1:
35 match = args[0].lower()
36 else:
37 parser.print_help()
38 sys.exit(1)
39 from zeroinstall.injector.iface_cache import iface_cache
40 for i in iface_cache.list_all_interfaces():
41 if match and match not in i.lower(): continue
42 print i
43 sys.exit(0)
45 if options.version:
46 import zeroinstall
47 print "0launch (zero-install) " + zeroinstall.version
48 print "Copyright (C) 2005 Thomas Leonard"
49 print "This program comes with ABSOLUTELY NO WARRANTY,"
50 print "to the extent permitted by law."
51 print "You may redistribute copies of this program"
52 print "under the terms of the GNU General Public License."
53 print "For more information about these matters, see the file named COPYING."
54 sys.exit(0)
56 if len(args) < 1:
57 if options.gui:
58 args = [namespaces.injector_gui_uri]
59 options.download_only = True
60 else:
61 parser.print_help()
62 sys.exit(1)
64 try:
65 if getattr(options, 'import'):
66 from zeroinstall.injector import gpg, handler
67 from zeroinstall.injector.iface_cache import iface_cache
68 from xml.dom import minidom
69 for x in args:
70 if not os.path.isfile(x):
71 raise model.SafeException("File '%s' does not exist" % x)
72 logging.info("Importing from file '%s'", x)
73 signed_data = file(x)
74 data, sigs = gpg.check_stream(signed_data)
75 doc = minidom.parseString(data.read())
76 uri = doc.documentElement.getAttribute('uri')
77 assert uri
78 new, iface = iface_cache.get_interface(uri)
79 logging.info("Importing information about interface %s", iface)
80 signed_data.seek(0)
81 iface_cache.check_signed_data(iface, signed_data, handler.Handler())
82 sys.exit(0)
84 if args[0].startswith('http:'):
85 iface_uri = args[0]
86 else:
87 iface_uri = os.path.realpath(args[0])
88 if not os.path.isfile(iface_uri):
89 raise model.SafeException("Bad interface name '%s'.\n"
90 "(doesn't start with 'http:', and "
91 "doesn't exist as a local file either)" % args[0])
93 # Singleton instance used everywhere...
94 policy = autopolicy.AutoPolicy(iface_uri,
95 download_only = bool(options.download_only),
96 dry_run = options.dry_run)
98 if options.gui is None and os.environ.get('DISPLAY', None):
99 if options.refresh:
100 options.gui = True
101 else:
102 options.gui = policy.need_download()
103 if options.gui:
104 # If we need to download anything, we might as well
105 # refresh all the interfaces first. Also, this triggers
106 # the 'checking for updates' box, which is non-interactive
107 # when there are no changes to the selection.
108 options.refresh = True
109 logging.info("Need to download; switching to GUI mode")
110 except model.SafeException, ex:
111 print >>sys.stderr, ex
112 sys.exit(1)
114 if options.gui:
115 policy.set_root(namespaces.injector_gui_uri)
117 # Try to start the GUI without using the network.
118 # The GUI can refresh itself if it wants to.
119 policy.freshness = 0
120 policy.network_use = model.network_minimal
122 prog_args = [iface_uri] + args[1:]
123 # Options apply to actual program, not GUI
124 if options.download_only:
125 policy.download_only = False
126 prog_args.insert(0, '--download-only')
127 if options.refresh:
128 options.refresh = False
129 prog_args.insert(0, '--refresh')
130 else:
131 prog_args = args[1:]
133 try:
134 policy.download_and_execute(prog_args, refresh = bool(options.refresh))
135 except model.SafeException, ex:
136 print >>sys.stderr, ex
137 sys.exit(1)
138 except autopolicy.NeedDownload, ex:
139 print ex
140 sys.exit(0)