summaryrefslogtreecommitdiff
path: root/site_scons/site_tools/md5sum.py
blob: 3be71f1138b38fd211b5df67a3b4be742405f917 (plain)
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
#!/usr/bin/python

from string import Template
import stat
import SCons

def md5sum(filename):
    import hashlib
    f = file(filename,'rb')
    return hashlib.md5(f.read()).hexdigest()

def md5sum_action(target, source, env):
    for i in range(len(source)):
        digest = md5sum(source[i].abspath)
        content = digest + ' ' + source[i].name + '\n'
        file(target[i].abspath, 'w').write(content)
    return 0

def md5sum_emitter(target, source, env):
    if len(target) < len(source):
        diff = len(source) - len(target)
        offset = len(target)
        for i in range(diff):
            s = source[offset + i]
            target.append(env.File(s.abspath + '.md5sum'))
    return (target, source)

def generate(env, **kw):
    try:
        env['BUILDERS']['MD5SUMSTR']
        env['BUILDERS']['MD5SUM']
    except KeyError:
        md5str = "Caculating md5sum: $TARGETS"
        action = SCons.Action.Action(md5sum_action, '$MD5SUMSTR')
        env['MD5SUMSTR'] = md5str
        env['BUILDERS']['MD5SUM'] = env.Builder(action = action,
                                                emitter = md5sum_emitter,
                                                suffix = '.md5sum')

def exists(env):
    try:
        import hashlib
        return True
    except:
        return False