Better colour-scheme.
[dom-editor.git] / Dome / findrox.py
blobe05beded211e8e60f4387a93a2da82f9f0aaa71c
1 # Most of the common code needed by ROX applications is in ROX-Lib2.
2 # Except this code, which is needed to find ROX-Lib2 in the first place!
4 # Just make sure you import findrox before importing anything inside
5 # ROX-Lib2...
7 import os, sys
8 from os.path import exists
9 import string
11 def version(major, minor, micro):
12 """Find ROX-Lib2, with a version >= (major, minor, micro), and
13 add it to sys.path. If version is missing or too old, either
14 prompt the user, or (if possible) upgrade it automatically."""
16 if os.path.exists('/uri/0install/rox.sourceforge.net'):
17 # We're using ZeroInstall. Good :-)
18 zpath = '/uri/0install/rox.sourceforge.net/lib/ROX-Lib2/' \
19 'latest'
20 if os.path.exists(zpath):
21 vs = os.readlink(zpath).split('-')[-1]
22 v = map(int, vs.split('.'))
23 if v[0] < major or v[1] < minor or v[2] < micro:
24 if os.system('cd /uri/0install/rox.sourceforge.net; 0refresh'):
25 report_error('Using ROX-Lib in Zero Install, but cached version (%s) is too old (need %d.%d.%d) and updating failed (is zero-install running?)' % (vs, major, minor, micro))
26 sys.path.append(zpath + '/python')
27 return
28 print >>sys.stderr, "Using Zero Install, but failed to " \
29 "fetch", zpath, "-- trying non-0install system."
31 try:
32 path = os.environ['LIBDIRPATH']
33 paths = string.split(path, ':')
34 except KeyError:
35 paths = [os.environ['HOME'] + '/lib',
36 '/usr/local/lib', '/usr/lib' ]
38 for p in paths:
39 p = os.path.join(p, 'ROX-Lib2')
40 if exists(p):
41 # TODO: check version is new enough
42 sys.path.append(os.path.join(p, 'python'))
43 break
44 else:
45 report_error("This program needs ROX-Lib2 to run.\n" + \
46 "I tried all of these places:\n\n" + \
47 string.join(paths, '\n') + '\n\n' + \
48 "ROX-Lib2 is available from:\n" + \
49 "http://rox.sourceforge.net")
51 def report_error(err):
52 "Write 'error' to stderr and, if possible, display a dialog box too."
53 try:
54 sys.stderr.write('*** ' + err + '\n')
55 except:
56 pass
57 try:
58 import pygtk; pygtk.require('2.0')
59 import gtk; g = gtk
60 except:
61 import gtk
62 win = gtk.GtkDialog()
63 message = gtk.GtkLabel(err +
64 '\n\nAlso, pygtk2 needs to be present')
65 win.set_title('Missing ROX-Lib2')
66 win.set_position(gtk.WIN_POS_CENTER)
67 message.set_padding(20, 20)
68 win.vbox.pack_start(message)
70 ok = gtk.GtkButton("OK")
71 ok.set_flags(gtk.CAN_DEFAULT)
72 win.action_area.pack_start(ok)
73 ok.connect('clicked', gtk.mainquit)
74 ok.grab_default()
76 win.connect('destroy', gtk.mainquit)
77 win.show_all()
78 gtk.mainloop()
79 else:
80 box = g.MessageDialog(None, g.MESSAGE_ERROR, 0,
81 g.BUTTONS_OK, err)
82 box.set_title('Missing ROX-Lib2')
83 box.set_position(g.WIN_POS_CENTER)
84 box.set_default_response(g.RESPONSE_OK)
85 box.run()
86 sys.exit(1)