Refactor Scanner internals as a prelude to fixing use of '${TARGET.dir}'
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Wed, 3 Mar 2004 14:45:54 +0000 (14:45 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Wed, 3 Mar 2004 14:45:54 +0000 (14:45 +0000)
in *PATH variables.

git-svn-id: http://scons.tigris.org/svn/scons/trunk@913 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/Scanner/Prog.py
src/engine/SCons/Scanner/ScannerTests.py
src/engine/SCons/Scanner/__init__.py
src/engine/SCons/Util.py
src/engine/SCons/UtilTests.py

index 5b3c93553debcccedd6767a1f7b362f551ed5899..0100b3dad1905481d07e3a5a6648a61734b203bf 100644 (file)
@@ -33,18 +33,10 @@ import SCons.Util
 def ProgScan(fs = SCons.Node.FS.default_fs):
     """Return a prototype Scanner instance for scanning executable
     files for static-lib dependencies"""
-    ps = SCons.Scanner.Base(scan, "ProgScan", fs, path_function = path)
+    pf = SCons.Scanner.FindPathDirs('LIBPATH', fs)
+    ps = SCons.Scanner.Base(scan, "ProgScan", path_function = pf)
     return ps
 
-def path(env, dir, fs = SCons.Node.FS.default_fs):
-    try:
-        libpath = env['LIBPATH']
-    except KeyError:
-        return ()
-    return tuple(fs.Rsearchall(SCons.Util.mapPaths(libpath, dir, env),
-                               clazz = SCons.Node.FS.Dir,
-                               must_exist = 0))
-
 def scan(node, env, libpath = (), fs = SCons.Node.FS.default_fs):
     """
     This scanner scans program files for static-library
index 8c8744dd2b500ef47647e3b765ac883876ab2a83..3d72bd236c3fa854d4169602cc66939cb6714e4e 100644 (file)
 
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
+import sys
 import unittest
+import UserDict
+
 import SCons.Scanner
-import sys
 
-class DummyEnvironment:
-    pass
+class DummyEnvironment(UserDict.UserDict):
+    def __init__(self, dict=None, **kw):
+        UserDict.UserDict.__init__(self, dict)
+        self.data.update(kw)
+    def subst(self, strSubst):
+        return strSubst
+
+class FindPathDirsTestCase(unittest.TestCase):
+    def test_FindPathDirs(self):
+        """Test the FindPathDirs callable class"""
+
+        class FS:
+            def Rsearchall(self, nodes, must_exist=0, clazz=None, cwd=dir):
+                return ['xxx'] + nodes
+
+        env = DummyEnvironment(LIBPATH = [ 'foo' ])
+
+        fpd = SCons.Scanner.FindPathDirs('LIBPATH', FS())
+        result = fpd(env, dir)
+        assert result == ('xxx', 'foo'), result
 
 class ScannerTestCase(unittest.TestCase):
     
@@ -297,6 +317,7 @@ class ClassicCPPTestCase(unittest.TestCase):
 def suite():
     suite = unittest.TestSuite()
     tclasses = [
+                 FindPathDirsTestCase,
                  ScannerTestCase,
                  CurrentTestCase,
                  ClassicTestCase,
index 2146ebebdb0ee6d1af40972218c81a38a6ed65f6..4bf8b6cf305722b2f3efdc91dd1054aa33f42e43 100644 (file)
@@ -43,6 +43,31 @@ class _Null:
 # used as an actual argument value.
 _null = _Null
 
+class FindPathDirs:
+    """A class to bind a specific *PATH variable name and the fs object
+    to a function that will return all of the *path directories."""
+    def __init__(self, variable, fs):
+        self.variable = variable
+        self.fs = fs
+    def __call__(self, env, dir, argument=None):
+        try:
+            path = env[self.variable]
+        except KeyError:
+            return ()
+
+        if not SCons.Util.is_List(path):
+            path = [path]
+        r = []
+        for p in path:
+            if SCons.Util.is_String(p):
+                p = env.subst(p)
+            r.append(p)
+
+        return tuple(self.fs.Rsearchall(r,
+                                        must_exist = 0,
+                                        clazz = SCons.Node.FS.Dir,
+                                        cwd = dir))
+
 class Base:
     """
     The base class for dependency scanners.  This implements
@@ -200,20 +225,11 @@ class Classic(Current):
         self.cre = re.compile(regex, re.M)
         self.fs = fs
 
-        def _path(env, dir, pv=path_variable, fs=fs):
-            try:
-                path = env[pv]
-            except KeyError:
-                return ()
-            return tuple(fs.Rsearchall(SCons.Util.mapPaths(path, dir, env),
-                                       clazz = SCons.Node.FS.Dir,
-                                       must_exist = 0))
-
         def _scan(node, env, path, self=self, fs=fs):
             return self.scan(node, env, path)
 
         kw['function'] = _scan
-        kw['path_function'] = _path
+        kw['path_function'] = FindPathDirs(path_variable, fs)
         kw['recursive'] = 1
         kw['skeys'] = suffixes
 
index d189ecf0716bfdfde3b3725c5d34d4faf7d1e7d8..97ba8b5e534243ce7989b254e11e175c4f71ff2f 100644 (file)
@@ -879,44 +879,6 @@ def is_Dict(e):
 def is_List(e):
     return type(e) is types.ListType or isinstance(e, UserList.UserList)
 
-def mapPaths(paths, dir, env=None):
-    """Takes a single node or string, or a list of nodes and/or
-    strings.  We leave the nodes untouched, but we put the strings
-    under the supplied directory node dir, if they are not an absolute
-    path.
-
-    For instance, the following:
-
-    n = SCons.Node.FS.default_fs.File('foo')
-    mapPaths([ n, 'foo', '/bar' ],
-             SCons.Node.FS.default_fs.Dir('baz'), env)
-
-    ...would return:
-
-    [ n, 'baz/foo', '/bar' ]
-
-    The env argument, if given, is used to perform variable
-    substitution on the source string(s).
-    """
-
-    def mapPathFunc(path, dir=dir, env=env):
-        if is_String(path):
-            if env:
-                path = env.subst(path)
-            if dir:
-                if not path:
-                    return str(dir)
-                if os.path.isabs(path) or path[0] == '#':
-                    return path
-                return str(dir) + os.sep + path
-        return path
-
-    if not is_List(paths):
-        paths = [ paths ]
-    ret = map(mapPathFunc, paths)
-    return ret
-
-
 if hasattr(types, 'UnicodeType'):
     def is_String(e):
         return type(e) is types.StringType \
index 85911196a8a75db85a38c53e98b4edf1d2009431..c96f1249f5576f36c9279c3cb447ca149f9503e2 100644 (file)
@@ -1132,31 +1132,6 @@ class UtilTestCase(unittest.TestCase):
         cmd_list = SCons.Util.escape_list(cmd_list[0], escape_func)
         assert cmd_list == ['BAZ', '**BLEH**'], cmd_list
 
-    def test_mapPaths(self):
-        """Test the mapPaths function"""
-        class MyFileNode:
-            def __init__(self, path):
-                self.path = path
-            def __str__(self):
-                return self.path
-
-        dir=MyFileNode('foo')
-        file=MyFileNode('bar/file')
-
-        class DummyEnv:
-            def subst(self, arg):
-                return 'bar'
-
-        res = mapPaths([ file, 'baz', 'blat/boo', '#test' ], dir)
-        assert res[0] == file, res[0]
-        assert res[1] == os.path.join('foo', 'baz'), res[1]
-        assert res[2] == os.path.join('foo', 'blat/boo'), res[2]
-        assert res[3] == '#test', res[3]
-
-        env=DummyEnv()
-        res=mapPaths('bleh', dir, env)
-        assert res[0] == os.path.normpath('foo/bar'), res[1]
-
     def test_display(self):
         old_stdout = sys.stdout
         sys.stdout = OutBuffer()