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