1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
"""SCons.Sig
The Signature package for the scons software construction utility.
"""
#
# Copyright (c) 2001, 2002 Steven Knight
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
import os
import os.path
import string
import SCons.Node
import time
#XXX Get rid of the global array so this becomes re-entrant.
sig_files = []
def write():
global sig_files
for sig_file in sig_files:
sig_file.write()
class SConsignEntry:
def __init__(self, module, entry=None):
self.timestamp = self.csig = self.bsig = self.implicit = None
if not entry is None:
arr = map(string.strip, string.split(entry, " ", 3))
try:
if arr[0] == '-': self.timestamp = None
else: self.timestamp = int(arr[0])
if arr[1] == '-': self.bsig = None
else: self.bsig = module.from_string(arr[1])
if arr[2] == '-': self.csig = None
else: self.csig = module.from_string(arr[2])
if len(arr) < 4: self.implicit = None # pre-0.07 format
elif arr[3] == '': self.implicit = '' # pre-0.08 format
elif arr[3] == '-': self.implicit = None
else: self.implicit = string.replace(arr[3], '\0\0', '')
except IndexError:
pass
def render(self, module):
if self.timestamp is None: timestamp = '-'
else: timestamp = "%d"%self.timestamp
if self.bsig is None: bsig = '-'
else: bsig = module.to_string(self.bsig)
if self.csig is None: csig = '-'
else: csig = module.to_string(self.csig)
if self.implicit is None: implicit = '-'
else: implicit = '\0\0%s\0\0'%self.implicit
return '%s %s %s %s' % (timestamp, bsig, csig, implicit)
def get_implicit(self):
if self.implicit is None:
return None
elif self.implicit == '':
return []
else:
return string.split(self.implicit, '\0')
def set_implicit(self, implicit):
if implicit is None:
self.implicit = None
else:
if SCons.Util.is_String(implicit):
implicit = [implicit]
self.implicit = string.join(map(str, implicit), '\0')
class SConsignFile:
"""
Encapsulates reading and writing a .sconsign file.
"""
def __init__(self, dir, module=None):
"""
dir - the directory for the file
module - the signature module being used
"""
self.dir = dir
if module is None:
self.module = default_calc.module
else:
self.module = module
self.sconsign = os.path.join(dir.path, '.sconsign')
self.entries = {}
self.dirty = None
try:
file = open(self.sconsign, 'rt')
except:
pass
else:
for line in file.readlines():
filename, rest = map(string.strip, string.split(line, ":", 1))
self.entries[filename] = SConsignEntry(self.module, rest)
global sig_files
sig_files.append(self)
def get(self, filename):
"""
Get the .sconsign entry for a file
filename - the filename whose signature will be returned
returns - (timestamp, bsig, csig)
"""
try:
entry = self.entries[filename]
return (entry.timestamp, entry.bsig, entry.csig)
except KeyError:
return (None, None, None)
def create_entry(self, filename):
"""
Create an entry for the filename and return it, or if one already exists,
then return it.
"""
try:
entry = self.entries[filename]
except KeyError:
entry = SConsignEntry(self.module)
self.entries[filename] = entry
return entry
def set_csig(self, filename, csig):
"""
Set the csig .sconsign entry for a file
filename - the filename whose signature will be set
csig - the file's content signature
"""
entry = self.create_entry(filename)
entry.csig = csig
self.dirty = 1
def set_bsig(self, filename, bsig):
"""
Set the csig .sconsign entry for a file
filename - the filename whose signature will be set
bsig - the file's built signature
"""
entry = self.create_entry(filename)
entry.bsig = bsig
self.dirty = 1
def set_timestamp(self, filename, timestamp):
"""
Set the csig .sconsign entry for a file
filename - the filename whose signature will be set
timestamp - the file's timestamp
"""
entry = self.create_entry(filename)
entry.timestamp = timestamp
self.dirty = 1
def get_implicit(self, filename):
"""Fetch the cached implicit dependencies for 'filename'"""
try:
entry = self.entries[filename]
return entry.get_implicit()
except KeyError:
return None
def set_implicit(self, filename, implicit):
"""Cache the implicit dependencies for 'filename'."""
entry = self.create_entry(filename)
entry.set_implicit(implicit)
self.dirty = 1
def write(self):
"""
Write the .sconsign file to disk.
Try to write to a temporary file first, and rename it if we
succeed. If we can't write to the temporary file, it's
probably because the directory isn't writable (and if so,
how did we build anything in this directory, anyway?), so
try to write directly to the .sconsign file as a backup.
If we can't rename, try to copy the temporary contents back
to the .sconsign file. Either way, always try to remove
the temporary file at the end.
"""
if self.dirty:
temp = os.path.join(self.dir.path, '.scons%d' % os.getpid())
try:
file = open(temp, 'wt')
fname = temp
except:
try:
file = open(self.sconsign, 'wt')
fname = self.sconsign
except:
return
keys = self.entries.keys()
keys.sort()
for name in keys:
file.write("%s: %s\n" % (name, self.entries[name].render(self.module)))
file.close()
if fname != self.sconsign:
try:
mode = os.stat(self.sconsign)[0]
os.chmod(self.sconsign, 0666)
os.unlink(self.sconsign)
except:
pass
try:
os.rename(fname, self.sconsign)
except:
open(self.sconsign, 'wb').write(open(fname, 'rb').read())
os.chmod(self.sconsign, mode)
try:
os.unlink(temp)
except:
pass
class Calculator:
"""
Encapsulates signature calculations and .sconsign file generating
for the build engine.
"""
def __init__(self, module=None, max_drift=2*24*60*60):
"""
Initialize the calculator.
module - the signature module to use for signature calculations
max_drift - the maximum system clock drift used to determine when to
cache content signatures. A negative value means to never cache
content signatures. (defaults to 2 days)
"""
if module is None:
try:
import MD5
self.module = MD5
except ImportError:
# fallback on timestamp signatures if MD5 is not available
# XXX add a warning message here
import TimeStamp
self.module = TimeStamp
else:
self.module = module
self.max_drift = max_drift
def bsig(self, node):
"""
Generate a node's build signature, the digested signatures
of its dependency files and build information.
node - the node whose sources will be collected
returns - the build signature
This no longer handles the recursive descent of the
node's children's signatures. We expect that they're
already built and updated by someone else, if that's
what's wanted.
"""
if not node.use_signature:
return None
bsig = node.get_bsig()
if not bsig is None:
return bsig
sigs = map(self.get_signature, node.children())
if node.builder:
sigs.append(self.module.signature(node.builder_sig_adapter()))
bsig = self.module.collect(filter(lambda x: not x is None, sigs))
node.set_bsig(bsig)
# don't store the bsig here, because it isn't accurate until
# the node is actually built.
return bsig
def csig(self, node):
"""
Generate a node's content signature, the digested signature
of its content.
node - the node
returns - the content signature
"""
if not node.use_signature:
return None
csig = node.get_csig()
if not csig is None:
return csig
if self.max_drift >= 0:
info = node.get_prevsiginfo()
else:
info = None
mtime = node.get_timestamp()
if (info and info[0] and info[2] and info[0] == mtime):
# use the signature stored in the .sconsign file
csig = info[2]
# Set the csig here so it doesn't get recalculated unnecessarily
# and so it's set when the .sconsign file gets written
node.set_csig(csig)
else:
csig = self.module.signature(node)
# Set the csig here so it doesn't get recalculated unnecessarily
# and so it's set when the .sconsign file gets written
node.set_csig(csig)
if self.max_drift >= 0 and (time.time() - mtime) > self.max_drift:
node.store_csig()
node.store_timestamp()
return csig
def get_signature(self, node):
"""
Get the appropriate signature for a node.
node - the node
returns - the signature or None if the signature could not
be computed.
This method does not store the signature in the node and
in the .sconsign file.
"""
if not node.use_signature:
# This node type doesn't use a signature (e.g. a
# directory) so bail right away.
return None
elif node.builder:
return self.bsig(node)
elif not node.exists():
return None
else:
return self.csig(node)
def current(self, node, newsig):
"""
Check if a signature is up to date with respect to a node.
node - the node whose signature will be checked
newsig - the (presumably current) signature of the file
returns - 1 if the file is current with the specified signature,
0 if it isn't
"""
c = node.current()
if not c is None:
# The node itself has told us whether or not it's
# current without checking the signature. The
# canonical uses here are a "0" return for a file
# that doesn't exist, or a directory.
return c
oldtime, oldbsig, oldcsig = node.get_prevsiginfo()
if not node.builder and node.get_timestamp() == oldtime:
return 1
return self.module.current(newsig, oldbsig)
default_calc = Calculator()
|