summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Options/PathOption.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/SCons/Options/PathOption.py')
-rw-r--r--src/engine/SCons/Options/PathOption.py78
1 files changed, 54 insertions, 24 deletions
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()