Skip to content

Commit bcf7158

Browse files
authored
Merge pull request SCons#4525 from mwichmann/maint/Variables-package
Variables cleanup: PackageVariable
2 parents a8ad675 + 2051463 commit bcf7158

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

SCons/Variables/PackageVariable.py

+37-21
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@
4242
default='yes'
4343
)
4444
)
45-
...
46-
if env['x11'] == True:
45+
env = Environment(variables=opts)
46+
if env['x11'] is True:
4747
dir = ... # search X11 in some standard places ...
4848
env['x11'] = dir
4949
if env['x11']:
5050
... # build with x11 ...
5151
"""
5252

53-
from typing import Tuple, Callable
53+
import os
54+
from typing import Callable, Optional, Tuple
5455

5556
import SCons.Errors
5657

@@ -60,7 +61,12 @@
6061
DISABLE_STRINGS = ('0', 'no', 'false', 'off', 'disable')
6162

6263
def _converter(val):
63-
""" """
64+
"""Convert package variables.
65+
66+
Returns True or False if one of the recognized truthy or falsy
67+
values is seen, else return the value unchanged (expected to
68+
be a path string).
69+
"""
6470
lval = val.lower()
6571
if lval in ENABLE_STRINGS:
6672
return True
@@ -70,35 +76,45 @@ def _converter(val):
7076

7177

7278
def _validator(key, val, env, searchfunc) -> None:
73-
""" """
74-
# NB: searchfunc is currently undocumented and unsupported
75-
# TODO write validator, check for path
76-
import os
79+
"""Validate package variable for valid path.
7780
81+
Checks that if a path is given as the value, that pathname actually exists.
82+
"""
83+
# NOTE: searchfunc is currently undocumented and unsupported
7884
if env[key] is True:
7985
if searchfunc:
8086
env[key] = searchfunc(key, val)
87+
# TODO: need to check path, or be sure searchfunc raises.
8188
elif env[key] and not os.path.exists(val):
82-
raise SCons.Errors.UserError(
83-
'Path does not exist for option %s: %s' % (key, val))
89+
msg = f'Path does not exist for variable {key!r}: {val!r}'
90+
raise SCons.Errors.UserError(msg) from None
8491

8592

86-
def PackageVariable(key, help, default, searchfunc=None) -> Tuple[str, str, str, Callable, Callable]:
93+
# lint: W0622: Redefining built-in 'help' (redefined-builtin)
94+
def PackageVariable(
95+
key: str, help: str, default, searchfunc: Optional[Callable] = None
96+
) -> Tuple[str, str, str, Callable, Callable]:
8797
"""Return a tuple describing a package list SCons Variable.
8898
89-
The input parameters describe a 'package list' option. Returns
90-
a tuple including the correct converter and validator appended.
91-
The result is usable as input to :meth:`Add` .
99+
The input parameters describe a 'package list' variable. Returns
100+
a tuple with the correct converter and validator appended.
101+
The result is usable as input to :meth:`~SCons.Variables.Variables.Add`.
92102
93-
A 'package list' option may either be 'all', 'none' or a pathname
94-
string. This information is appended to *help*.
103+
A 'package list' variable may either be a truthy string from
104+
:const:`ENABLE_STRINGS`, a falsy string from
105+
:const:`DISABLE_STRINGS`, or a pathname string.
106+
This information is appended to *help* using only one string
107+
each for truthy/falsy.
95108
"""
96109
# NB: searchfunc is currently undocumented and unsupported
97-
help = '\n '.join(
98-
(help, '( yes | no | /path/to/%s )' % key))
99-
return (key, help, default,
100-
lambda k, v, e: _validator(k, v, e, searchfunc),
101-
_converter)
110+
help = '\n '.join((help, f'( yes | no | /path/to/{key} )'))
111+
return (
112+
key,
113+
help,
114+
default,
115+
lambda k, v, e: _validator(k, v, e, searchfunc),
116+
_converter,
117+
)
102118

103119
# Local Variables:
104120
# tab-width:4

SCons/Variables/PackageVariableTests.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ class PackageVariableTestCase(unittest.TestCase):
3232
def test_PackageVariable(self) -> None:
3333
"""Test PackageVariable creation"""
3434
opts = SCons.Variables.Variables()
35-
opts.Add(SCons.Variables.PackageVariable('test', 'test option help', '/default/path'))
35+
opts.Add(SCons.Variables.PackageVariable('test', 'test build variable help', '/default/path'))
3636

3737
o = opts.options[0]
3838
assert o.key == 'test', o.key
39-
assert o.help == 'test option help\n ( yes | no | /path/to/test )', repr(o.help)
39+
assert o.help == 'test build variable help\n ( yes | no | /path/to/test )', repr(o.help)
4040
assert o.default == '/default/path', o.default
4141
assert o.validator is not None, o.validator
4242
assert o.converter is not None, o.converter
4343

4444
def test_converter(self) -> None:
4545
"""Test the PackageVariable converter"""
4646
opts = SCons.Variables.Variables()
47-
opts.Add(SCons.Variables.PackageVariable('test', 'test option help', '/default/path'))
47+
opts.Add(SCons.Variables.PackageVariable('test', 'test build variable help', '/default/path'))
4848

4949
o = opts.options[0]
5050

@@ -64,11 +64,11 @@ def test_converter(self) -> None:
6464

6565
for t in true_values:
6666
x = o.converter(t)
67-
assert x, "converter returned false for '%s'" % t
67+
assert x, f"converter returned False for {t!r}"
6868

6969
for f in false_values:
7070
x = o.converter(f)
71-
assert not x, "converter returned true for '%s'" % f
71+
assert not x, f"converter returned True for {f!r}"
7272

7373
x = o.converter('/explicit/path')
7474
assert x == '/explicit/path', x
@@ -85,7 +85,7 @@ def test_converter(self) -> None:
8585
def test_validator(self) -> None:
8686
"""Test the PackageVariable validator"""
8787
opts = SCons.Variables.Variables()
88-
opts.Add(SCons.Variables.PackageVariable('test', 'test option help', '/default/path'))
88+
opts.Add(SCons.Variables.PackageVariable('test', 'test build variable help', '/default/path'))
8989

9090
test = TestCmd.TestCmd(workdir='')
9191
test.write('exists', 'exists\n')
@@ -101,12 +101,8 @@ def test_validator(self) -> None:
101101
o.validator('T', '/path', env)
102102
o.validator('X', exists, env)
103103

104-
caught = None
105-
try:
104+
with self.assertRaises(SCons.Errors.UserError) as cm:
106105
o.validator('X', does_not_exist, env)
107-
except SCons.Errors.UserError:
108-
caught = 1
109-
assert caught, "did not catch expected UserError"
110106

111107

112108
if __name__ == "__main__":

test/Variables/PackageVariable.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
#
3-
# __COPYRIGHT__
3+
# MIT License
4+
#
5+
# Copyright The SCons Foundation
46
#
57
# Permission is hereby granted, free of charge, to any person obtaining
68
# a copy of this software and associated documentation files (the
@@ -20,9 +22,6 @@
2022
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2123
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2224
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23-
#
24-
25-
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
2625

2726
"""
2827
Test the PackageVariable canned Variable type.
@@ -39,8 +38,6 @@ def check(expect):
3938
result = test.stdout().split('\n')
4039
assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
4140

42-
43-
4441
test.write(SConstruct_path, """\
4542
from SCons.Variables.PackageVariable import PackageVariable
4643
PV = PackageVariable
@@ -75,13 +72,11 @@ def check(expect):
7572
check([test.workpath()])
7673

7774
expect_stderr = """
78-
scons: *** Path does not exist for option x11: /non/existing/path/
75+
scons: *** Path does not exist for variable 'x11': '/non/existing/path/'
7976
""" + test.python_file_line(SConstruct_path, 14)
8077

8178
test.run(arguments='x11=/non/existing/path/', stderr=expect_stderr, status=2)
8279

83-
84-
8580
test.pass_test()
8681

8782
# Local Variables:

0 commit comments

Comments
 (0)