Checkpoint refactoring of the find_file() interface.
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 1 Mar 2005 20:58:36 +0000 (20:58 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 1 Mar 2005 20:58:36 +0000 (20:58 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@1239 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/Environment.py
src/engine/SCons/Node/FS.py
src/engine/SCons/Node/FSTests.py
src/engine/SCons/Scanner/D.py
src/engine/SCons/Scanner/Prog.py
src/engine/SCons/Scanner/ScannerTests.py
src/engine/SCons/Scanner/__init__.py

index 5249bbd4ef17b950aaa3abd95c93c9206997410d..10787a7a524ffb02ff26948c597691b1aa9e890d 100644 (file)
@@ -1242,7 +1242,7 @@ class Base(SubstitutionEnvironment):
     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)
+        return SCons.Node.FS.find_file(file, tuple(nodes))
 
     def Flatten(self, sequence):
         return SCons.Util.flatten(sequence)
index d15043cd35fd12706edda2e9bbdc85b523b456e8..52f19f553ee3c9807e57685f27d6f7d80ee07cff 100644 (file)
@@ -999,15 +999,11 @@ class FS(LocalFS):
             self.__setTopLevelDir()
             self.Top.addRepository(d)
 
-    def do_Rsearch(self, path, func, clazz=_classEntry, cwd=None, verbose=lambda x: x):
+    def do_Rsearch(self, path, dir, func, clazz=_classEntry):
         """Search for something in a Repository.  Returns the first
         one found in the list, or None if there isn't one.
         __cacheable__
         """
-        if isinstance(path, SCons.Node.Node):
-            return path
-
-        path, dir = self.__transformPath(path, cwd)
         d, name = os.path.split(path)
         norm_name = _my_normcase(name)
         if d:
@@ -1021,8 +1017,7 @@ class FS(LocalFS):
             if node:
                 dir = node.get_dir()
         if node:
-            verbose("... FOUND '%s' in '%s'\n" % (name, dir))
-            return node
+            return node, dir
         fname = '.'
         while dir:
             for rep in dir.getRepositories():
@@ -1034,19 +1029,21 @@ class FS(LocalFS):
                 else:
                     node = func(node)
                 if node:
-                    verbose("... FOUND '%s' in '%s'\n" % (name, dir))
-                    return node
+                    return node, dir
             fname = dir.name + os.sep + fname
             dir = dir.get_dir()
-        return None
+        return None, None
 
     def Rsearch(self, path, clazz=_classEntry, cwd=None):
+        if isinstance(path, SCons.Node.Node):
+            return path
         def func(node):
             if node.exists() and \
                (isinstance(node, Dir) or not node.is_derived()):
                    return node
             return None
-        return self.do_Rsearch(path, func, clazz, cwd)
+        path, dir = self.__transformPath(path, cwd)
+        return self.do_Rsearch(path, dir, func, clazz)[0]
 
     def Rsearchall(self, pathlist, must_exist=1, clazz=_classEntry, cwd=None):
         """Search for a list of somethings in the Repository list.
@@ -1848,7 +1845,7 @@ class File(Base):
 
 default_fs = FS()
 
-def find_file(filename, paths, node_factory=default_fs.File, verbose=None):
+def find_file(filename, paths, verbose=None):
     """
     find_file(str, [Dir()]) -> [nodes]
 
@@ -1874,36 +1871,35 @@ def find_file(filename, paths, node_factory=default_fs.File, verbose=None):
     else:
         verbose = lambda x: x
 
-    filedir, filename = os.path.split(filename)
-    if filedir:
-        lookup_dir = lambda d, fd=filedir: d.Dir(fd)
-    else:
-        lookup_dir = lambda d: d
-
     if callable(paths):
         paths = paths()
 
     # Give Entries a chance to morph into Dirs.
     paths = map(lambda p: p.must_be_a_Dir(), paths)
 
-    for pathdir in paths:
-        verbose("looking for '%s' in '%s' ...\n" % (filename, pathdir))
-
-        try: dir = lookup_dir(pathdir)
-        except TypeError: dir = None
-        if not dir:
-            # We tried to look up a directory, but it seems there's
-            # already a file node (or something else) there.  No big.
-            continue
+    filedir, filename = os.path.split(filename)
+    if filedir:
+        def filedir_lookup(p, fd=filedir):
+            try:
+                return p.Dir(fd)
+            except TypeError:
+                # We tried to look up a Dir, but it seems there's already
+                # a File (or something else) there.  No big.
+                return None
+        paths = filter(None, map(filedir_lookup, paths))
+
+    def func(node):
+        if isinstance(node, SCons.Node.FS.File) and \
+           (node.is_derived() or node.is_pseudo_derived() or node.exists()):
+                return node
+        return None
 
-        def func(node):
-            if isinstance(node, SCons.Node.FS.File) and \
-               (node.is_derived() or node.is_pseudo_derived() or node.exists()):
-                    return node
-            return None
+    for dir in paths:
+        verbose("looking for '%s' in '%s' ...\n" % (filename, dir))
 
-        node = default_fs.do_Rsearch(filename, func, File, dir, verbose)
+        node, d = default_fs.do_Rsearch(filename, dir, func, File)
         if node:
+            verbose("... FOUND '%s' in '%s'\n" % (filename, d))
             return node
 
         dirname = '.'
@@ -1915,15 +1911,16 @@ def find_file(filename, paths, node_factory=default_fs.File, verbose=None):
                     # build_dir is probably under src_dir, in which case
                     # we are reflecting.
                     break
-                node = dir.fs.do_Rsearch(filename, func, File, d, verbose)
+                node, d = dir.fs.do_Rsearch(filename, d, func, File)
                 if node:
+                    verbose("... FOUND '%s' in '%s'\n" % (filename, d))
                     return File(filename, dir.Dir(dirname), dir.fs)
             dirname = dir.name + os.sep + dirname
             dir = dir.get_dir()
 
     return None
 
-def find_files(filenames, paths, node_factory = default_fs.File):
+def find_files(filenames, paths):
     """
     find_files([str], [Dir()]) -> [nodes]
 
@@ -1938,7 +1935,5 @@ def find_files(filenames, paths, node_factory = default_fs.File):
     Only the first file found is returned for each filename,
     and any files that aren't found are ignored.
     """
-    nodes = map(lambda x, paths=paths, node_factory=node_factory:
-                       find_file(x, paths, node_factory),
-                filenames)
-    return filter(lambda x: x != None, nodes)
+    nodes = map(lambda x, paths=paths: find_file(x, paths), filenames)
+    return filter(None, nodes)
index f841d32afff59d4110c6a74614b10714ee96c32a..c3f87bc9ae510d7654df0b6d6606021a6c5b1ef7 100644 (file)
@@ -1443,10 +1443,10 @@ class find_fileTestCase(unittest.TestCase):
         node_pseudo.set_src_builder(1) # Any non-zero value.
 
         paths = map(fs.Dir, ['.', 'same', './bar'])
-        nodes = [SCons.Node.FS.find_file('foo', paths, fs.File)]
-        nodes.append(SCons.Node.FS.find_file('baz', paths, fs.File))
-        nodes.append(SCons.Node.FS.find_file('pseudo', paths, fs.File))
-        nodes.append(SCons.Node.FS.find_file('same', paths, fs.File))
+        nodes = [SCons.Node.FS.find_file('foo', paths)]
+        nodes.append(SCons.Node.FS.find_file('baz', paths))
+        nodes.append(SCons.Node.FS.find_file('pseudo', paths))
+        nodes.append(SCons.Node.FS.find_file('same', paths))
 
         file_names = map(str, nodes)
         file_names = map(os.path.normpath, file_names)
@@ -1458,7 +1458,7 @@ class find_fileTestCase(unittest.TestCase):
         # of a directory that we'd otherwise try to search.  If this
         # is broken, we'll see an exception like "Tried to lookup File
         # 'bar/baz' as a Dir.
-        SCons.Node.FS.find_file('baz/no_file_here', paths, fs.File)
+        SCons.Node.FS.find_file('baz/no_file_here', paths)
 
         import StringIO
         save_sys_stdout = sys.stdout
@@ -1466,7 +1466,7 @@ class find_fileTestCase(unittest.TestCase):
         try:
             sio = StringIO.StringIO()
             sys.stdout = sio
-            SCons.Node.FS.find_file('foo', paths, fs.File, verbose="xyz")
+            SCons.Node.FS.find_file('foo', paths, verbose="xyz")
             expect = "  xyz: looking for 'foo' in '.' ...\n" + \
                      "  xyz: ... FOUND 'foo' in '.'\n"
             c = sio.getvalue()
@@ -1474,7 +1474,7 @@ class find_fileTestCase(unittest.TestCase):
 
             sio = StringIO.StringIO()
             sys.stdout = sio
-            SCons.Node.FS.find_file('baz', paths, fs.File, verbose=1)
+            SCons.Node.FS.find_file('baz', paths, verbose=1)
             expect = "  find_file: looking for 'baz' in '.' ...\n" + \
                      "  find_file: looking for 'baz' in 'same' ...\n" + \
                      "  find_file: looking for 'baz' in 'bar' ...\n" + \
@@ -1484,7 +1484,7 @@ class find_fileTestCase(unittest.TestCase):
 
             sio = StringIO.StringIO()
             sys.stdout = sio
-            SCons.Node.FS.find_file('on_disk', paths, fs.File, verbose=1)
+            SCons.Node.FS.find_file('on_disk', paths, verbose=1)
             expect = "  find_file: looking for 'on_disk' in '.' ...\n" + \
                      "  find_file: looking for 'on_disk' in 'same' ...\n" + \
                      "  find_file: looking for 'on_disk' in 'bar' ...\n" + \
index b80912d19fa327a69ee5871de64d9778b21b5202..cd93a6908086dad26cdb9136e561a13c4c1a5f66 100644 (file)
@@ -51,7 +51,5 @@ class D(SCons.Scanner.Classic):
         # translate dots (package separators) to slashes
         inc = string.replace(include, '.', '/')
 
-        i = SCons.Node.FS.find_file(inc + '.d',
-                                    (source_dir,) + path,
-                                    self.fs.File)
+        i = SCons.Node.FS.find_file(inc + '.d', (source_dir,) + path)
         return i, include
index f2f3bbf63863e3999a933d31b12195e5c17933ae..206c6de9dd635a78b0a531da7c2a226bc9b3f04b 100644 (file)
@@ -89,7 +89,7 @@ def scan(node, env, libpath = (), fs = SCons.Node.FS.default_fs):
             lib = env.subst(lib)
             for pref, suf in pairs:
                 l = adjustixes(lib, pref, suf)
-                l = find_file(l, libpath, fs.File, verbose=print_find_libs)
+                l = find_file(l, libpath, verbose=print_find_libs)
                 if l:
                     result.append(l)
         else:
index ce5411c2e84d162005dfa9cadf00a70d0d7b7063..ef836bca6475ea5dc085703e08fcc8b51e26eb02 100644 (file)
@@ -390,7 +390,7 @@ class ClassicTestCase(unittest.TestCase):
         env = DummyEnvironment()
         s = SCons.Scanner.Classic("t", ['.suf'], 'MYPATH', '^my_inc (\S+)')
 
-        def _find_file(filename, paths, factory):
+        def _find_file(filename, paths):
             return paths[0]+'/'+filename
 
         save = SCons.Node.FS.find_file
@@ -491,7 +491,7 @@ class ClassicCPPTestCase(unittest.TestCase):
         env = DummyEnvironment()
         s = SCons.Scanner.ClassicCPP("Test", [], None, "")
 
-        def _find_file(filename, paths, factory):
+        def _find_file(filename, paths):
             if callable(paths):
                 paths = paths()
             return paths[0]+'/'+filename
index cda156ca22e31bb517db419f62634eeea4dbb0ed..247eafb75bf344f618309ece4b62ebf8c675d961 100644 (file)
@@ -338,9 +338,7 @@ class Classic(Current):
     def find_include(self, include, source_dir, path):
         "__cacheable__"
         if callable(path): path = path()
-        n = SCons.Node.FS.find_file(include,
-                                    (source_dir,) + tuple(path),
-                                    SCons.Node.FS.File)
+        n = SCons.Node.FS.find_file(include, (source_dir,) + tuple(path))
         return n, include
 
     def sort_key(self, include):
@@ -398,9 +396,7 @@ class ClassicCPP(Classic):
         else:
             paths = Binder( tuple(path) + (source_dir,) )
 
-        n = SCons.Node.FS.find_file(include[1],
-                                    paths,
-                                    self.fs.File)
+        n = SCons.Node.FS.find_file(include[1], paths)
 
         return n, include[1]