Package SCons :: Package Variables :: Module PathVariable'
[hide private]
[frames] | no frames]

Source Code for Module SCons.Variables.PathVariable'

  1  """SCons.Variables.PathVariable 
  2   
  3  This file defines an option type for SCons implementing path settings. 
  4   
  5  To be used whenever a user-specified path override should be allowed. 
  6   
  7  Arguments to PathVariable are: 
  8    option-name  = name of this option on the command line (e.g. "prefix") 
  9    option-help  = help string for option 
 10    option-dflt  = default value for this option 
 11    validator    = [optional] validator for option value.  Predefined validators are: 
 12   
 13                       PathAccept -- accepts any path setting; no validation 
 14                       PathIsDir  -- path must be an existing directory 
 15                       PathIsDirCreate -- path must be a dir; will create 
 16                       PathIsFile -- path must be a file 
 17                       PathExists -- path must exist (any type) [default] 
 18   
 19                   The validator is a function that is called and which 
 20                   should return True or False to indicate if the path 
 21                   is valid.  The arguments to the validator function 
 22                   are: (key, val, env).  The key is the name of the 
 23                   option, the val is the path specified for the option, 
 24                   and the env is the env to which the Options have been 
 25                   added. 
 26   
 27  Usage example:: 
 28   
 29    Examples: 
 30        prefix=/usr/local 
 31   
 32    opts = Variables() 
 33   
 34    opts = Variables() 
 35    opts.Add(PathVariable('qtdir', 
 36                          'where the root of Qt is installed', 
 37                          qtdir, PathIsDir)) 
 38    opts.Add(PathVariable('qt_includes', 
 39                        'where the Qt includes are installed', 
 40                        '$qtdir/includes', PathIsDirCreate)) 
 41    opts.Add(PathVariable('qt_libraries', 
 42                        'where the Qt library is installed', 
 43                        '$qtdir/lib')) 
 44   
 45  """ 
 46   
 47  # 
 48  # Copyright (c) 2001 - 2019 The SCons Foundation 
 49  # 
 50  # Permission is hereby granted, free of charge, to any person obtaining 
 51  # a copy of this software and associated documentation files (the 
 52  # "Software"), to deal in the Software without restriction, including 
 53  # without limitation the rights to use, copy, modify, merge, publish, 
 54  # distribute, sublicense, and/or sell copies of the Software, and to 
 55  # permit persons to whom the Software is furnished to do so, subject to 
 56  # the following conditions: 
 57  # 
 58  # The above copyright notice and this permission notice shall be included 
 59  # in all copies or substantial portions of the Software. 
 60  # 
 61  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
 62  # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
 63  # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 64  # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 65  # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
 66  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 67  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 68  # 
 69   
 70  __revision__ = "src/engine/SCons/Variables/PathVariable.py 3a41ed6b288cee8d085373ad7fa02894e1903864 2019-01-23 17:30:35 bdeegan" 
 71   
 72  __all__ = ['PathVariable',] 
 73   
 74  import os 
 75  import os.path 
 76   
 77  import SCons.Errors 
 78   
79 -class _PathVariableClass(object):
80
81 - def PathAccept(self, key, val, env):
82 """Accepts any path, no checking done.""" 83 pass
84
85 - def PathIsDir(self, key, val, env):
86 """Validator to check if Path is a directory.""" 87 if not os.path.isdir(val): 88 if os.path.isfile(val): 89 m = 'Directory path for option %s is a file: %s' 90 else: 91 m = 'Directory path for option %s does not exist: %s' 92 raise SCons.Errors.UserError(m % (key, val))
93
94 - def PathIsDirCreate(self, key, val, env):
95 """Validator to check if Path is a directory, 96 creating it if it does not exist.""" 97 if os.path.isfile(val): 98 m = 'Path for option %s is a file, not a directory: %s' 99 raise SCons.Errors.UserError(m % (key, val)) 100 if not os.path.isdir(val): 101 os.makedirs(val)
102
103 - def PathIsFile(self, key, val, env):
104 """Validator to check if Path is a file""" 105 if not os.path.isfile(val): 106 if os.path.isdir(val): 107 m = 'File path for option %s is a directory: %s' 108 else: 109 m = 'File path for option %s does not exist: %s' 110 raise SCons.Errors.UserError(m % (key, val))
111
112 - def PathExists(self, key, val, env):
113 """Validator to check if Path exists""" 114 if not os.path.exists(val): 115 m = 'Path for option %s does not exist: %s' 116 raise SCons.Errors.UserError(m % (key, val))
117
118 - def __call__(self, key, help, default, validator=None):
119 """ 120 The input parameters describe a 'path list' option, thus they 121 are returned with the correct converter and validator appended. The 122 result is usable for input to opts.Add() . 123 124 The 'default' option specifies the default path to use if the 125 user does not specify an override with this option. 126 127 validator is a validator, see this file for examples 128 """ 129 if validator is None: 130 validator = self.PathExists 131 132 if SCons.Util.is_List(key) or SCons.Util.is_Tuple(key): 133 return (key, '%s ( /path/to/%s )' % (help, key[0]), default, 134 validator, None) 135 else: 136 return (key, '%s ( /path/to/%s )' % (help, key), default, 137 validator, None)
138 139 PathVariable = _PathVariableClass() 140 141 # Local Variables: 142 # tab-width:4 143 # indent-tabs-mode:nil 144 # End: 145 # vim: set expandtab tabstop=4 shiftwidth=4: 146