1 """SCons.Variables.PathVariable
2
3 This file defines an option type for SCons implementing path settings.
4
5 To be used whenever a 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
12 validators are:
13
14 PathAccept -- accepts any path setting; no validation
15 PathIsDir -- path must be an existing directory
16 PathIsDirCreate -- path must be a dir; will create
17 PathIsFile -- path must be a file
18 PathExists -- path must exist (any type) [default]
19
20 The validator is a function that is called and which
21 should return True or False to indicate if the path
22 is valid. The arguments to the validator function
23 are: (key, val, env). The key is the name of the
24 option, the val is the path specified for the option,
25 and the env is the env to which the Otions have been
26 added.
27
28 Usage example:
29
30 Examples:
31 prefix=/usr/local
32
33 opts = Variables()
34
35 opts = Variables()
36 opts.Add(PathVariable('qtdir',
37 'where the root of Qt is installed',
38 qtdir, PathIsDir))
39 opts.Add(PathVariable('qt_includes',
40 'where the Qt includes are installed',
41 '$qtdir/includes', PathIsDirCreate))
42 opts.Add(PathVariable('qt_libraries',
43 'where the Qt library is installed',
44 '$qtdir/lib'))
45
46 """
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 __revision__ = "src/engine/SCons/Variables/PathVariable.py rel_2.4.1:3453:73fefd3ea0b0 2015/11/09 03:25:05 bdbaddog"
72
73 __all__ = ['PathVariable',]
74
75 import os
76 import os.path
77
78 import SCons.Errors
79
81
83 """Accepts any path, no checking done."""
84 pass
85
87 """Validator to check if Path is a directory."""
88 if not os.path.isdir(val):
89 if os.path.isfile(val):
90 m = 'Directory path for option %s is a file: %s'
91 else:
92 m = 'Directory path for option %s does not exist: %s'
93 raise SCons.Errors.UserError(m % (key, val))
94
96 """Validator to check if Path is a directory,
97 creating it if it does not exist."""
98 if os.path.isfile(val):
99 m = 'Path for option %s is a file, not a directory: %s'
100 raise SCons.Errors.UserError(m % (key, val))
101 if not os.path.isdir(val):
102 os.makedirs(val)
103
105 """validator to check if Path is a file"""
106 if not os.path.isfile(val):
107 if os.path.isdir(val):
108 m = 'File path for option %s is a directory: %s'
109 else:
110 m = 'File path for option %s does not exist: %s'
111 raise SCons.Errors.UserError(m % (key, val))
112
114 """validator to check if Path exists"""
115 if not os.path.exists(val):
116 m = 'Path for option %s does not exist: %s'
117 raise SCons.Errors.UserError(m % (key, val))
118
119 - def __call__(self, key, help, default, validator=None):
120
121 """
122 The input parameters describe a 'path list' option, thus they
123 are returned with the correct converter and validator appended. The
124 result is usable for input to opts.Add() .
125
126 The 'default' option specifies the default path to use if the
127 user does not specify an override with this option.
128
129 validator is a validator, see this file for examples
130 """
131 if validator is None:
132 validator = self.PathExists
133
134 if SCons.Util.is_List(key) or SCons.Util.is_Tuple(key):
135 return (key, '%s ( /path/to/%s )' % (help, key[0]), default,
136 validator, None)
137 else:
138 return (key, '%s ( /path/to/%s )' % (help, key), default,
139 validator, None)
140
141 PathVariable = _PathVariableClass()
142
143
144
145
146
147
148