diff options
author | Steven Knight <knight@baldmt.com> | 2004-09-24 14:23:40 (GMT) |
---|---|---|
committer | Steven Knight <knight@baldmt.com> | 2004-09-24 14:23:40 (GMT) |
commit | 90c385ff951733c677ec185423f0578b16e761c1 (patch) | |
tree | f139aee5ab2c3a82b3fae7bc57ad63a962a5e0dd /src | |
parent | 0ac77c3737960007f0451959d0f442ca0a9551c8 (diff) | |
download | SCons-90c385ff951733c677ec185423f0578b16e761c1.zip SCons-90c385ff951733c677ec185423f0578b16e761c1.tar.gz SCons-90c385ff951733c677ec185423f0578b16e761c1.tar.bz2 |
Add validator functions for PathOption. (Steve Christensen)
Diffstat (limited to 'src')
-rw-r--r-- | src/CHANGES.txt | 5 | ||||
-rw-r--r-- | src/engine/SCons/Options/PathOption.py | 78 | ||||
-rw-r--r-- | src/engine/SCons/Options/PathOptionTests.py | 155 |
3 files changed, 205 insertions, 33 deletions
diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 61fafae..8e761e9 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -17,7 +17,10 @@ RELEASE 0.97 - XXX From Steve Christensen: - - Handle exceptions from Python functions a build actions. + - Handle exceptions from Python functions as build actions. + + - Add a set of canned PathOption validators: PathExists (the default), + PathIsFile, PathIsDir and PathIsDirCreate. From Eric Frias: diff --git a/src/engine/SCons/Options/PathOption.py b/src/engine/SCons/Options/PathOption.py index cea4750..f407864 100644 --- a/src/engine/SCons/Options/PathOption.py +++ b/src/engine/SCons/Options/PathOption.py @@ -55,31 +55,61 @@ Usage example: __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" -__all__ = ('PathOption',) - import os +import os.path import SCons.Errors -def _validator(key, val, env): - """ - """ - # todo: write validator, check for path - if not os.path.exists(val): - raise SCons.Errors.UserError( - 'Path does not exist for option %s: %s' % (key, val)) - - -def PathOption(key, help, default): - # NB: searchfunc is currenty undocumented and unsupported - """ - The input parameters describe a 'path list' option, thus they - are returned with the correct converter and validator appended. The - result is usable for input to opts.Add() . - - A 'package list' option may either be 'all', 'none' or a list of - package names (seperated by space). - """ - return (key, '%s ( /path/to/%s )' % (help, key), default, - _validator, None) - +class _PathOptionClass: + + def PathIsDir(self, key, val, env): + """Validator to check if Path is a directory.""" + if not os.path.isdir(val): + if os.path.isfile(val): + m = 'Directory path for option %s is a file: %s' + else: + m = 'Directory path for option %s does not exist: %s' + raise SCons.Errors.UserError(m % (key, val)) + + def PathIsDirCreate(self, key, val, env): + """Validator to check if Path is a directory, + creating it if it does not exist.""" + if os.path.isfile(val): + m = 'Path for option %s is a file, not a directory: %s' + raise SCons.Errors.UserError(m % (key, val)) + if not os.path.isdir(val): + os.makedirs(val) + + def PathIsFile(self, key, val, env): + """validator to check if Path is a file""" + if not os.path.isfile(val): + if os.path.isdir(val): + m = 'File path for option %s is a directory: %s' + else: + m = 'File path for option %s does not exist: %s' + raise SCons.Errors.UserError(m % (key, val)) + + def PathExists(self, key, val, env): + """validator to check if Path exists""" + if not os.path.exists(val): + m = 'Path for option %s does not exist: %s' + raise SCons.Errors.UserError(m % (key, val)) + + def __call__(self, key, help, default, validator=None): + # NB: searchfunc is currenty undocumented and unsupported + """ + The input parameters describe a 'path list' option, thus they + are returned with the correct converter and validator appended. The + result is usable for input to opts.Add() . + + A 'package list' option may either be 'all', 'none' or a list of + package names (seperated by space). + + validator is a validator, see this file for examples + """ + if validator is None: + validator = self.PathExists + return (key, '%s ( /path/to/%s )' % (help, key), default, + validator, None) + +PathOption = _PathOptionClass() diff --git a/src/engine/SCons/Options/PathOptionTests.py b/src/engine/SCons/Options/PathOptionTests.py index 358f8e7..9749ee7 100644 --- a/src/engine/SCons/Options/PathOptionTests.py +++ b/src/engine/SCons/Options/PathOptionTests.py @@ -23,6 +23,7 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import os.path import sys import unittest @@ -35,7 +36,9 @@ class PathOptionTestCase(unittest.TestCase): def test_PathOption(self): """Test PathOption creation""" opts = SCons.Options.Options() - opts.Add(SCons.Options.PathOption('test', 'test option help', '/default/path')) + opts.Add(SCons.Options.PathOption('test', + 'test option help', + '/default/path')) o = opts.options[0] assert o.key == 'test', o.key @@ -44,10 +47,124 @@ class PathOptionTestCase(unittest.TestCase): assert not o.validator is None, o.validator assert o.converter is None, o.converter + def test_PathExists(self): + """Test the PathExists validator""" + opts = SCons.Options.Options() + opts.Add(SCons.Options.PathOption('test', + 'test option help', + '/default/path', + SCons.Options.PathOption.PathExists)) + + test = TestCmd.TestCmd(workdir='') + test.write('exists', 'exists\n') + + o = opts.options[0] + + o.validator('X', test.workpath('exists'), {}) + + dne = test.workpath('does_not_exist') + try: + o.validator('X', dne, {}) + except SCons.Errors.UserError, e: + assert str(e) == 'Path for option X does not exist: %s' % dne, e + except: + raise "did not catch expected UserError" + + def test_PathIsDir(self): + """Test the PathIsDir validator""" + opts = SCons.Options.Options() + opts.Add(SCons.Options.PathOption('test', + 'test option help', + '/default/path', + SCons.Options.PathOption.PathIsDir)) + + test = TestCmd.TestCmd(workdir='') + test.subdir('dir') + test.write('file', "file\n") + + o = opts.options[0] + + o.validator('X', test.workpath('dir'), {}) + + f = test.workpath('file') + try: + o.validator('X', f, {}) + except SCons.Errors.UserError, e: + assert str(e) == 'Directory path for option X is a file: %s' % f, e + except: + raise "did not catch expected UserError" + + dne = test.workpath('does_not_exist') + try: + o.validator('X', dne, {}) + except SCons.Errors.UserError, e: + assert str(e) == 'Directory path for option X does not exist: %s' % dne, e + except: + raise "did not catch expected UserError" + + def test_PathIsDirCreate(self): + """Test the PathIsDirCreate validator""" + opts = SCons.Options.Options() + opts.Add(SCons.Options.PathOption('test', + 'test option help', + '/default/path', + SCons.Options.PathOption.PathIsDirCreate)) + + test = TestCmd.TestCmd(workdir='') + test.write('file', "file\n") + + o = opts.options[0] + + d = test.workpath('dir') + o.validator('X', d, {}) + assert os.path.isdir(d) + + f = test.workpath('file') + try: + o.validator('X', f, {}) + except SCons.Errors.UserError, e: + assert str(e) == 'Path for option X is a file, not a directory: %s' % f, e + except: + raise "did not catch expected UserError" + + def test_PathIsFile(self): + """Test the PathIsFile validator""" + opts = SCons.Options.Options() + opts.Add(SCons.Options.PathOption('test', + 'test option help', + '/default/path', + SCons.Options.PathOption.PathIsFile)) + + test = TestCmd.TestCmd(workdir='') + test.subdir('dir') + test.write('file', "file\n") + + o = opts.options[0] + + o.validator('X', test.workpath('file'), {}) + + d = test.workpath('d') + try: + o.validator('X', d, {}) + except SCons.Errors.UserError, e: + assert str(e) == 'File path for option X does not exist: %s' % d, e + except: + raise "did not catch expected UserError" + + dne = test.workpath('does_not_exist') + try: + o.validator('X', dne, {}) + except SCons.Errors.UserError, e: + assert str(e) == 'File path for option X does not exist: %s' % dne, e + except: + raise "did not catch expected UserError" + def test_validator(self): - """Test the PathOption validator""" + """Test the PathOption validator argument""" opts = SCons.Options.Options() - opts.Add(SCons.Options.PathOption('test', 'test option help', '/default/path')) + opts.Add(SCons.Options.PathOption('test', + 'test option help', + '/default/path')) test = TestCmd.TestCmd(workdir='') test.write('exists', 'exists\n') @@ -56,12 +173,34 @@ class PathOptionTestCase(unittest.TestCase): o.validator('X', test.workpath('exists'), {}) - caught = None + dne = test.workpath('does_not_exist') + try: + o.validator('X', dne, {}) + except SCons.Errors.UserError, e: + expect = 'Path for option X does not exist: %s' % dne + assert str(e) == expect, e + else: + raise "did not catch expected UserError" + + def my_validator(key, val, env): + raise Exception, "my_validator() got called for %s, %s!" % (key, val) + + opts = SCons.Options.Options() + opts.Add(SCons.Options.PathOption('test2', + 'more help', + '/default/path/again', + my_validator)) + + o = opts.options[0] + try: - o.validator('X', test.workpath('does_not_exist'), {}) - except SCons.Errors.UserError: - caught = 1 - assert caught, "did not catch expected UserError" + o.validator('Y', 'value', {}) + except Exception, e: + assert str(e) == 'my_validator() got called for Y, value!', e + else: + raise "did not catch expected exception from my_validator()" + + if __name__ == "__main__": |