Sam Clegg | 6386061 | 2015-04-09 18:01:33 -0700 | [diff] [blame] | 1 | # Copyright 2009 Baptiste Lepilleur |
| 2 | # Distributed under MIT license, or public domain if desired and |
| 3 | # recognized in your jurisdiction. |
| 4 | # See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
| 5 | |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 6 | import fnmatch |
| 7 | import os |
| 8 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 9 | def generate(env): |
| 10 | def Glob(env, includes = None, excludes = None, dir = '.'): |
| 11 | """Adds Glob(includes = Split('*'), excludes = None, dir = '.') |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 12 | helper function to environment. |
| 13 | |
| 14 | Glob both the file-system files. |
| 15 | |
| 16 | includes: list of file name pattern included in the return list when matched. |
| 17 | excludes: list of file name pattern exluced from the return list. |
| 18 | |
| 19 | Example: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 20 | sources = env.Glob(("*.cpp", '*.h'), "~*.cpp", "#src") |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 21 | """ |
| 22 | def filterFilename(path): |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 23 | abs_path = os.path.join(dir, path) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 24 | if not os.path.isfile(abs_path): |
| 25 | return 0 |
| 26 | fn = os.path.basename(path) |
| 27 | match = 0 |
| 28 | for include in includes: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 29 | if fnmatch.fnmatchcase(fn, include): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 30 | match = 1 |
| 31 | break |
| 32 | if match == 1 and not excludes is None: |
| 33 | for exclude in excludes: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 34 | if fnmatch.fnmatchcase(fn, exclude): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 35 | match = 0 |
| 36 | break |
| 37 | return match |
| 38 | if includes is None: |
| 39 | includes = ('*',) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 40 | elif type(includes) in (type(''), type(u'')): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 41 | includes = (includes,) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 42 | if type(excludes) in (type(''), type(u'')): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 43 | excludes = (excludes,) |
| 44 | dir = env.Dir(dir).abspath |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 45 | paths = os.listdir(dir) |
| 46 | def makeAbsFileNode(path): |
| 47 | return env.File(os.path.join(dir, path)) |
| 48 | nodes = filter(filterFilename, paths) |
| 49 | return map(makeAbsFileNode, nodes) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 50 | |
| 51 | from SCons.Script import Environment |
Baptiste Lepilleur | f66d370 | 2008-01-20 16:49:53 +0000 | [diff] [blame] | 52 | Environment.Glob = Glob |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 53 | |
| 54 | def exists(env): |
| 55 | """ |
| 56 | Tool always exists. |
| 57 | """ |
| 58 | return True |