diff options
author | Steven Knight <knight@baldmt.com> | 2003-09-08 04:01:32 (GMT) |
---|---|---|
committer | Steven Knight <knight@baldmt.com> | 2003-09-08 04:01:32 (GMT) |
commit | 68b5dfbc87552d4cd1a9c067f6ba4523d9560bb9 (patch) | |
tree | 910dcf1998d6b72aa8eb4bdb5497177d331ea900 /src/engine | |
parent | 61d018dfceac6cb9717caeeea7125d7a55b4b0eb (diff) | |
download | SCons-68b5dfbc87552d4cd1a9c067f6ba4523d9560bb9.zip SCons-68b5dfbc87552d4cd1a9c067f6ba4523d9560bb9.tar.gz SCons-68b5dfbc87552d4cd1a9c067f6ba4523d9560bb9.tar.bz2 |
Give the global functions corresponding Environment methods.
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/SCons/Environment.py | 43 | ||||
-rw-r--r-- | src/engine/SCons/EnvironmentTests.py | 81 | ||||
-rw-r--r-- | src/engine/SCons/Script/SConscript.py | 62 | ||||
-rw-r--r-- | src/engine/SCons/Script/__init__.py | 6 |
4 files changed, 146 insertions, 46 deletions
diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index e7407ee..074fd3c 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -577,6 +577,20 @@ class Environment: # same-named global functions. ####################################################################### + def AddPreAction(self, files, action): + nodes = self.arg2nodes(files, self.fs.Entry) + action = SCons.Action.Action(action) + for n in nodes: + n.add_pre_action(action) + return nodes + + def AddPostAction(self, files, action): + nodes = self.arg2nodes(files, self.fs.Entry) + action = SCons.Action.Action(action) + for n in nodes: + n.add_post_action(action) + return nodes + def AlwaysBuild(self, *targets): tlist = [] for t in targets: @@ -598,6 +612,18 @@ class Environment: source_factory=SCons.Node.FS.default_fs.Entry) return bld(self, target, source) + def Default(self, *targets): + global DefaultTargets + if DefaultTargets is None: + DefaultTargets = [] + for t in targets: + if t is None: + DefaultTargets = [] + elif isinstance(t, SCons.Node.Node): + DefaultTargets.append(t) + else: + DefaultTargets.extend(self.arg2nodes(t, self.fs.Entry)) + def Depends(self, target, dependency): """Explicity specify that 'target's depend on 'dependency'.""" tlist = self.arg2nodes(target, self.fs.File) @@ -609,6 +635,11 @@ class Environment: tlist = tlist[0] return tlist + def FindFile(self, file, dirs): + file = self.subst(file) + nodes = self.arg2nodes(dirs, self.fs.Dir) + return SCons.Node.FS.find_file(file, nodes, self.fs.File) + def Ignore(self, target, dependency): """Ignore a dependency.""" tlist = self.arg2nodes(target, self.fs.File) @@ -653,6 +684,18 @@ class Environment: ret = ret[0] return ret + def Local(self, *targets): + ret = [] + for targ in targets: + if isinstance(targ, SCons.Node.Node): + targ.set_local() + ret.append(targ) + else: + for t in self.arg2nodes(targ, self.fs.Entry): + t.set_local() + ret.append(t) + return ret + def Precious(self, *targets): tlist = [] for t in targets: diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index d3807f5..db05351 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -957,6 +957,18 @@ class EnvironmentTestCase(unittest.TestCase): assert(env1['ENV']['PATH'] == r'C:\dir\num\three;C:\dir\num\two;C:\dir\num\one') assert(env1['MYENV']['MYPATH'] == r'C:\mydir\num\one;C:\mydir\num\three;C:\mydir\num\two') + def test_PrependENVPath(self): + """Test prepending to an ENV path.""" + env1 = Environment(ENV = {'PATH': r'C:\dir\num\one;C:\dir\num\two'}, + MYENV = {'MYPATH': r'C:\mydir\num\one;C:\mydir\num\two'}) + # have to include the pathsep here so that the test will work on UNIX too. + env1.PrependENVPath('PATH',r'C:\dir\num\two',sep = ';') + env1.PrependENVPath('PATH',r'C:\dir\num\three',sep = ';') + env1.PrependENVPath('MYPATH',r'C:\mydir\num\three','MYENV',sep = ';') + env1.PrependENVPath('MYPATH',r'C:\mydir\num\one','MYENV',sep = ';') + assert(env1['ENV']['PATH'] == r'C:\dir\num\three;C:\dir\num\two;C:\dir\num\one') + assert(env1['MYENV']['MYPATH'] == r'C:\mydir\num\one;C:\mydir\num\three;C:\mydir\num\two') + def test_Replace(self): """Test replacing construction variables in an Environment @@ -997,6 +1009,28 @@ class EnvironmentTestCase(unittest.TestCase): + def test_AddPostAction(self): + """Test the AddPostAction() method""" + env = Environment(FOO='fff', BAR='bbb') + + n = env.AddPostAction('$FOO', lambda x: x) + assert str(n[0]) == 'fff', n[0] + + n = env.AddPostAction(['ggg', '$BAR'], lambda x: x) + assert str(n[0]) == 'ggg', n[0] + assert str(n[1]) == 'bbb', n[1] + + def test_AddPreAction(self): + """Test the AddPreAction() method""" + env = Environment(FOO='fff', BAR='bbb') + + n = env.AddPreAction('$FOO', lambda x: x) + assert str(n[0]) == 'fff', n[0] + + n = env.AddPreAction(['ggg', '$BAR'], lambda x: x) + assert str(n[0]) == 'ggg', n[0] + assert str(n[1]) == 'bbb', n[1] + def test_AlwaysBuild(self): """Test the AlwaysBuild() method""" env = Environment(FOO='fff', BAR='bbb') @@ -1045,6 +1079,25 @@ class EnvironmentTestCase(unittest.TestCase): assert 'foo1.in' in map(lambda x: x.path, t.sources) assert 'foo2.in' in map(lambda x: x.path, t.sources) + def test_Default(self): + """Test the Default() method""" + env = Environment(FOO = 'fff', BAR = 'bbb') + + t = env.Default(None) + assert SCons.Environment.DefaultTargets == [] + + t = env.Default('xyz') + d = map(str, SCons.Environment.DefaultTargets) + assert d == ['xyz'], d + + t = env.Default('$FOO') + d = map(str, SCons.Environment.DefaultTargets) + assert d == ['xyz', 'fff'], d + + t = env.Default(None, '$BAR', 'another_file') + d = map(str, SCons.Environment.DefaultTargets) + assert d == ['bbb', 'another_file'], d + def test_Depends(self): """Test the explicit Depends method.""" env = Environment(FOO = 'xxx', BAR='yyy') @@ -1064,6 +1117,15 @@ class EnvironmentTestCase(unittest.TestCase): assert d.__class__.__name__ == 'File' assert d.path == 'yyy.py' + def test_FindFile(self): + """Test the FindFile() method""" + env = Environment(FOO = 'fff', BAR = 'bbb') + + r = env.FindFile('foo', ['no_such_directory']) + assert r is None, r + + # XXX + def test_Ignore(self): """Test the explicit Ignore method.""" env = Environment(FOO='yyy', BAR='zzz') @@ -1083,7 +1145,7 @@ class EnvironmentTestCase(unittest.TestCase): assert i.path == 'zzzyyy' def test_Install(self): - """Test Install and InstallAs methods""" + """Test the Install method""" env = Environment(FOO='iii', BAR='jjj') tgt = env.Install('export', [ 'build/foo1', 'build/foo2' ]) @@ -1129,6 +1191,10 @@ class EnvironmentTestCase(unittest.TestCase): match = str(e) == "Target `export/foo1' of Install() is a file, but should be a directory. Perhaps you have the Install() arguments backwards?" assert match, e + def test_InstallAs(self): + """Test the InstallAs method""" + env = Environment(FOO='iii', BAR='jjj') + tgt = env.InstallAs(target=string.split('foo1 foo2'), source=string.split('bar1 bar2')) assert len(tgt) == 2, len(tgt) @@ -1144,6 +1210,17 @@ class EnvironmentTestCase(unittest.TestCase): assert tgt.sources[0].path == 'jjj.s' assert tgt.builder == InstallBuilder + def test_Local(self): + """Test the Local() method.""" + env = Environment(FOO='lll') + + l = env.Local(env.fs.File('fff')) + assert str(l[0]) == 'fff', l[0] + + l = env.Local('ggg', '$FOO') + assert str(l[0]) == 'ggg', l[0] + assert str(l[1]) == 'lll', l[1] + def test_Precious(self): """Test the Precious() method.""" env = Environment(FOO='ggg', BAR='hhh') @@ -1207,7 +1284,7 @@ class EnvironmentTestCase(unittest.TestCase): s = e.src_builder() assert s is None, s - + if __name__ == "__main__": suite = unittest.makeSuite(EnvironmentTestCase, 'test_') if not unittest.TextTestRunner().run(suite).wasSuccessful(): diff --git a/src/engine/SCons/Script/SConscript.py b/src/engine/SCons/Script/SConscript.py index 1c17ee2..276ff4e 100644 --- a/src/engine/SCons/Script/SConscript.py +++ b/src/engine/SCons/Script/SConscript.py @@ -56,7 +56,6 @@ import traceback def do_nothing(text): pass HelpFunction = do_nothing -default_targets = None clean_targets = {} arguments = {} launch_dir = os.path.abspath(os.curdir) @@ -356,27 +355,6 @@ def annotate(node): # leave this disabled until we find a more efficient mechanism. #SCons.Node.Annotate = annotate -def Default(*targets): - global default_targets - if default_targets is None: - default_targets = [] - for t in targets: - if t is None: - default_targets = [] - elif isinstance(t, SCons.Node.Node): - default_targets.append(t) - else: - default_targets.extend(SCons.Node.arg2nodes(t, - SCons.Node.FS.default_fs.Entry)) - -def Local(*targets): - for targ in targets: - if isinstance(targ, SCons.Node.Node): - targ.set_local() - else: - for t in SCons.Node.arg2nodes(targ, SCons.Node.FS.default_fs.Entry): - t.set_local() - def Help(text): HelpFunction(text) @@ -390,10 +368,6 @@ def GetBuildPath(files): return ret[0] return ret -def FindFile(file, dirs): - nodes = SCons.Node.arg2nodes(dirs, SCons.Node.FS.default_fs.Dir) - return SCons.Node.FS.find_file(file, nodes) - def Export(*vars): for var in vars: global_exports.update(compute_exports(var)) @@ -512,16 +486,6 @@ def Clean(target, files): except KeyError: clean_targets[target] = nodes -def AddPreAction(files, action): - nodes = SCons.Node.arg2nodes(files, SCons.Node.FS.default_fs.Entry) - for n in nodes: - n.add_pre_action(SCons.Action.Action(action)) - -def AddPostAction(files, action): - nodes = SCons.Node.arg2nodes(files, SCons.Node.FS.default_fs.Entry) - for n in nodes: - n.add_post_action(SCons.Action.Action(action)) - def Exit(value=0): sys.exit(value) @@ -553,8 +517,6 @@ def BuildDefaultGlobals(): globals = {} globals['Action'] = SCons.Action.Action - globals['AddPostAction'] = AddPostAction - globals['AddPreAction'] = AddPreAction globals['Alias'] = Alias globals['ARGUMENTS'] = arguments globals['BuildDir'] = BuildDir @@ -563,7 +525,6 @@ def BuildDefaultGlobals(): globals['Clean'] = Clean globals['Configure'] = SCons.SConf.SConf globals['CScan'] = SCons.Defaults.CScan - globals['Default'] = Default globals['DefaultEnvironment'] = SCons.Defaults.DefaultEnvironment globals['Dir'] = SCons.Node.FS.default_fs.Dir globals['EnsurePythonVersion'] = EnsurePythonVersion @@ -572,7 +533,6 @@ def BuildDefaultGlobals(): globals['Exit'] = Exit globals['Export'] = Export globals['File'] = SCons.Node.FS.default_fs.File - globals['FindFile'] = FindFile globals['GetBuildPath'] = GetBuildPath globals['GetCommandHandler'] = SCons.Action.GetCommandHandler globals['GetJobs'] = GetJobs @@ -581,7 +541,6 @@ def BuildDefaultGlobals(): globals['Help'] = Help globals['Import'] = Import globals['Literal'] = SCons.Util.Literal - globals['Local'] = Local globals['Options'] = Options globals['ParseConfig'] = SCons.Util.ParseConfig globals['Platform'] = SCons.Platform.Platform @@ -602,4 +561,25 @@ def BuildDefaultGlobals(): globals['Tool'] = SCons.Tool.Tool globals['Value'] = SCons.Node.Python.Value globals['WhereIs'] = SCons.Util.WhereIs + + class DefaultEnvironmentCall: + """ """ + def __init__(self, method_name): + self.method_name = method_name + def __call__(self, *args, **kw): + method = getattr(SCons.Defaults.DefaultEnvironment(), + self.method_name) + return apply(method, args, kw) + + EnvironmentMethods = [ + 'AddPostAction', + 'AddPreAction', + 'Default', + 'FindFile', + 'Local', + ] + + for name in EnvironmentMethods: + globals[name] = DefaultEnvironmentCall(name) + return globals diff --git a/src/engine/SCons/Script/__init__.py b/src/engine/SCons/Script/__init__.py index 3b5c9d9..3ab4ed0 100644 --- a/src/engine/SCons/Script/__init__.py +++ b/src/engine/SCons/Script/__init__.py @@ -883,16 +883,16 @@ def _main(args, parser): # or not a file, so go ahead and keep it as a default # target and let the engine sort it out: return 1 - default_targets = SCons.Script.SConscript.default_targets + default_targets = SCons.Environment.DefaultTargets if default_targets is None: default_targets = [] else: default_targets = filter(check_dir, default_targets) - SCons.Script.SConscript.default_targets = default_targets + SCons.Environment.DefaultTargets = default_targets target_top = None lookup_top = None - targets = SCons.Script.SConscript.default_targets + targets = SCons.Environment.DefaultTargets if targets is None: targets = [SCons.Node.FS.default_fs.Dir('.')] |