3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 class ValueNodeInfo(SCons
.Node
.NodeInfoBase
):
33 current_version_id
= 2
37 def str_to_node(self
, s
):
38 return ValueWithMemo(s
)
41 class ValueBuildInfo(SCons
.Node
.BuildInfoBase
):
43 current_version_id
= 2
46 class Value(SCons
.Node
.Node
):
47 """A Node class for values represented by Python expressions.
49 Values are typically passed on the command line or generated
50 by a script, but not from a file or some other source.
52 .. versionchanged:: 4.0
53 the *name* parameter was added.
56 NodeInfo
= ValueNodeInfo
57 BuildInfo
= ValueBuildInfo
59 def __init__(self
, value
, built_value
=None, name
=None) -> None:
62 self
.changed_since_last_build
= 6
64 if built_value
is not None:
65 self
.built_value
= built_value
67 # Set a name so it can be a child of a node and not break
68 # its parent's implementation of Node.get_contents.
72 self
.name
= str(value
)
74 def str_for_display(self
):
75 return repr(self
.value
)
77 def __str__(self
) -> str:
78 return str(self
.value
)
80 def make_ready(self
) -> None:
83 def build(self
, **kw
) -> None:
84 if not hasattr(self
, 'built_value'):
85 SCons
.Node
.Node
.build(self
, **kw
)
87 is_up_to_date
= SCons
.Node
.Node
.children_are_up_to_date
89 def is_under(self
, dir) -> bool:
90 # Make Value nodes get built regardless of
91 # what directory scons was run from. Value nodes
92 # are outside the filesystem:
95 def write(self
, built_value
) -> None:
96 """Set the value of the node."""
97 self
.built_value
= built_value
100 """Return the value. If necessary, the value is built."""
102 if not hasattr(self
, 'built_value'):
103 self
.built_value
= self
.value
104 return self
.built_value
106 def get_text_contents(self
) -> str:
107 """By the assumption that the node.built_value is a
108 deterministic product of the sources, the contents of a Value
109 are the concatenation of all the contents of its sources. As
110 the value need not be built when get_contents() is called, we
111 cannot use the actual node.built_value."""
112 ###TODO: something reasonable about universal newlines
113 contents
= str(self
.value
)
114 for kid
in self
.children(None):
115 # Get csig() value of child as this is more efficent
116 contents
= contents
+ kid
.get_csig()
119 def get_contents(self
) -> bytes
:
120 """Get contents for signature calculations."""
121 return self
.get_text_contents().encode()
123 def get_csig(self
, calc
=None):
124 """Because we're a Python value node and don't have a real
125 timestamp, we get to ignore the calculator and just use the
128 Returns string. Ideally string of hex digits. (Not bytes)
131 return self
.ninfo
.csig
132 except AttributeError:
135 contents
= self
.get_text_contents()
137 self
.get_ninfo().csig
= contents
141 def ValueWithMemo(value
, built_value
=None, name
=None):
142 """Memoized :class:`Value` node factory.
144 .. versionchanged:: 4.0
145 the *name* parameter was added.
147 global _memo_lookup_map
149 # No current support for memoizing a value that needs to be built.
151 return Value(value
, built_value
, name
=name
)
154 memo_lookup_key
= hash((value
, name
))
156 # Non-primitive types will hit this codepath.
157 return Value(value
, name
=name
)
160 return _memo_lookup_map
[memo_lookup_key
]
162 v
= Value(value
, built_value
, name
)
163 _memo_lookup_map
[memo_lookup_key
] = v
169 # indent-tabs-mode:nil
171 # vim: set expandtab tabstop=4 shiftwidth=4: