Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 1 | import fnmatch |
| 2 | import os |
| 3 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 4 | def generate(env): |
| 5 | def Glob(env, includes = None, excludes = None, dir = '.'): |
| 6 | """Adds Glob(includes = Split('*'), excludes = None, dir = '.') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 7 | 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 Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 15 | sources = env.Glob(("*.cpp", '*.h'), "~*.cpp", "#src") |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 16 | """ |
| 17 | def filterFilename(path): |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 18 | abs_path = os.path.join(dir, path) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 19 | 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 Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 24 | if fnmatch.fnmatchcase(fn, include): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 25 | match = 1 |
| 26 | break |
| 27 | if match == 1 and not excludes is None: |
| 28 | for exclude in excludes: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 29 | if fnmatch.fnmatchcase(fn, exclude): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 30 | match = 0 |
| 31 | break |
| 32 | return match |
| 33 | if includes is None: |
| 34 | includes = ('*',) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 35 | elif type(includes) in (type(''), type(u'')): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 36 | includes = (includes,) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 37 | if type(excludes) in (type(''), type(u'')): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 38 | excludes = (excludes,) |
| 39 | dir = env.Dir(dir).abspath |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 40 | 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 Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 45 | |
| 46 | from SCons.Script import Environment |
Baptiste Lepilleur | f66d370 | 2008-01-20 16:49:53 +0000 | [diff] [blame] | 47 | Environment.Glob = Glob |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 48 | |
| 49 | def exists(env): |
| 50 | """ |
| 51 | Tool always exists. |
| 52 | """ |
| 53 | return True |