-vv gives extra verbose output.
[zeroinstall.git] / 0launch
blobebf35292da509c427ad03cb8f8cc65b9e3003728
1 #!/usr/bin/env python
2 import os, sys
3 from optparse import OptionParser
5 from zeroinstall.injector import model, download, autopolicy, run, namespaces
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("-D", "--dry-run", help="just print actions", action='store_true')
12 parser.add_option("-g", "--gui", help="show graphical policy editor", action='store_true')
13 parser.add_option("-l", "--list", help="list all known interfaces", action='store_true')
14 parser.add_option("-q", "--quiet", help="suppress informational messages", action='store_true')
15 parser.add_option("-r", "--refresh", help="refresh all used interfaces", action='store_true')
16 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
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.verbose > 1:
23 # Very verbose
24 from logging import getLogger, DEBUG
25 getLogger().setLevel(DEBUG)
27 if options.list:
28 if len(args) == 0:
29 match = None
30 elif len(args) == 1:
31 match = args[0].lower()
32 else:
33 parser.print_help()
34 sys.exit(1)
35 from zeroinstall.injector import reader
36 for i in reader.list_all_interfaces():
37 if match and match not in i.lower(): continue
38 print i
39 sys.exit(0)
41 if options.version:
42 import zeroinstall
43 print "0launch (zero-install) " + zeroinstall.version
44 print "Copyright (C) 2005 Thomas Leonard"
45 print "This program comes with ABSOLUTELY NO WARRANTY,"
46 print "to the extent permitted by law."
47 print "You may redistribute copies of this program"
48 print "under the terms of the GNU General Public License."
49 print "For more information about these matters, see the file named COPYING."
50 sys.exit(0)
52 if len(args) < 1:
53 parser.print_help()
54 sys.exit(1)
56 # Singleton instance used everywhere...
57 policy = autopolicy.AutoPolicy(args[0],
58 quiet = bool(options.quiet),
59 verbose = bool(options.verbose),
60 download_only = bool(options.download_only),
61 dry_run = options.dry_run)
63 if options.gui is None and os.environ.get('DISPLAY', None):
64 if options.refresh:
65 options.gui = True
66 else:
67 options.gui = policy.need_download()
68 if options.gui and not options.quiet:
69 print "Need to download; switching to GUI mode"
71 if options.gui:
72 policy.set_root(namespaces.injector_gui_uri)
73 prog_args = args[:]
74 # Options apply to actual program, not GUI
75 if options.download_only:
76 policy.download_only = False
77 prog_args.insert(0, '--download-only')
78 if options.refresh:
79 options.refresh = False
80 prog_args.insert(0, '--refresh')
81 else:
82 prog_args = args[1:]
84 try:
85 policy.download_and_execute(prog_args, refresh = bool(options.refresh))
86 except model.SafeException, ex:
87 print >>sys.stderr, ex
88 sys.exit(1)
89 except autopolicy.NeedDownload, ex:
90 print ex
91 sys.exit(0)