Toplevel directory cleanup
[senf.git] / site_scons / site_init.py
1 import os.path, SCons
2
3 # SCons is at     #/tools/scons-<v>/engine/SCons/__init__.py
4 # site_init is at #/site_scons/site_init.py
5
6 sconsbase = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),"tools")
7 sconsbase = os.path.join(
8     sconsbase,sorted((f for f in os.listdir(sconsbase) if f.startswith("scons-")))[-1])
9 sconsengine = os.path.join(sconsbase, 'engine')
10 sconsscript = os.path.join(sconsbase, 'script', 'scons')
11
12 if os.path.dirname(os.path.dirname(os.path.abspath(SCons.__file__))) != sconsengine:
13     import os, sys, SCons.Util
14     SCons.Util.display("scons: Switching version")
15
16     # BEGIN HACK
17     # This is really ugly. SCons has already switched into the
18     # top-level directory when this code is executed. To make 'scons
19     # -u <target>' work, we need to switch back into the orgiginal
20     # startup dir. This directory however is only available in a local
21     # variable called 'target_top' in SCons.Script.Main._main.
22     #
23     # We walk the stack to find a local variable called
24     # 'target_top'. If we can't find it (could really only happen if
25     # the SCons.Main code is changed considerably), we output a
26     # warning, that '-u' will probably fail (we re-start scons from
27     # the top-level directory, relative targets therefore will not
28     # work)
29     frame = sys._getframe()
30     target_top = marker = []
31     while frame and target_top is marker:
32         target_top = frame.f_locals.get('target_top',marker)
33         frame = frame.f_back
34     if target_top is marker:
35         SCons.Util.display("scons: WARNING: scons -u <target> will probably fail")
36     if not target_top:
37         target_top = '.'
38     # END HACK
39
40     os.chdir(target_top)
41     os.environ['SCONS_LIB_DIR'] = sconsengine
42     os.execv(sconsscript, sys.argv)