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
8 from os
.path
import exists
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
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" % \
28 rox
.roxlib_version
[0],
29 rox
.roxlib_version
[1],
30 rox
.roxlib_version
[2],
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/' \
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')
49 path
= os
.environ
['LIBDIRPATH']
50 paths
= string
.split(path
, ':')
52 paths
= [os
.environ
['HOME'] + '/lib',
53 '/usr/local/lib', '/usr/lib' ]
56 p
= os
.path
.join(p
, 'ROX-Lib2')
58 # TODO: check version is new enough
59 sys
.path
.append(os
.path
.join(p
, 'python'))
61 if major
== 1 and minor
== 9 and micro
< 10:
62 return # Can't check version
63 if not hasattr(rox
, 'roxlib_version'):
65 if (major
, minor
, micro
) <= rox
.roxlib_version
:
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."
77 sys
.stderr
.write('*** ' + err
+ '\n')
81 import pygtk
; pygtk
.require('2.0')
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
)
99 win
.connect('destroy', gtk
.mainquit
)
103 box
= g
.MessageDialog(None, g
.MESSAGE_ERROR
, 0,
105 box
.set_title('Missing ROX-Lib2')
106 box
.set_position(g
.WIN_POS_CENTER
)
107 box
.set_default_response(g
.RESPONSE_OK
)