PathVariable.py (scons-4.2.0) | : | PathVariable.py (SCons-4.3.0) | ||
---|---|---|---|---|
skipping to change at line 24 | skipping to change at line 24 | |||
# in all copies or substantial portions of the Software. | # in all copies or substantial portions of the Software. | |||
# | # | |||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY | |||
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |||
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |||
"""Option type for path Variables. | """Variable type for path Variables. | |||
This file defines an option type for SCons implementing path settings. | To be used whenever a user-specified path override setting should be allowed. | |||
To be used whenever a user-specified path override should be allowed. | ||||
Arguments to PathVariable are: | Arguments to PathVariable are: | |||
option-name = name of this option on the command line (e.g. "prefix") | * *key* - name of this option on the command line (e.g. "prefix") | |||
option-help = help string for option | * *help* - help string for option | |||
option-dflt = default value for this option | * *default* - default value for this option | |||
validator = [optional] validator for option value. Predefined are: | * *validator* - [optional] validator for option value. Predefined are: | |||
PathAccept -- accepts any path setting; no validation | ||||
PathIsDir -- path must be an existing directory | * *PathAccept* - accepts any path setting; no validation | |||
PathIsDirCreate -- path must be a dir; will create | * *PathIsDir* - path must be an existing directory | |||
PathIsFile -- path must be a file | * *PathIsDirCreate* - path must be a dir; will create | |||
PathExists -- path must exist (any type) [default] | * *PathIsFile* - path must be a file | |||
* *PathExists* - path must exist (any type) [default] | ||||
The validator is a function that is called and which should return | The *validator* is a function that is called and which should return | |||
True or False to indicate if the path is valid. The arguments | True or False to indicate if the path is valid. The arguments | |||
to the validator function are: (key, val, env). The key is the | to the validator function are: (*key*, *val*, *env*). *key* is the | |||
name of the option, the val is the path specified for the option, | name of the option, *val* is the path specified for the option, | |||
and the env is the env to which the Options have been added. | and *env* is the environment to which the Options have been added. | |||
Usage example:: | Usage example:: | |||
Examples: | opts = Variables() | |||
prefix=/usr/local | opts.Add( | |||
PathVariable( | ||||
opts = Variables() | 'qtdir', | |||
help='where the root of Qt is installed', | ||||
opts = Variables() | default=qtdir, | |||
opts.Add(PathVariable('qtdir', | validator=PathIsDir, | |||
'where the root of Qt is installed', | ) | |||
qtdir, PathIsDir)) | ) | |||
opts.Add(PathVariable('qt_includes', | opts.Add( | |||
'where the Qt includes are installed', | PathVariable( | |||
'$qtdir/includes', PathIsDirCreate)) | 'qt_includes', | |||
opts.Add(PathVariable('qt_libraries', | help='where the Qt includes are installed', | |||
'where the Qt library is installed', | default='$qtdir/includes', | |||
'$qtdir/lib')) | validator=PathIsDirCreate, | |||
) | ||||
) | ||||
opts.Add( | ||||
PathVariable( | ||||
'qt_libraries', | ||||
help='where the Qt library is installed', | ||||
default='$qtdir/lib', | ||||
) | ||||
) | ||||
""" | """ | |||
__all__ = ['PathVariable',] | ||||
import os | import os | |||
import os.path | import os.path | |||
from typing import Tuple, Callable | ||||
import SCons.Errors | import SCons.Errors | |||
__all__ = ['PathVariable',] | ||||
class _PathVariableClass: | class _PathVariableClass: | |||
@staticmethod | @staticmethod | |||
def PathAccept(key, val, env): | def PathAccept(key, val, env) -> None: | |||
"""Accepts any path, no checking done.""" | """Accepts any path, no checking done.""" | |||
pass | pass | |||
@staticmethod | @staticmethod | |||
def PathIsDir(key, val, env): | def PathIsDir(key, val, env) -> None: | |||
"""Validator to check if Path is a directory.""" | """Validator to check if Path is a directory.""" | |||
if not os.path.isdir(val): | if not os.path.isdir(val): | |||
if os.path.isfile(val): | if os.path.isfile(val): | |||
m = 'Directory path for option %s is a file: %s' | m = 'Directory path for option %s is a file: %s' | |||
else: | else: | |||
m = 'Directory path for option %s does not exist: %s' | m = 'Directory path for option %s does not exist: %s' | |||
raise SCons.Errors.UserError(m % (key, val)) | raise SCons.Errors.UserError(m % (key, val)) | |||
@staticmethod | @staticmethod | |||
def PathIsDirCreate(key, val, env): | def PathIsDirCreate(key, val, env) -> None: | |||
"""Validator to check if Path is a directory, | """Validator to check if Path is a directory, | |||
creating it if it does not exist.""" | creating it if it does not exist.""" | |||
if os.path.isfile(val): | try: | |||
os.makedirs(val, exist_ok=True) | ||||
except FileExistsError: | ||||
m = 'Path for option %s is a file, not a directory: %s' | m = 'Path for option %s is a file, not a directory: %s' | |||
raise SCons.Errors.UserError(m % (key, val)) | raise SCons.Errors.UserError(m % (key, val)) | |||
if not os.path.isdir(val): | except PermissionError: | |||
os.makedirs(val) | m = 'Path for option %s could not be created: %s' | |||
raise SCons.Errors.UserError(m % (key, val)) | ||||
@staticmethod | @staticmethod | |||
def PathIsFile(key, val, env): | def PathIsFile(key, val, env) -> None: | |||
"""Validator to check if Path is a file""" | """Validator to check if Path is a file""" | |||
if not os.path.isfile(val): | if not os.path.isfile(val): | |||
if os.path.isdir(val): | if os.path.isdir(val): | |||
m = 'File path for option %s is a directory: %s' | m = 'File path for option %s is a directory: %s' | |||
else: | else: | |||
m = 'File path for option %s does not exist: %s' | m = 'File path for option %s does not exist: %s' | |||
raise SCons.Errors.UserError(m % (key, val)) | raise SCons.Errors.UserError(m % (key, val)) | |||
@staticmethod | @staticmethod | |||
def PathExists(key, val, env): | def PathExists(key, val, env) -> None: | |||
"""Validator to check if Path exists""" | """Validator to check if Path exists""" | |||
if not os.path.exists(val): | if not os.path.exists(val): | |||
m = 'Path for option %s does not exist: %s' | m = 'Path for option %s does not exist: %s' | |||
raise SCons.Errors.UserError(m % (key, val)) | raise SCons.Errors.UserError(m % (key, val)) | |||
def __call__(self, key, help, default, validator=None): | def __call__(self, key, help, default, validator=None) -> Tuple[str, str, st | |||
""" | r, Callable, None]: | |||
The input parameters describe a 'path list' option, thus they | """Return a tuple describing a path list SCons Variable. | |||
are returned with the correct converter and validator appended. The | ||||
result is usable for input to opts.Add() . | The input parameters describe a 'path list' option. Returns | |||
a tuple with the correct converter and validator appended. The | ||||
result is usable for input to :meth:`Add`. | ||||
The 'default' option specifies the default path to use if the | The *default* option specifies the default path to use if the | |||
user does not specify an override with this option. | user does not specify an override with this option. | |||
validator is a validator, see this file for examples | *validator* is a validator, see this file for examples | |||
""" | """ | |||
if validator is None: | if validator is None: | |||
validator = self.PathExists | validator = self.PathExists | |||
if SCons.Util.is_List(key) or SCons.Util.is_Tuple(key): | if SCons.Util.is_List(key) or SCons.Util.is_Tuple(key): | |||
return (key, '%s ( /path/to/%s )' % (help, key[0]), default, | helpmsg = '%s ( /path/to/%s )' % (help, key[0]) | |||
validator, None) | ||||
else: | else: | |||
return (key, '%s ( /path/to/%s )' % (help, key), default, | helpmsg = '%s ( /path/to/%s )' % (help, key) | |||
validator, None) | return (key, helpmsg, default, validator, None) | |||
PathVariable = _PathVariableClass() | PathVariable = _PathVariableClass() | |||
# Local Variables: | # Local Variables: | |||
# tab-width:4 | # tab-width:4 | |||
# indent-tabs-mode:nil | # indent-tabs-mode:nil | |||
# End: | # End: | |||
# vim: set expandtab tabstop=4 shiftwidth=4: | # vim: set expandtab tabstop=4 shiftwidth=4: | |||
End of changes. 21 change blocks. | ||||
53 lines changed or deleted | 66 lines changed or added |