Cleanup
[rox-menu.git] / findrox.py
blob823b644cd30b30c6ee1b38fbf72dfdab56fabed0
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.
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'
38 if os.path.exists(zpath):
39 vs = os.readlink(zpath).split('-')[-1]
40 v = map(int, vs.split('.'))
41 if v[0] < major or v[1] < minor or v[2] < micro:
42 if os.system('0refresh rox.sourceforge.net'):
43 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))
44 sys.path.append(zpath + '/python')
45 return
46 print >>sys.stderr, "Using Zero Install, but failed to " \
47 "fetch", zpath, "-- trying non-0install system."
49 try:
50 path = os.environ['LIBDIRPATH']
51 paths = string.split(path, ':')
52 except KeyError:
53 paths = [os.environ['HOME'] + '/lib',
54 '/usr/local/lib', '/usr/lib' ]
56 for p in paths:
57 p = os.path.join(p, 'ROX-Lib2')
58 if exists(p):
59 # TODO: check version is new enough
60 sys.path.append(os.path.join(p, 'python'))
61 import rox
62 if major == 1 and minor == 9 and micro < 10:
63 return # Can't check version
64 if not hasattr(rox, 'roxlib_version'):
65 break
66 if (major, minor, micro) <= rox.roxlib_version:
67 return # OK
68 report_error("This program needs ROX-Lib2 (version %d.%d.%d) " % \
69 (major, minor, micro) + "to run.\n" + \
70 "I tried all of these places:\n\n" + \
71 string.join(paths, '\n') + '\n\n' + \
72 "ROX-Lib2 is available from:\n" + \
73 "http://rox.sourceforge.net")
75 def report_error(err):
76 "Write 'error' to stderr and, if possible, display a dialog box too."
77 try:
78 sys.stderr.write('*** ' + err + '\n')
79 except:
80 pass
81 try:
82 import pygtk; pygtk.require('2.0')
83 import gtk; g = gtk
84 except:
85 import gtk
86 win = gtk.GtkDialog()
87 message = gtk.GtkLabel(err +
88 '\n\nAlso, pygtk2 needs to be present')
89 win.set_title('Missing ROX-Lib2')
90 win.set_position(gtk.WIN_POS_CENTER)
91 message.set_padding(20, 20)
92 win.vbox.pack_start(message)
94 ok = gtk.GtkButton("OK")
95 ok.set_flags(gtk.CAN_DEFAULT)
96 win.action_area.pack_start(ok)
97 ok.connect('clicked', gtk.mainquit)
98 ok.grab_default()
100 win.connect('destroy', gtk.mainquit)
101 win.show_all()
102 gtk.mainloop()
103 else:
104 box = g.MessageDialog(None, g.MESSAGE_ERROR, 0,
105 g.BUTTONS_OK, err)
106 box.set_title('Missing ROX-Lib2')
107 box.set_position(g.WIN_POS_CENTER)
108 box.set_default_response(g.RESPONSE_OK)
109 box.run()
110 sys.exit(1)