Added --trust option (suggested by Wayne Scott).
[zeroinstall.git] / 0launch
blobf823abbe6e5f5b863aedbade8066d7cba3eeee7f
1 #!/usr/bin/env python
2 import os, sys
3 from optparse import OptionParser
5 from zeroinstall.injector import model, download, autopolicy, run
7 parser = OptionParser(usage="usage: %prog [options] interface [args]\n"
8 " %prog --list [search-term]")
9 parser.add_option("-c", "--console", help="never use GUI", action='store_false', dest='gui')
10 parser.add_option("-d", "--download-only", help="fetch but don't run", action='store_true')
11 parser.add_option("-g", "--gui", help="show graphical policy editor", action='store_true')
12 parser.add_option("-l", "--list", help="list all known interfaces", action='store_true')
13 parser.add_option("-q", "--quiet", help="suppress informational messages", action='store_true')
14 parser.add_option("-r", "--refresh", help="refresh all used interfaces", action='store_true')
15 parser.add_option("-t", "--trust", help="add a trusted key", action='append', metavar='FINGERPRINT')
16 parser.add_option("-v", "--verbose", help="more verbose output", action='store_true')
17 parser.add_option("-V", "--version", help="display version information", action='store_true')
18 parser.disable_interspersed_args()
20 (options, args) = parser.parse_args()
22 if options.trust:
23 from zeroinstall.injector import trust
24 for x in options.trust:
25 if trust.trust_db.trust_key(x) and options.verbose is True:
26 print "Marked key with fingerprint '%s' as trusted." % x
28 if options.list:
29 if len(args) == 0:
30 match = None
31 elif len(args) == 1:
32 match = args[0].lower()
33 else:
34 parser.print_help()
35 sys.exit(1)
36 from zeroinstall.injector import reader
37 for i in reader.list_all_interfaces():
38 if match and match not in i.lower(): continue
39 print i
40 sys.exit(0)
42 if options.version:
43 import zeroinstall
44 print "0launch (zero-install) " + zeroinstall.version
45 print "Copyright (C) 2005 Thomas Leonard"
46 print "This program comes with ABSOLUTELY NO WARRANTY,"
47 print "to the extent permitted by law."
48 print "You may redistribute copies of this program"
49 print "under the terms of the GNU General Public License."
50 print "For more information about these matters, see the file named COPYING."
51 sys.exit(0)
53 if len(args) < 1:
54 if options.trust:
55 sys.exit(0)
56 parser.print_help()
57 sys.exit(1)
59 if options.gui is None and os.environ.get('DISPLAY', None):
60 if options.refresh:
61 options.gui = True
62 else:
63 # Decide whether to use the GUI or not...
64 try:
65 test_policy = autopolicy.AutoPolicy(args[0], False, quiet = True)
66 test_policy.recalculate()
67 test_policy.start_downloading_impls()
68 except autopolicy.NeedDownload:
69 if not options.quiet:
70 print "Need to download; switching to GUI mode"
71 options.gui = True
73 if options.gui:
74 interface_uri = 'http://0install.net/2005/interfaces/injector-gui'
75 prog_args = args[:]
76 if options.download_only:
77 options.download_only = False
78 prog_args.insert(0, '--download-only')
79 if options.refresh:
80 options.refresh = False
81 prog_args.insert(0, '--refresh')
82 else:
83 interface_uri = args[0]
84 prog_args = args[1:]
86 # Singleton instance used everywhere...
87 policy = autopolicy.AutoPolicy(interface_uri, True,
88 quiet = bool(options.quiet),
89 verbose = bool(options.verbose),
90 download_only = bool(options.download_only))
91 policy.recalculate()
93 if options.refresh:
94 for x in policy.walk_interfaces():
95 policy.begin_iface_download(x, False)
97 # Get interfaces...
98 try:
99 policy.wait_for_downloads()
100 except model.SafeException, ex:
101 print >>sys.stderr, ex
102 sys.exit(1)
104 try:
105 policy.execute(prog_args)
106 except model.SafeException, ex:
107 if policy.network_use != model.network_full:
108 if not options.quiet:
109 print >>sys.stderr, "Error. Retrying with network use = full"
110 policy.network_use = model.network_full
111 policy.recalculate()
112 try:
113 policy.execute(prog_args)
114 except model.SafeException, ex:
115 print >>sys.stderr, ex
116 else:
117 print >>sys.stderr, ex