blob: ea7db2d3d88d6148ad5e4c3f8f45497f90e1f2f0 [file] [log] [blame]
Christopher Dunndc0f7362011-06-21 21:18:49 +00001import fnmatch
2import os
3
Christopher Dunn494950a2015-01-24 15:29:52 -06004def generate(env):
5 def Glob(env, includes = None, excludes = None, dir = '.'):
6 """Adds Glob(includes = Split('*'), excludes = None, dir = '.')
Christopher Dunndc0f7362011-06-21 21:18:49 +00007 helper function to environment.
8
9 Glob both the file-system files.
10
11 includes: list of file name pattern included in the return list when matched.
12 excludes: list of file name pattern exluced from the return list.
13
14 Example:
Christopher Dunn494950a2015-01-24 15:29:52 -060015 sources = env.Glob(("*.cpp", '*.h'), "~*.cpp", "#src")
Christopher Dunndc0f7362011-06-21 21:18:49 +000016 """
17 def filterFilename(path):
Christopher Dunn494950a2015-01-24 15:29:52 -060018 abs_path = os.path.join(dir, path)
Christopher Dunndc0f7362011-06-21 21:18:49 +000019 if not os.path.isfile(abs_path):
20 return 0
21 fn = os.path.basename(path)
22 match = 0
23 for include in includes:
Christopher Dunn494950a2015-01-24 15:29:52 -060024 if fnmatch.fnmatchcase(fn, include):
Christopher Dunndc0f7362011-06-21 21:18:49 +000025 match = 1
26 break
27 if match == 1 and not excludes is None:
28 for exclude in excludes:
Christopher Dunn494950a2015-01-24 15:29:52 -060029 if fnmatch.fnmatchcase(fn, exclude):
Christopher Dunndc0f7362011-06-21 21:18:49 +000030 match = 0
31 break
32 return match
33 if includes is None:
34 includes = ('*',)
Christopher Dunn494950a2015-01-24 15:29:52 -060035 elif type(includes) in (type(''), type(u'')):
Christopher Dunndc0f7362011-06-21 21:18:49 +000036 includes = (includes,)
Christopher Dunn494950a2015-01-24 15:29:52 -060037 if type(excludes) in (type(''), type(u'')):
Christopher Dunndc0f7362011-06-21 21:18:49 +000038 excludes = (excludes,)
39 dir = env.Dir(dir).abspath
Christopher Dunn494950a2015-01-24 15:29:52 -060040 paths = os.listdir(dir)
41 def makeAbsFileNode(path):
42 return env.File(os.path.join(dir, path))
43 nodes = filter(filterFilename, paths)
44 return map(makeAbsFileNode, nodes)
Christopher Dunndc0f7362011-06-21 21:18:49 +000045
46 from SCons.Script import Environment
Baptiste Lepilleurf66d3702008-01-20 16:49:53 +000047 Environment.Glob = Glob
Christopher Dunndc0f7362011-06-21 21:18:49 +000048
49def exists(env):
50 """
51 Tool always exists.
52 """
53 return True