You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.4 KiB

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