summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGary Oberbrunner <garyo@oberbrunner.com>2010-07-04 17:47:05 (GMT)
committerGary Oberbrunner <garyo@oberbrunner.com>2010-07-04 17:47:05 (GMT)
commit18d1302e8531ac99b30fa16615b270d5e751745b (patch)
treeb684687c1e666f7e506cb0227277dcd5bcaa66e5
parent4e1e98a120c8679d5492d308b7061a71dd47408d (diff)
downloadSCons-18d1302e8531ac99b30fa16615b270d5e751745b.zip
SCons-18d1302e8531ac99b30fa16615b270d5e751745b.tar.gz
SCons-18d1302e8531ac99b30fa16615b270d5e751745b.tar.bz2
Set module metadata for site_scons/site_init.py files. From Arve Knudsen; closes issue 2520.
-rw-r--r--src/CHANGES.txt6
-rw-r--r--src/engine/SCons/Script/Main.py16
-rw-r--r--test/site_scons/site_init.py54
3 files changed, 70 insertions, 6 deletions
diff --git a/src/CHANGES.txt b/src/CHANGES.txt
index c63f206..a9d4372 100644
--- a/src/CHANGES.txt
+++ b/src/CHANGES.txt
@@ -7,6 +7,12 @@
RELEASE 2.1.0.alpha.yyyymmdd - NEW DATE WILL BE INSERTED HERE
+ From Arve Knudsen:
+
+ - Set module metadata when loading site_scons/site_init.py
+ so it is treated as a proper module; __doc__, __file__ and
+ __name__ now refer to the site_init.py file.
+
From Russel Winder:
- Users Guide updates explaining that Tools can be packages as
diff --git a/src/engine/SCons/Script/Main.py b/src/engine/SCons/Script/Main.py
index eed782e..b4ebb09 100644
--- a/src/engine/SCons/Script/Main.py
+++ b/src/engine/SCons/Script/Main.py
@@ -688,7 +688,7 @@ def _load_site_scons_dir(topdir, site_dir_name=None):
site_init_file = os.path.join(site_dir, site_init_filename)
site_tools_dir = os.path.join(site_dir, site_tools_dirname)
if os.path.exists(site_init_file):
- import imp
+ import imp, re
# TODO(2.4): turn this into try:-except:-finally:
try:
try:
@@ -707,14 +707,26 @@ def _load_site_scons_dir(topdir, site_dir_name=None):
fmt = 'cannot import site_init.py: missing SCons.Script module %s'
raise SCons.Errors.InternalError(fmt % repr(e))
try:
+ sfx = description[0]
+ modname = os.path.basename(pathname)[:-len(sfx)]
+ site_m = {"__file__": pathname, "__name__": modname, "__doc__": None}
+ re_special = re.compile("__[^_]+__")
+ for k in m.__dict__.keys():
+ if not re_special.match(k):
+ site_m[k] = m.__dict__[k]
+
# This is the magic.
- exec fp in m.__dict__
+ exec fp in site_m
except KeyboardInterrupt:
raise
except Exception, e:
fmt = '*** Error loading site_init file %s:\n'
sys.stderr.write(fmt % repr(site_init_file))
raise
+ else:
+ for k in site_m:
+ if not re_special.match(k):
+ m.__dict__[k] = site_m[k]
except KeyboardInterrupt:
raise
except ImportError, e:
diff --git a/test/site_scons/site_init.py b/test/site_scons/site_init.py
index 83c9a7f..9f2e411 100644
--- a/test/site_scons/site_init.py
+++ b/test/site_scons/site_init.py
@@ -24,18 +24,64 @@
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+"""
+Verify various aspects of the handling of site_init.py.
+
+And more ..
+"""
+
import TestSCons
import sys
+import os.path
+
+test = TestSCons.TestSCons()
+
+test.subdir('site_scons')
+
+
+def _test_metadata():
+ """Test site_init's module metadata.
+
+ The following special variables should be predefined: __doc__,
+ __file__ and __name__. No special variables should be transferred from
+ SCons.Script.
+ """
+ test.write(['site_scons', 'site_init.py'], """\
+import os.path
+import re
+
+special = []
+for x in globals().keys():
+ if re.match("__[^_]+__", x):
+ if x in ("__builtins__", "__package__",):
+ # Ignore certain keywords, as they are known to be added by Python
+ continue
+ special.append(x)
+
+print sorted(special)
+print __doc__
+print os.path.realpath(__file__)
+print __name__
+""")
+
+ test.write('SConstruct', "\n")
+ test.run(arguments = '-Q .',
+ stdout = """\
+['__doc__', '__file__', '__name__']
+None
+%s
+site_init
+scons: `.' is up to date.
+""" % (os.path.realpath(os.path.join('site_scons', 'site_init.py')),))
+
+_test_metadata()
+
"""
Verify site_scons/site_init.py file can define a tool, and it shows up
automatically in the SCons.Script namespace.
"""
-test = TestSCons.TestSCons()
-
-test.subdir('site_scons')
-
if sys.platform == 'win32':
cat_cmd='type'
else: