Added gpg unit test.
[zeroinstall.git] / 0launch
bloba33636b20313f4ac43d44d2a398dc356bd65f832
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("-v", "--verbose", help="more verbose output", action='store_true')
16 parser.add_option("-V", "--version", help="display version information", action='store_true')
17 parser.disable_interspersed_args()
19 (options, args) = parser.parse_args()
21 if options.list:
22 if len(args) == 0:
23 match = None
24 elif len(args) == 1:
25 match = args[0].lower()
26 else:
27 parser.print_help()
28 sys.exit(1)
29 from zeroinstall.injector import reader
30 for i in reader.list_all_interfaces():
31 if match and match not in i.lower(): continue
32 print i
33 sys.exit(0)
35 if options.version:
36 import zeroinstall
37 print "0launch (zero-install) " + zeroinstall.version
38 print "Copyright (C) 2005 Thomas Leonard"
39 print "This program comes with ABSOLUTELY NO WARRANTY,"
40 print "to the extent permitted by law."
41 print "You may redistribute copies of this program"
42 print "under the terms of the GNU General Public License."
43 print "For more information about these matters, see the file named COPYING."
44 sys.exit(0)
46 if len(args) < 1:
47 parser.print_help()
48 sys.exit(1)
50 if options.gui is None and os.environ.get('DISPLAY', None):
51 if options.refresh:
52 options.gui = True
53 else:
54 # Decide whether to use the GUI or not...
55 try:
56 test_policy = autopolicy.AutoPolicy(args[0], False, quiet = True)
57 test_policy.recalculate()
58 test_policy.start_downloading_impls()
59 except autopolicy.NeedDownload:
60 if not options.quiet:
61 print "Need to download; switching to GUI mode"
62 options.gui = True
64 if options.gui:
65 interface_uri = 'http://0install.net/2005/interfaces/injector-gui'
66 prog_args = args[:]
67 if options.download_only:
68 options.download_only = False
69 prog_args.insert(0, '--download-only')
70 if options.refresh:
71 options.refresh = False
72 prog_args.insert(0, '--refresh')
73 else:
74 interface_uri = args[0]
75 prog_args = args[1:]
77 # Singleton instance used everywhere...
78 policy = autopolicy.AutoPolicy(interface_uri, True,
79 quiet = bool(options.quiet),
80 verbose = bool(options.verbose),
81 download_only = bool(options.download_only))
82 policy.recalculate()
84 if options.refresh:
85 for x in policy.walk_interfaces():
86 policy.begin_iface_download(x, False)
88 # Get interfaces...
89 try:
90 policy.wait_for_downloads()
91 except model.SafeException, ex:
92 print >>sys.stderr, ex
93 sys.exit(1)
95 try:
96 policy.execute(prog_args)
97 except model.SafeException, ex:
98 if policy.network_use != model.network_full:
99 if not options.quiet:
100 print >>sys.stderr, "Error. Retrying with network use = full"
101 policy.network_use = model.network_full
102 policy.recalculate()
103 try:
104 policy.execute(prog_args)
105 except model.SafeException, ex:
106 print >>sys.stderr, ex
107 else:
108 print >>sys.stderr, ex