Moved AutoPolicy from launcher script to its own module.
[zeroinstall.git] / 0launch
blob51b036923aa6a841326f50b0a5965ea2c9be38c9
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("-g", "--gui", help="show graphical policy editor", action='store_true')
10 parser.add_option("-l", "--list", help="list all known interfaces", action='store_true')
11 parser.add_option("-r", "--refresh", help="refresh all used interfaces", action='store_true')
12 parser.add_option("-v", "--version", help="display version information", action='store_true')
13 parser.disable_interspersed_args()
15 (options, args) = parser.parse_args()
17 if options.list:
18 if len(args) == 0:
19 match = None
20 elif len(args) == 1:
21 match = args[0].lower()
22 else:
23 parser.print_help()
24 sys.exit(1)
25 from zeroinstall.injector import reader
26 for i in reader.list_all_interfaces():
27 if match and match not in i.lower(): continue
28 print i
29 sys.exit(0)
31 if options.version:
32 import zeroinstall
33 print "0launch (zero-install) " + zeroinstall.version
34 print "Copyright (C) 2005 Thomas Leonard"
35 print "This program comes with ABSOLUTELY NO WARRANTY,"
36 print "to the extent permitted by law."
37 print "You may redistribute copies of this program"
38 print "under the terms of the GNU General Public License."
39 print "For more information about these matters, see the file named COPYING."
40 sys.exit(0)
42 if len(args) < 1:
43 parser.print_help()
44 sys.exit(1)
46 if options.gui:
47 interface_uri = 'http://0install.net/2005/interfaces/injector-gui'
48 prog_args = args[:]
49 else:
50 interface_uri = args[0]
51 prog_args = args[1:]
53 if not interface_uri.startswith('http:'):
54 interface_uri = os.path.realpath(interface_uri) # For testing
56 # Singleton instance used everywhere...
57 policy = autopolicy.AutoPolicy(interface_uri)
58 policy.recalculate()
60 if options.refresh:
61 for x in policy.walk_interfaces():
62 policy.begin_iface_download(x, False)
64 # Get interfaces...
65 try:
66 policy.wait_for_downloads()
67 except model.SafeException, ex:
68 print >>sys.stderr, ex
69 sys.exit(1)
71 try:
72 policy.execute(prog_args)
73 except model.SafeException, ex:
74 if policy.network_use != model.network_full:
75 print >>sys.stderr, "Error. Retrying with network use = full"
76 policy.network_use = model.network_full
77 policy.recalculate()
78 try:
79 policy.execute(prog_args)
80 except model.SafeException, ex:
81 print >>sys.stderr, ex
82 else:
83 print >>sys.stderr, ex