diff options
author | Steven Knight <knight@baldmt.com> | 2004-09-25 02:50:45 (GMT) |
---|---|---|
committer | Steven Knight <knight@baldmt.com> | 2004-09-25 02:50:45 (GMT) |
commit | f5f2d8ad9d24df1b0f58477006b784adc4bc8a03 (patch) | |
tree | 5c0fc7d7e7bec84d3e88ca77133d693f7eb98cd1 /src | |
parent | e4a3739689eac21ce5db67d606a9fe29a7695394 (diff) | |
download | SCons-f5f2d8ad9d24df1b0f58477006b784adc4bc8a03.zip SCons-f5f2d8ad9d24df1b0f58477006b784adc4bc8a03.tar.gz SCons-f5f2d8ad9d24df1b0f58477006b784adc4bc8a03.tar.bz2 |
Add a ParseDepends() function to read up mkdep-style files.
Diffstat (limited to 'src')
-rw-r--r-- | src/CHANGES.txt | 3 | ||||
-rw-r--r-- | src/engine/SCons/Environment.py | 26 | ||||
-rw-r--r-- | src/engine/SCons/EnvironmentTests.py | 32 | ||||
-rw-r--r-- | src/engine/SCons/Script/SConscript.py | 1 | ||||
-rw-r--r-- | src/engine/SCons/Util.py | 29 | ||||
-rw-r--r-- | src/engine/SCons/UtilTests.py | 23 |
6 files changed, 114 insertions, 0 deletions
diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 1008e8e..7379368 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -69,6 +69,9 @@ RELEASE 0.97 - XXX - Allow a ListOption's default value(s) to be a Python list of specified values, not just a string containing a comma-separated list of names. + - Add a ParseDepends() function that will parse up a list of explicit + dependencies from a "make depend" style file. + From Clive Levinson: - Make ParseConfig() recognize and add -mno-cygwin to $LINKFLAGS and diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index df2a93a..9fa7b34 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -792,6 +792,32 @@ class Base: command = self.subst(command) return function(self, os.popen(command).read()) + def ParseDepends(self, filename, must_exist=None): + """ + Parse a mkdep-style file for explicit dependencies. This is + completely abusable, and should be unnecessary in the "normal" + case of proper SCons configuration, but it may help make + the transition from a Make hierarchy easier for some people + to swallow. It can also be genuinely useful when using a tool + that can write a .d file, but for which writing a scanner would + be too complicated. + """ + try: + fp = open(filename, 'r') + except IOError: + if must_exist: + raise + return + for line in SCons.Util.LogicalLines(fp).readlines(): + if line[0] == '#': + continue + try: + target, depends = string.split(line, ':', 1) + except: + pass + else: + self.Depends(string.split(target), string.split(depends)) + def Platform(self, platform): platform = self.subst(platform) return SCons.Platform.Platform(platform)(self) diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 7023fea..aa38965 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -1353,6 +1353,38 @@ class EnvironmentTestCase(unittest.TestCase): finally: os.popen = orig_popen + def test_ParseDepends(self): + """Test the ParseDepends() method""" + env = Environment() + + test = TestCmd.TestCmd(workdir = '') + + test.write('mkdep', """ +f1: foo +f2 f3: bar +f4: abc def +#file: dependency +f5: \ + ghi \ + jkl \ + mno \ +""") + + tlist = [] + dlist = [] + def my_depends(target, dependency, tlist=tlist, dlist=dlist): + tlist.extend(target) + dlist.extend(dependency) + + env.Depends = my_depends + + env.ParseDepends(test.workpath('mkdep')) + + t = map(str, tlist) + d = map(str, dlist) + assert t == ['f1', 'f2', 'f3', 'f4', 'f5'], t + assert d == ['foo', 'bar', 'abc', 'def', 'ghi', 'jkl', 'mno'], d + def test_Platform(self): """Test the Platform() method""" env = Environment(WIN32='win32', NONE='no-such-platform') diff --git a/src/engine/SCons/Script/SConscript.py b/src/engine/SCons/Script/SConscript.py index fab0d82..88d3268 100644 --- a/src/engine/SCons/Script/SConscript.py +++ b/src/engine/SCons/Script/SConscript.py @@ -594,6 +594,7 @@ GlobalDefaultEnvironmentFunctions = [ 'InstallAs', 'Literal', 'Local', + 'ParseDepends', 'Precious', 'Repository', 'SConsignFile', diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index df2f604..afcaf11 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -1520,3 +1520,32 @@ def unique(s): if x not in u: u.append(x) return u + +# Much of the logic here was originally based on recipe 4.9 from the +# Python CookBook, but we had to dumb it way down for Python 1.5.2. +class LogicalLines: + + def __init__(self, fileobj): + self.fileobj = fileobj + + def readline(self): + result = [] + while 1: + line = self.fileobj.readline() + if not line: + break + if line[-2:] == '\\\n': + result.append(line[:-2]) + else: + result.append(line) + break + return string.join(result, '') + + def readlines(self): + result = [] + while 1: + line = self.readline() + if not line: + break + result.append(line) + return result diff --git a/src/engine/SCons/UtilTests.py b/src/engine/SCons/UtilTests.py index 2b5fdef..054d0b2 100644 --- a/src/engine/SCons/UtilTests.py +++ b/src/engine/SCons/UtilTests.py @@ -1552,6 +1552,29 @@ class UtilTestCase(unittest.TestCase): assert containsOnly('.83', '0123456789.') assert not containsOnly('43221', '123') + def test_LogicalLines(self): + """Test the LogicalLines class""" + import StringIO + + fobj = StringIO.StringIO(r""" +foo \ +bar \ +baz +foo +bling \ +bling \ bling +bling +""") + + lines = LogicalLines(fobj).readlines() + assert lines == [ + '\n', + 'foo bar baz\n', + 'foo\n', + 'bling bling \\ bling\n', + 'bling\n', + ], lines + if __name__ == "__main__": suite = unittest.makeSuite(UtilTestCase, 'test_') if not unittest.TextTestRunner().run(suite).wasSuccessful(): |