Added some missing copyright headers.
[zeroinstall.git] / zeroinstall / 0launch-gui / main.py
blob87d85f15683138508da871f4ffd3e6fdc3e5338e
1 # Copyright (C) 2008, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import os, sys
6 from optparse import OptionParser
8 from zeroinstall.injector import model, autopolicy, namespaces
9 from zeroinstall.injector.policy import Policy
10 from zeroinstall.injector.iface_cache import iface_cache
11 from zeroinstall.support import tasks
13 def run_gui(args):
14 parser = OptionParser(usage="usage: %prog [options] interface")
15 parser.add_option("", "--before", help="choose a version before this", metavar='VERSION')
16 parser.add_option("-c", "--cache", help="show the cache", action='store_true')
17 parser.add_option("-d", "--download-only", help="fetch but don't run", action='store_true')
18 parser.add_option("", "--not-before", help="minimum version to choose", metavar='VERSION')
19 parser.add_option("-r", "--refresh", help="check for updates of all interfaces", action='store_true')
20 parser.add_option("-s", "--source", help="select source code", action='store_true')
21 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
22 parser.add_option("-V", "--version", help="display version information", action='store_true')
24 parser.disable_interspersed_args()
26 (options, args) = parser.parse_args(args)
28 if options.verbose:
29 import logging
30 logger = logging.getLogger()
31 if options.verbose == 1:
32 logger.setLevel(logging.INFO)
33 else:
34 logger.setLevel(logging.DEBUG)
36 if options.cache:
37 # Must fork before importing gtk, or ATK dies
38 if os.fork():
39 # We exit, so our parent can call waitpid and unblock.
40 sys.exit(0)
41 # The grandchild continues...
43 import gui
45 if options.version:
46 print "0launch-gui (zero-install) " + gui.version
47 print "Copyright (C) 2007 Thomas Leonard"
48 print "This program comes with ABSOLUTELY NO WARRANTY,"
49 print "to the extent permitted by law."
50 print "You may redistribute copies of this program"
51 print "under the terms of the GNU General Public License."
52 print "For more information about these matters, see the file named COPYING."
53 sys.exit(0)
55 import gtk
56 if gtk.gdk.get_display() is None:
57 print >>sys.stderr, "Failed to connect to display. Aborting."
58 sys.exit(1)
60 if not hasattr(gtk, 'combo_box_new_text'):
61 import combo_compat
63 if options.cache:
64 import cache
65 cache_explorer = cache.CacheExplorer()
66 cache_explorer.show()
67 cache_explorer.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
68 gtk.gdk.flush()
69 cache_explorer.populate_model()
70 cache_explorer.window.set_cursor(None)
71 gtk.main()
72 sys.exit(0)
74 if len(args) < 1:
75 parser.print_help()
76 sys.exit(1)
78 interface_uri = args[0]
80 if len(args) > 1:
81 parser.print_help()
82 sys.exit(1)
84 import mainwindow, dialog
86 restrictions = []
87 if options.before or options.not_before:
88 restrictions.append(model.VersionRangeRestriction(model.parse_version(options.before),
89 model.parse_version(options.not_before)))
91 widgets = dialog.Template('main')
93 handler = gui.GUIHandler()
94 policy = Policy(interface_uri, handler, src = bool(options.source))
95 root_iface = iface_cache.get_interface(interface_uri)
96 policy.solver.extra_restrictions[root_iface] = restrictions
97 policy.solver.record_details = True
99 window = mainwindow.MainWindow(policy, widgets, download_only = bool(options.download_only))
100 handler.mainwindow = window
102 root = iface_cache.get_interface(policy.root)
103 window.browser.set_root(root)
105 window.window.connect('destroy', lambda w: handler.abort_all_downloads())
107 @tasks.async
108 def main():
109 force_refresh = bool(options.refresh)
110 while True:
111 window.refresh_button.set_sensitive(False)
113 solved = policy.solve_with_downloads(force = force_refresh)
115 window.show()
116 yield solved
117 try:
118 window.refresh_button.set_sensitive(True)
119 tasks.check(solved)
120 except model.SafeException, ex:
121 dialog.alert(window.window, str(ex))
122 except Exception, ex:
123 import traceback
124 traceback.print_exc()
125 dialog.alert(window.window, str(ex))
127 yield dialog.ButtonClickedBlocker(window.refresh_button)
128 force_refresh = True
130 handler.wait_for_blocker(main())