Remove redundant code
[scons.git] / SCons / Node / Python.py
blobc057b8d5cab97c3c57d4ce80aa3d6a1e3842f813
1 # MIT License
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.
24 """Python nodes."""
26 import SCons.Node
28 _memo_lookup_map = {}
31 class ValueNodeInfo(SCons.Node.NodeInfoBase):
32 __slots__ = ('csig',)
33 current_version_id = 2
35 field_list = ['csig']
37 def str_to_node(self, s):
38 return ValueWithMemo(s)
41 class ValueBuildInfo(SCons.Node.BuildInfoBase):
42 __slots__ = ()
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.
54 """
56 NodeInfo = ValueNodeInfo
57 BuildInfo = ValueBuildInfo
59 def __init__(self, value, built_value=None, name=None) -> None:
60 super().__init__()
61 self.value = value
62 self.changed_since_last_build = 6
63 self.store_info = 0
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.
69 if name:
70 self.name = name
71 else:
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:
81 self.get_csig()
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:
93 return True
95 def write(self, built_value) -> None:
96 """Set the value of the node."""
97 self.built_value = built_value
99 def read(self):
100 """Return the value. If necessary, the value is built."""
101 self.build()
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()
117 return contents
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
126 value contents.
128 Returns string. Ideally string of hex digits. (Not bytes)
130 try:
131 return self.ninfo.csig
132 except AttributeError:
133 pass
135 contents = self.get_text_contents()
137 self.get_ninfo().csig = contents
138 return 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.
150 if built_value:
151 return Value(value, built_value, name=name)
153 try:
154 memo_lookup_key = hash((value, name))
155 except TypeError:
156 # Non-primitive types will hit this codepath.
157 return Value(value, name=name)
159 try:
160 return _memo_lookup_map[memo_lookup_key]
161 except KeyError:
162 v = Value(value, built_value, name)
163 _memo_lookup_map[memo_lookup_key] = v
164 return v
167 # Local Variables:
168 # tab-width:4
169 # indent-tabs-mode:nil
170 # End:
171 # vim: set expandtab tabstop=4 shiftwidth=4: