4c94b1acdb1eefc99630e583a00ca93b9531a029
[scons.git] / src / engine / SCons / Sig / MD5.py
1 """SCons.Sig.MD5
2
3 The MD5 signature package for the SCons software construction
4 utility.
5
6 """
7
8 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
9
10 import md5
11 import string
12
13 def current(obj, sig):
14     """Return whether a given object is up-to-date with the
15     specified signature.
16     """
17     return obj.get_signature() == sig
18
19 def hexdigest(s):
20     """Return a signature as a string of hex characters.
21     """
22     # NOTE:  This routine is a method in the Python 2.0 interface
23     # of the native md5 module, but we want SCons to operate all
24     # the way back to at least Python 1.5.2, which doesn't have it.
25     h = string.hexdigits
26     r = ''
27     for c in s:
28         i = ord(c)
29         r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
30     return r
31
32 def collect(signatures):
33     """
34     Collect a list of signatures into an aggregate signature.
35
36     signatures - a list of signatures
37     returns - the aggregate signature
38     """
39     if len(signatures) == 1:
40         return signatures[0]
41     else:
42         contents = string.join(signatures, ', ')
43         return hexdigest(md5.new(contents).digest())
44
45 def signature(obj):
46     """Generate a signature for an object
47     """
48     return hexdigest(md5.new(obj.get_contents()).digest())
49
50 def to_string(signature):
51     """Convert a signature to a string"""
52     return signature
53
54 def from_string(string):
55     """Convert a string to a signature"""
56     return string