Fix the 'x lines selected' message (v3)
[rox-edit/bju.git] / findrox.py
blob3405e2a646dd105c77c3cf9dd0d33c6e211eea51
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 run findrox.version() 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.
15 If 'rox' is already in PYTHONPATH, just use that (assume the injector
16 is being used)."""
17 try:
18 import rox
19 except ImportError:
20 pass
21 else:
22 #print "Using ROX-Lib in PYTHONPATH"
23 if (major, minor, micro) > rox.roxlib_version:
24 print >>sys.stderr, "WARNING: ROX-Lib version " \
25 "%d.%d.%d requested, but using version " \
26 "%d.%d.%d from %s" % \
27 (major, minor, micro,
28 rox.roxlib_version[0],
29 rox.roxlib_version[1],
30 rox.roxlib_version[2],
31 rox.__file__)
32 return
34 if not os.getenv('ROXLIB_DISABLE_ZEROINSTALL') and os.path.exists('/uri/0install/rox.sourceforge.net'):
35 # We're using ZeroInstall. Good :-)
36 zpath = '/uri/0install/rox.sourceforge.net/lib/ROX-Lib2/' \
37 'latest-2'
38 if not os.path.exists(zpath):
39 os.system('0refresh rox.sourceforge.net')
40 assert os.path.exists(zpath)
41 vs = os.readlink(zpath).split('-')[-1]
42 v = tuple(map(int, vs.split('.')))
43 if v < (major, minor, micro):
44 if os.system('0refresh rox.sourceforge.net'):
45 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))
46 sys.path.append(zpath + '/python')
47 return
48 try:
49 path = os.environ['LIBDIRPATH']
50 paths = string.split(path, ':')
51 except KeyError:
52 paths = [os.environ['HOME'] + '/lib',
53 '/usr/local/lib', '/usr/lib' ]
55 for p in paths:
56 p = os.path.join(p, 'ROX-Lib2')
57 if exists(p):
58 # TODO: check version is new enough
59 sys.path.append(os.path.join(p, 'python'))
60 import rox
61 if major == 1 and minor == 9 and micro < 10:
62 return # Can't check version
63 if not hasattr(rox, 'roxlib_version'):
64 break
65 if (major, minor, micro) <= rox.roxlib_version:
66 return # OK
67 report_error("This program needs ROX-Lib2 (version %d.%d.%d) " % \
68 (major, minor, micro) + "to run.\n" + \
69 "I tried all of these places:\n\n" + \
70 string.join(paths, '\n') + '\n\n' + \
71 "ROX-Lib2 is available from:\n" + \
72 "http://rox.sourceforge.net")
74 def report_error(err):
75 "Write 'error' to stderr and, if possible, display a dialog box too."
76 try:
77 sys.stderr.write('*** ' + err + '\n')
78 except:
79 pass
80 try:
81 import pygtk; pygtk.require('2.0')
82 import gtk; g = gtk
83 except:
84 import gtk
85 win = gtk.GtkDialog()
86 message = gtk.GtkLabel(err +
87 '\n\nAlso, pygtk2 needs to be present')
88 win.set_title('Missing ROX-Lib2')
89 win.set_position(gtk.WIN_POS_CENTER)
90 message.set_padding(20, 20)
91 win.vbox.pack_start(message)
93 ok = gtk.GtkButton("OK")
94 ok.set_flags(gtk.CAN_DEFAULT)
95 win.action_area.pack_start(ok)
96 ok.connect('clicked', gtk.mainquit)
97 ok.grab_default()
99 win.connect('destroy', gtk.mainquit)
100 win.show_all()
101 gtk.mainloop()
102 else:
103 box = g.MessageDialog(None, g.MESSAGE_ERROR, 0,
104 g.BUTTONS_OK, err)
105 box.set_title('Missing ROX-Lib2')
106 box.set_position(g.WIN_POS_CENTER)
107 box.set_default_response(g.RESPONSE_OK)
108 box.run()
109 sys.exit(1)