Package SCons :: Package Node :: Module Python
[hide private]
[frames] | no frames]

Source Code for Module SCons.Node.Python

  1  """scons.Node.Python 
  2   
  3  Python nodes. 
  4   
  5  """ 
  6   
  7  # 
  8  # Copyright (c) 2001 - 2019 The SCons Foundation 
  9  # 
 10  # Permission is hereby granted, free of charge, to any person obtaining 
 11  # a copy of this software and associated documentation files (the 
 12  # "Software"), to deal in the Software without restriction, including 
 13  # without limitation the rights to use, copy, modify, merge, publish, 
 14  # distribute, sublicense, and/or sell copies of the Software, and to 
 15  # permit persons to whom the Software is furnished to do so, subject to 
 16  # the following conditions: 
 17  # 
 18  # The above copyright notice and this permission notice shall be included 
 19  # in all copies or substantial portions of the Software. 
 20  # 
 21  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
 22  # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
 23  # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 24  # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 25  # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
 26  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 27  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 28  # 
 29   
 30  __revision__ = "src/engine/SCons/Node/Python.py 3a41ed6b288cee8d085373ad7fa02894e1903864 2019-01-23 17:30:35 bdeegan" 
 31   
 32  import SCons.Node 
 33   
34 -class ValueNodeInfo(SCons.Node.NodeInfoBase):
35 __slots__ = ('csig',) 36 current_version_id = 2 37 38 field_list = ['csig'] 39
40 - def str_to_node(self, s):
41 return Value(s)
42
43 - def __getstate__(self):
44 """ 45 Return all fields that shall be pickled. Walk the slots in the class 46 hierarchy and add those to the state dictionary. If a '__dict__' slot is 47 available, copy all entries to the dictionary. Also include the version 48 id, which is fixed for all instances of a class. 49 """ 50 state = getattr(self, '__dict__', {}).copy() 51 for obj in type(self).mro(): 52 for name in getattr(obj,'__slots__',()): 53 if hasattr(self, name): 54 state[name] = getattr(self, name) 55 56 state['_version_id'] = self.current_version_id 57 try: 58 del state['__weakref__'] 59 except KeyError: 60 pass 61 62 return state
63
64 - def __setstate__(self, state):
65 """ 66 Restore the attributes from a pickled state. 67 """ 68 # TODO check or discard version 69 del state['_version_id'] 70 for key, value in state.items(): 71 if key not in ('__weakref__',): 72 setattr(self, key, value)
73 74
75 -class ValueBuildInfo(SCons.Node.BuildInfoBase):
76 __slots__ = () 77 current_version_id = 2
78
79 -class Value(SCons.Node.Node):
80 """A class for Python variables, typically passed on the command line 81 or generated by a script, but not from a file or some other source. 82 """ 83 84 NodeInfo = ValueNodeInfo 85 BuildInfo = ValueBuildInfo 86
87 - def __init__(self, value, built_value=None):
88 SCons.Node.Node.__init__(self) 89 self.value = value 90 self.changed_since_last_build = 6 91 self.store_info = 0 92 if built_value is not None: 93 self.built_value = built_value
94
95 - def str_for_display(self):
96 return repr(self.value)
97
98 - def __str__(self):
99 return str(self.value)
100
101 - def make_ready(self):
102 self.get_csig()
103
104 - def build(self, **kw):
105 if not hasattr(self, 'built_value'): 106 SCons.Node.Node.build(self, **kw)
107 108 is_up_to_date = SCons.Node.Node.children_are_up_to_date 109
110 - def is_under(self, dir):
111 # Make Value nodes get built regardless of 112 # what directory scons was run from. Value nodes 113 # are outside the filesystem: 114 return 1
115
116 - def write(self, built_value):
117 """Set the value of the node.""" 118 self.built_value = built_value
119
120 - def read(self):
121 """Return the value. If necessary, the value is built.""" 122 self.build() 123 if not hasattr(self, 'built_value'): 124 self.built_value = self.value 125 return self.built_value
126
127 - def get_text_contents(self):
128 """By the assumption that the node.built_value is a 129 deterministic product of the sources, the contents of a Value 130 are the concatenation of all the contents of its sources. As 131 the value need not be built when get_contents() is called, we 132 cannot use the actual node.built_value.""" 133 ###TODO: something reasonable about universal newlines 134 contents = str(self.value) 135 for kid in self.children(None): 136 contents = contents + kid.get_contents().decode() 137 return contents
138
139 - def get_contents(self):
140 """ 141 Get contents for signature calculations. 142 :return: bytes 143 """ 144 text_contents = self.get_text_contents() 145 try: 146 return text_contents.encode() 147 except UnicodeDecodeError: 148 # Already encoded as python2 str are bytes 149 return text_contents
150 151
152 - def changed_since_last_build(self, target, prev_ni):
153 cur_csig = self.get_csig() 154 try: 155 return cur_csig != prev_ni.csig 156 except AttributeError: 157 return 1
158
159 - def get_csig(self, calc=None):
160 """Because we're a Python value node and don't have a real 161 timestamp, we get to ignore the calculator and just use the 162 value contents. 163 164 Returns string. Ideally string of hex digits. (Not bytes) 165 """ 166 try: 167 return self.ninfo.csig 168 except AttributeError: 169 pass 170 171 contents = self.get_text_contents() 172 173 self.get_ninfo().csig = contents 174 return contents
175 176 # Local Variables: 177 # tab-width:4 178 # indent-tabs-mode:nil 179 # End: 180 # vim: set expandtab tabstop=4 shiftwidth=4: 181