Vesion 019
[rox-musicbox.git] / Extras / Next / findrox.py
blob49ed1091ae589e5bfd0f24fedcd81df7d8a17c4c
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 not os.getenv('ROXLIB_DISABLE_ZEROINSTALL') and 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('0refresh rox.sourceforge.net'):
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 import rox
44 if major == 1 and minor == 9 and micro < 10:
45 return # Can't check version
46 if not hasattr(rox, 'roxlib_version'):
47 break
48 if (major, minor, micro) <= rox.roxlib_version:
49 return # OK
50 report_error("This program needs ROX-Lib2 (version %d.%d.%d) " % \
51 (major, minor, micro) + "to run.\n" + \
52 "I tried all of these places:\n\n" + \
53 string.join(paths, '\n') + '\n\n' + \
54 "ROX-Lib2 is available from:\n" + \
55 "http://rox.sourceforge.net")
57 def report_error(err):
58 "Write 'error' to stderr and, if possible, display a dialog box too."
59 try:
60 sys.stderr.write('*** ' + err + '\n')
61 except:
62 pass
63 try:
64 import pygtk; pygtk.require('2.0')
65 import gtk; g = gtk
66 except:
67 import gtk
68 win = gtk.GtkDialog()
69 message = gtk.GtkLabel(err +
70 '\n\nAlso, pygtk2 needs to be present')
71 win.set_title('Missing ROX-Lib2')
72 win.set_position(gtk.WIN_POS_CENTER)
73 message.set_padding(20, 20)
74 win.vbox.pack_start(message)
76 ok = gtk.GtkButton("OK")
77 ok.set_flags(gtk.CAN_DEFAULT)
78 win.action_area.pack_start(ok)
79 ok.connect('clicked', gtk.mainquit)
80 ok.grab_default()
82 win.connect('destroy', gtk.mainquit)
83 win.show_all()
84 gtk.mainloop()
85 else:
86 box = g.MessageDialog(None, g.MESSAGE_ERROR, 0,
87 g.BUTTONS_OK, err)
88 box.set_title('Missing ROX-Lib2')
89 box.set_position(g.WIN_POS_CENTER)
90 box.set_default_response(g.RESPONSE_OK)
91 box.run()
92 sys.exit(1)