From ec3b571538533abcf0da7769e37a550606c68525 Mon Sep 17 00:00:00 2001 From: stevenknight Date: Tue, 1 Mar 2005 20:58:36 +0000 Subject: [PATCH] Checkpoint refactoring of the find_file() interface. git-svn-id: http://scons.tigris.org/svn/scons/trunk@1239 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- src/engine/SCons/Environment.py | 2 +- src/engine/SCons/Node/FS.py | 73 +++++++++++------------- src/engine/SCons/Node/FSTests.py | 16 +++--- src/engine/SCons/Scanner/D.py | 4 +- src/engine/SCons/Scanner/Prog.py | 2 +- src/engine/SCons/Scanner/ScannerTests.py | 4 +- src/engine/SCons/Scanner/__init__.py | 8 +-- 7 files changed, 49 insertions(+), 60 deletions(-) diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index 5249bbd4..10787a7a 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -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) diff --git a/src/engine/SCons/Node/FS.py b/src/engine/SCons/Node/FS.py index d15043cd..52f19f55 100644 --- a/src/engine/SCons/Node/FS.py +++ b/src/engine/SCons/Node/FS.py @@ -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) diff --git a/src/engine/SCons/Node/FSTests.py b/src/engine/SCons/Node/FSTests.py index f841d32a..c3f87bc9 100644 --- a/src/engine/SCons/Node/FSTests.py +++ b/src/engine/SCons/Node/FSTests.py @@ -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" + \ diff --git a/src/engine/SCons/Scanner/D.py b/src/engine/SCons/Scanner/D.py index b80912d1..cd93a690 100644 --- a/src/engine/SCons/Scanner/D.py +++ b/src/engine/SCons/Scanner/D.py @@ -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 diff --git a/src/engine/SCons/Scanner/Prog.py b/src/engine/SCons/Scanner/Prog.py index f2f3bbf6..206c6de9 100644 --- a/src/engine/SCons/Scanner/Prog.py +++ b/src/engine/SCons/Scanner/Prog.py @@ -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: diff --git a/src/engine/SCons/Scanner/ScannerTests.py b/src/engine/SCons/Scanner/ScannerTests.py index ce5411c2..ef836bca 100644 --- a/src/engine/SCons/Scanner/ScannerTests.py +++ b/src/engine/SCons/Scanner/ScannerTests.py @@ -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 diff --git a/src/engine/SCons/Scanner/__init__.py b/src/engine/SCons/Scanner/__init__.py index cda156ca..247eafb7 100644 --- a/src/engine/SCons/Scanner/__init__.py +++ b/src/engine/SCons/Scanner/__init__.py @@ -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] -- 2.26.2