From ae1e7c742dcd1ed0b521640455794172fe275c65 Mon Sep 17 00:00:00 2001 From: Steven Knight Date: Sun, 18 Apr 2010 14:03:36 +0000 Subject: Replace remaining os.path.walk() calls with os.walk(). --- QMTest/TestCmd.py | 73 ++++++++++++++------------------------- QMTest/TestSCons.py | 9 +++++ runtest.py | 73 ++++++++++++++++++++++----------------- src/engine/SCons/Tool/javac.py | 14 ++++---- src/engine/SCons/Tool/zip.py | 11 +++--- src/script/scons-time.py | 7 ++-- src/test_strings.py | 23 ++++++------ test/Java/Java-1.4.py | 12 +------ test/Java/Java-1.5.py | 12 +------ test/Java/Java-1.6.py | 12 +------ test/Parallel/duplicate-target.py | 20 ++++++----- 11 files changed, 117 insertions(+), 149 deletions(-) diff --git a/QMTest/TestCmd.py b/QMTest/TestCmd.py index 1dff4a6..ab1c212 100644 --- a/QMTest/TestCmd.py +++ b/QMTest/TestCmd.py @@ -221,7 +221,6 @@ __version__ = "0.37" import errno import os -import os.path import re import shutil import stat @@ -317,13 +316,6 @@ except ImportError: else: atexit.register(_clean) -class Collector: - def __init__(self, top): - self.entries = [top] - def __call__(self, arg, dirname, names): - pathjoin = lambda n: os.path.join(dirname, n) - self.entries.extend(list(map(pathjoin, names))) - def _caller(tblist, skip): string = "" arr = [] @@ -1461,29 +1453,21 @@ class TestCmd(object): # It's a directory and we're trying to turn on read # permission, so it's also pretty easy, just chmod the # directory and then chmod every entry on our walk down the - # tree. Because os.path.walk() is top-down, we'll enable - # read permission on any directories that have it disabled - # before os.path.walk() tries to list their contents. - do_chmod(top) - - def chmod_entries(arg, dirname, names, do_chmod=do_chmod): - for n in names: - do_chmod(os.path.join(dirname, n)) - - os.path.walk(top, chmod_entries, None) + # tree. + for dirpath, dirnames, filenames in os.walk(top): + do_chmod(dirpath) + for fn in filenames: + do_chmod(os.path.join(dirpath, fn)) else: # It's a directory and we're trying to turn off read - # permission, which means we have to chmod the directoreis + # permission, which means we have to chmod the directories # in the tree bottom-up, lest disabling read permission from # the top down get in the way of being able to get at lower - # parts of the tree. But os.path.walk() visits things top - # down, so we just use an object to collect a list of all - # of the entries in the tree, reverse the list, and then - # chmod the reversed (bottom-up) list. - col = Collector(top) - os.path.walk(top, col, None) - col.entries.reverse() - for d in col.entries: do_chmod(d) + # parts of the tree. + for dirpath, dirnames, filenames in os.walk(top, topdown=0): + for fn in filenames: + do_chmod(os.path.join(dirpath, fn)) + do_chmod(dirpath) def writable(self, top, write=1): """Make the specified directory tree writable (write == 1) @@ -1517,9 +1501,10 @@ class TestCmd(object): if os.path.isfile(top): do_chmod(top) else: - col = Collector(top) - os.path.walk(top, col, None) - for d in col.entries: do_chmod(d) + for dirpath, dirnames, filenames in os.walk(top, topdown=0): + for fn in filenames: + do_chmod(os.path.join(dirpath, fn)) + do_chmod(dirpath) def executable(self, top, execute=1): """Make the specified directory tree executable (execute == 1) @@ -1550,29 +1535,21 @@ class TestCmd(object): # It's a directory and we're trying to turn on execute # permission, so it's also pretty easy, just chmod the # directory and then chmod every entry on our walk down the - # tree. Because os.path.walk() is top-down, we'll enable - # execute permission on any directories that have it disabled - # before os.path.walk() tries to list their contents. - do_chmod(top) - - def chmod_entries(arg, dirname, names, do_chmod=do_chmod): - for n in names: - do_chmod(os.path.join(dirname, n)) - - os.path.walk(top, chmod_entries, None) + # tree. + for dirpath, dirnames, filenames in os.walk(top): + do_chmod(dirpath) + for fn in filenames: + do_chmod(os.path.join(dirpath, fn)) else: # It's a directory and we're trying to turn off execute # permission, which means we have to chmod the directories # in the tree bottom-up, lest disabling execute permission from # the top down get in the way of being able to get at lower - # parts of the tree. But os.path.walk() visits things top - # down, so we just use an object to collect a list of all - # of the entries in the tree, reverse the list, and then - # chmod the reversed (bottom-up) list. - col = Collector(top) - os.path.walk(top, col, None) - col.entries.reverse() - for d in col.entries: do_chmod(d) + # parts of the tree. + for dirpath, dirnames, filenames in os.walk(top, topdown=0): + for fn in filenames: + do_chmod(os.path.join(dirpath, fn)) + do_chmod(dirpath) def write(self, file, content, mode = 'wb'): """Writes the specified content text (second argument) to the diff --git a/QMTest/TestSCons.py b/QMTest/TestSCons.py index 561dcda..307c118 100644 --- a/QMTest/TestSCons.py +++ b/QMTest/TestSCons.py @@ -650,6 +650,15 @@ class TestSCons(TestCommon): self.skip_test("Could not find Java rmic, skipping non-simulated test(s).\n") return where_rmic + def java_get_class_files(self, dir): + result = [] + for dirpath, dirnames, filenames in os.walk(dir): + for fname in filenames: + if fname.endswith('.class'): + result.append(os.path.join(dirpath, fname)) + return sorted(result) + + def Qt_dummy_installation(self, dir='qt'): # create a dummy qt installation diff --git a/runtest.py b/runtest.py index 11f87e1..031a1f6 100644 --- a/runtest.py +++ b/runtest.py @@ -94,6 +94,14 @@ import stat import sys import time +try: + x = True +except NameError: + True = not 0 + False = not 1 +else: + del x + if not hasattr(os, 'WEXITSTATUS'): os.WEXITSTATUS = lambda x: x @@ -604,24 +612,28 @@ if old_pythonpath: tests = [] -def find_Tests_py(tdict, dirname, names): - for n in [n for n in names if n[-8:] == "Tests.py"]: - tdict[os.path.join(dirname, n)] = 1 - -def find_py(tdict, dirname, names): - tests = [n for n in names if n[-3:] == ".py"] - try: - excludes = open(os.path.join(dirname,".exclude_tests")).readlines() - except (OSError, IOError): - pass - else: - for exclude in excludes: - exclude = exclude.split('#' , 1)[0] - exclude = exclude.strip() - if not exclude: continue - tests = [n for n in tests if n != exclude] - for n in tests: - tdict[os.path.join(dirname, n)] = 1 +def find_Tests_py(directory): + result = [] + for dirpath, dirnames, filenames in os.walk(directory): + for fname in filenames: + if fname.endswith("Tests.py"): + result.append(os.path.join(dirpath, fname)) + return sorted(result) + +def find_py(directory): + result = [] + for dirpath, dirnames, filenames in os.walk(directory): + try: + exclude_fp = open(os.path.join(dirpath, ".exclude_tests")) + except EnvironmentError: + excludes = [] + else: + excludes = [ e.split('#', 1)[0].strip() + for e in exclude_fp.readlines() ] + for fname in filenames: + if fname.endswith(".py") and fname not in excludes: + result.append(os.path.join(dirpath, fname)) + return sorted(result) if args: if spe: @@ -639,12 +651,11 @@ if args: for a in args: for path in glob.glob(a): if os.path.isdir(path): - tdict = {} if path[:3] == 'src': - os.path.walk(path, find_Tests_py, tdict) + tests.extend(find_Tests_py(path)) + elif path[:4] == 'test': - os.path.walk(path, find_py, tdict) - tests.extend(sorted(tdict.keys())) + tests.extend(find_py(path)) else: tests.append(path) elif testlistfile: @@ -663,25 +674,23 @@ elif all and not qmtest: # still be executed by hand, though, and are routinely executed # by the Aegis packaging build to make sure that we're building # things correctly.) - tdict = {} - os.path.walk('src', find_Tests_py, tdict) - os.path.walk('test', find_py, tdict) + tests.extend(find_Tests_py('src')) + tests.extend(find_py('test')) if format == '--aegis' and aegis: cmd = "aegis -list -unf pf 2>/dev/null" for line in os.popen(cmd, "r").readlines(): a = line.split() - if a[0] == "test" and a[-1] not in tdict: - tdict[a[-1]] = Test(a[-1], spe) + if a[0] == "test" and a[-1] not in tests: + tests.append(Test(a[-1], spe)) cmd = "aegis -list -unf cf 2>/dev/null" for line in os.popen(cmd, "r").readlines(): a = line.split() if a[0] == "test": if a[1] == "remove": - del tdict[a[-1]] - elif a[-1] not in tdict: - tdict[a[-1]] = Test(a[-1], spe) - - tests = sorted(tdict.keys()) + tests.remove(a[-1]) + elif a[-1] not in tests: + tests.append(Test(a[-1], spe)) + tests.sort() if qmtest: if baseline: diff --git a/src/engine/SCons/Tool/javac.py b/src/engine/SCons/Tool/javac.py index ef4bd5b..6cc0779 100644 --- a/src/engine/SCons/Tool/javac.py +++ b/src/engine/SCons/Tool/javac.py @@ -73,15 +73,17 @@ def emit_java_classes(target, source, env): slist.append(entry) elif isinstance(entry, SCons.Node.FS.Dir): result = SCons.Util.OrderedDict() - def visit(arg, dirname, names, dirnode=entry.rdir()): - java_files = sorted([n for n in names if _my_normcase(n[-len(js):]) == js]) - mydir = dirnode.Dir(dirname) + dirnode = entry.rdir() + def find_java_files(arg, dirpath, filenames): + java_files = sorted([n for n in filenames + if _my_normcase(n).endswith(js)]) + mydir = dirnode.Dir(dirpath) java_paths = [mydir.File(f) for f in java_files] for jp in java_paths: arg[jp] = True - - os.path.walk(entry.rdir().get_abspath(), visit, result) - entry.walk(visit, result) + for dirpath, dirnames, filenames in os.walk(dirnode.get_abspath()): + find_java_files(result, dirpath, filenames) + entry.walk(find_java_files, result) slist.extend(result.keys()) else: diff --git a/src/engine/SCons/Tool/zip.py b/src/engine/SCons/Tool/zip.py index ce0b2fc..8e6028e 100644 --- a/src/engine/SCons/Tool/zip.py +++ b/src/engine/SCons/Tool/zip.py @@ -49,16 +49,15 @@ except ImportError: if internal_zip: zipcompression = zipfile.ZIP_DEFLATED def zip(target, source, env): - def visit(arg, dirname, names): - for name in names: - path = os.path.join(dirname, name) - if os.path.isfile(path): - arg.write(path) compression = env.get('ZIPCOMPRESSION', 0) zf = zipfile.ZipFile(str(target[0]), 'w', compression) for s in source: if s.isdir(): - os.path.walk(str(s), visit, zf) + for dirpath, dirnames, filenames in os.walk(str(s)): + for fname in filenames: + path = os.path.join(dirpath, fname) + if os.path.isfile(path): + zf.write(path) else: zf.write(str(s)) zf.close() diff --git a/src/script/scons-time.py b/src/script/scons-time.py index 5f3d515..6b52554 100644 --- a/src/script/scons-time.py +++ b/src/script/scons-time.py @@ -303,12 +303,11 @@ def unzip(fname): open(name, 'w').write(zf.read(name)) def read_tree(dir): - def read_files(arg, dirname, fnames): - for fn in fnames: - fn = os.path.join(dirname, fn) + for dirpath, dirnames, filenames in os.walk(dir): + for fn in filenames: + fn = os.path.join(dirpath, fn) if os.path.isfile(fn): open(fn, 'rb').read() - os.path.walk('.', read_files, None) def redirect_to_file(command, log): return '%s > %s 2>&1' % (command, log) diff --git a/src/test_strings.py b/src/test_strings.py index c6bf75d..9aaad08 100644 --- a/src/test_strings.py +++ b/src/test_strings.py @@ -85,20 +85,19 @@ class Checker: else: return os.path.isfile(path) - def visit(self, result, dirname, names): - for name, path in [(n, os.path.join(dirname, n)) for n in names]: - if self.remove_this(name, path): - names.remove(name) - elif self.search_this(path): - body = open(path, 'r').read() - for expr in self.expressions: - if not expr.search(body): - msg = '%s: missing %s' % (path, repr(expr.pattern)) - result.append(msg) - def find_missing(self): result = [] - os.path.walk(self.directory, self.visit, result) + for dirpath, dirnames, filenames in os.walk(self.directory): + for fname in filenames: + fpath = os.path.join(dirpath, fname) + if self.remove_this(fname, fpath): + result.remove(fname) + elif self.search_this(fpath): + body = open(path, 'r').read() + for expr in self.expressions: + if not expr.search(body): + msg = '%s: missing %s' % (path, repr(expr.pattern)) + result.append(msg) return result class CheckUnexpandedStrings(Checker): diff --git a/test/Java/Java-1.4.py b/test/Java/Java-1.4.py index 9cc1d96..c84a2f0 100644 --- a/test/Java/Java-1.4.py +++ b/test/Java/Java-1.4.py @@ -334,19 +334,9 @@ expect_6 = [ failed = None -def get_class_files(dir): - def find_class_files(arg, dirname, fnames): - for fname in fnames: - if fname[-6:] == '.class': - arg.append(os.path.join(dirname, fname)) - result = [] - os.path.walk(dir, find_class_files, result) - result.sort() - return result - def classes_must_match(dir, expect): global failed - got = get_class_files(test.workpath(dir)) + got = test.java_get_class_files(test.workpath(dir)) if expect != got: missing = set(expect) - set(got) if missing: diff --git a/test/Java/Java-1.5.py b/test/Java/Java-1.5.py index ca9cbce..6c4ea46 100644 --- a/test/Java/Java-1.5.py +++ b/test/Java/Java-1.5.py @@ -334,19 +334,9 @@ expect_6 = [ failed = None -def get_class_files(dir): - def find_class_files(arg, dirname, fnames): - for fname in fnames: - if fname[-6:] == '.class': - arg.append(os.path.join(dirname, fname)) - result = [] - os.path.walk(dir, find_class_files, result) - result.sort() - return result - def classes_must_match(dir, expect): global failed - got = get_class_files(test.workpath(dir)) + got = test.java_get_class_files(test.workpath(dir)) if expect != got: sys.stderr.write("Expected the following class files in '%s':\n" % dir) for c in expect: diff --git a/test/Java/Java-1.6.py b/test/Java/Java-1.6.py index ec6df54..d34fcca 100644 --- a/test/Java/Java-1.6.py +++ b/test/Java/Java-1.6.py @@ -334,19 +334,9 @@ expect_6 = [ failed = None -def get_class_files(dir): - def find_class_files(arg, dirname, fnames): - for fname in fnames: - if fname[-6:] == '.class': - arg.append(os.path.join(dirname, fname)) - result = [] - os.path.walk(dir, find_class_files, result) - result.sort() - return result - def classes_must_match(dir, expect): global failed - got = get_class_files(test.workpath(dir)) + got = test.java_get_class_files(test.workpath(dir)) if expect != got: sys.stderr.write("Expected the following class files in '%s':\n" % dir) for c in expect: diff --git a/test/Parallel/duplicate-target.py b/test/Parallel/duplicate-target.py index a9d511e..efe20d9 100644 --- a/test/Parallel/duplicate-target.py +++ b/test/Parallel/duplicate-target.py @@ -56,16 +56,20 @@ test.write(['work', 'mytar.py'], """\ import sys import os.path -def visit(arg, dirname, fnames): - fnames.sort() - for fn in fnames: - p = os.path.join(dirname, fn) - if os.path.isfile(p): - arg.write(open(p, 'rb').read()) - fp = open(sys.argv[1], 'wb') + +def visit(dirname): + names = os.listdir(dirname) + names.sort() + for n in names: + p = os.path.join(dirname, n) + if os.path.isdir(p): + visit(p) + elif os.path.isfile(p): + fp.write(open(p, 'rb').read()) + for s in sys.argv[2:]: - os.path.walk(s, visit, fp) + visit(s) """) test.write(['work', 'SConstruct'], """\ -- cgit v0.12