您的方法可以进行如下微调:
import fnmatch
import os
def RecursiveGlob(pathname)
matches = []
for root, dirnames, filenames in os.walk(pathname):
for filename in fnmatch.filter(filenames, '*.c'):
matches.append(File(os.path.join(root, filename)))
return matches
请注意,我将其转换为 File(),因为如果“strings”参数为 false,SCons Glob() 函数将返回 Nodes。
为了能够处理 VariantDir 等并更好地将功能与现有 SCons Glob() 功能集成,您实际上可以合并对现有 Glob() 函数的调用,如下所示:
# Notice the signature is similar to the SCons Glob() signature,
# look at scons-2.1.0/engine/SCons/Node/FS.py line 1403
def RecursiveGlob(pattern, ondisk=True, source=True, strings=False):
matches = []
# Instead of using os.getcwd() consider passing-in a path
for root, dirnames, filenames in os.walk(os.getcwd()):
cwd = Dir(root)
# Glob() returns a list, so using extend() instead of append()
# The cwd param isnt documented, (look at the code) but its
# how you tell SCons what directory to look in.
matches.extend(Glob(pattern, ondisk, source, strings, cwd))
return matches
您可以更进一步并执行以下操作:
def MyGlob(pattern, ondisk=True, source=True, strings=False, recursive=False):
if not recursive:
return Glob(pattern, ondisk, source, strings)
matches = []
# Instead of using os.getcwd() consider passing-in a path
for root, dirnames, filenames in os.walk(os.getcwd()):
cwd = Dir(root)
# Glob() returns a list, so using extend() instead of append()
# The cwd param isnt documented, (look at the code) but its
# how you tell SCons what directory to look in.
matches.extend(Glob(pattern, ondisk, source, strings, cwd))
return matches