From: stevenknight Date: Sun, 15 Sep 2002 01:45:17 +0000 (+0000) Subject: Provide a Scanner hook to allow file scans to be avoided when it isn't necessary. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=31071bba5682cc9d9d8ae68cf78bbac77a1210da;p=scons.git Provide a Scanner hook to allow file scans to be avoided when it isn't necessary. git-svn-id: http://scons.tigris.org/svn/scons/trunk@466 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/doc/man/scons.1 b/doc/man/scons.1 index dfed6c34..13c31d74 100644 --- a/doc/man/scons.1 +++ b/doc/man/scons.1 @@ -2681,6 +2681,16 @@ The argument is the argument supplied when the scanner was created, if any. +.IP scan_check +An optional Python function that takes a Node (file) +as an argument and returns whether the +Node should, in fact, +be scanned for dependencies. +This check can be used to eliminate unnecessary +calls to the scanner function when, +for example, the underlying file +represented by a Node does not yet exist. + .SH SYSTEM-SPECIFIC BEHAVIOR SCons and its configuration files are very portable, due largely to its implementation in Python. diff --git a/src/CHANGES.txt b/src/CHANGES.txt index dc203937..ba3ccb16 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -37,6 +37,10 @@ RELEASE 0.09 - - Issue a warning when -c can't remove a target. + - Eliminate unnecessary Scanner calls by checking for the + existence of a file before scanning it. (This adds a generic + hook to check an arbitrary condition before scanning.) + From Jeff Petkau: - Fix interpretation of '#/../foo' on Win32 systems. diff --git a/src/engine/SCons/Scanner/ScannerTests.py b/src/engine/SCons/Scanner/ScannerTests.py index fce4078e..2d0e47a5 100644 --- a/src/engine/SCons/Scanner/ScannerTests.py +++ b/src/engine/SCons/Scanner/ScannerTests.py @@ -124,6 +124,21 @@ class ScannerHashTestCase(ScannerTestBase, unittest.TestCase): self.failUnless(hash(dict.keys()[0]) == hash(None), "did not hash Scanner base class as expected") +class ScannerCheckTestCase(unittest.TestCase): + "Test the Scanner.Base class __hash__() method" + def setUp(self): + self.checked = {} + def runTest(self): + def my_scan(filename, env, target, *args): + return [] + def check(node, s=self): + s.checked[node] = 1 + return 1 + s = SCons.Scanner.Base(my_scan, "Check", scan_check = check) + scanned = s.scan('x', DummyEnvironment(), DummyTarget()) + self.failUnless(self.checked['x'] == 1, + "did not call check function") + def suite(): suite = unittest.TestSuite() suite.addTest(ScannerPositionalTestCase()) @@ -131,6 +146,7 @@ def suite(): suite.addTest(ScannerPositionalArgumentTestCase()) suite.addTest(ScannerKeywordArgumentTestCase()) suite.addTest(ScannerHashTestCase()) + suite.addTest(ScannerCheckTestCase()) return suite if __name__ == "__main__": diff --git a/src/engine/SCons/Scanner/__init__.py b/src/engine/SCons/Scanner/__init__.py index 28159c91..dc106480 100644 --- a/src/engine/SCons/Scanner/__init__.py +++ b/src/engine/SCons/Scanner/__init__.py @@ -52,7 +52,8 @@ class Base: name = "NONE", argument = _null, skeys = [], - node_factory = SCons.Node.FS.default_fs.File): + node_factory = SCons.Node.FS.default_fs.File, + scan_check = None): """ Construct a new scanner object given a scanner function. @@ -77,7 +78,7 @@ class Base: Examples: s = Scanner(my_scanner_function) - + s = Scanner(function = my_scanner_function) s = Scanner(function = my_scanner_function, argument = 'foo') @@ -94,6 +95,7 @@ class Base: self.argument = argument self.skeys = skeys self.node_factory = node_factory + self.scan_check = scan_check def scan(self, node, env, target): """ @@ -102,6 +104,8 @@ class Base: environment that will be passed to the scanner function. A list of direct dependency nodes for the specified node will be returned. """ + if self.scan_check and not self.scan_check(node): + return [] if not self.argument is _null: list = self.function(node, env, target, self.argument) @@ -123,7 +127,17 @@ class Base: def __hash__(self): return hash(None) -class Recursive(Base): +class RExists(Base): + """ + Scan a node only if it exists (locally or in a Repository). + """ + def __init__(self, *args, **kw): + def rexists_check(node): + return node.rexists() + kw['scan_check'] = rexists_check + apply(Base.__init__, (self,) + args, kw) + +class Recursive(RExists): """ The class for recursive dependency scanning. This will re-scan any new files returned by each call to the diff --git a/test/scan-once.py b/test/scan-once.py new file mode 100644 index 00000000..76c3451d --- /dev/null +++ b/test/scan-once.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python +# +# Copyright (c) 2001, 2002 Steven Knight +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# + +__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" + +import TestSCons + +test = TestSCons.TestSCons() + +test.write('SConstruct', r""" +import os.path + +def scan(node, env, target, arg): + print 'scanning',node,'for',target + return [] + +def exists_check(node): + return os.path.exists(str(node)) + +PScanner = Scanner(name = 'PScanner', + function = scan, + argument = None, + scan_check = exists_check, + skeys = ['.s']) + +def echo(env, target, source): + print 'create %s from %s' % (str(target[0]), str(source[0])) + +Echo = Builder(action = echo, + src_suffix = '.s', + suffix = '.s') + +env = Environment(BUILDERS = {'Echo':Echo}, SCANNERS = [PScanner]) + +f1 = env.Echo(source=['file1'], target=['file2']) +f2 = env.Echo(source=['file2'], target=['file3']) +f3 = env.Echo(source=['file3'], target=['file4']) + +Default(f3) +""") + +test.run(arguments = '.', stdout = """create file2.s from file1.s +create file3.s from file2.s +create file4.s from file3.s +""") + +test.write('file1.s', 'file1.s\n') + +test.run(arguments = '.', stdout = """scanning file1.s for file2.s +create file2.s from file1.s +scanning file1.s for file2.s +create file3.s from file2.s +create file4.s from file3.s +""") + +test.write('file2.s', 'file2.s\n') + +test.run(arguments = '.', stdout = """scanning file1.s for file2.s +scanning file2.s for file3.s +create file3.s from file2.s +scanning file2.s for file3.s +create file4.s from file3.s +""") + +test.write('file3.s', 'file3.s\n') + +test.run(arguments = '.', stdout = """scanning file1.s for file2.s +scanning file2.s for file3.s +scanning file3.s for file4.s +create file4.s from file3.s +""") + +test.pass_test()