cleanup.py revision dbc573ff29b6345027dc0e66f545d3780ef2d165
1#!/usr/bin/env python
2# Copyright (C) 2012   Christian Heimes (christian@python.org)
3# Licensed to PSF under a Contributor Agreement.
4#
5# cleanup Keccak sources
6
7import os
8import re
9
10CPP1 = re.compile("^//(.*)")
11CPP2 = re.compile("\ //(.*)")
12
13STATICS = ("void ", "int ", "HashReturn ", "const UINT64 ", "UINT16 ")
14
15HERE = os.path.dirname(os.path.abspath(__file__))
16KECCAK = os.path.join(HERE, "keccak")
17
18def getfiles():
19    for name in os.listdir(KECCAK):
20        name = os.path.join(KECCAK, name)
21        if os.path.isfile(name):
22            yield name
23
24def cleanup(f):
25    buf = []
26    for line in f:
27        # mark all functions and global data as static
28        if line.startswith(STATICS):
29            buf.append("static " + line)
30            continue
31        # remove UINT64 typedef, we have our own
32        if line.startswith("typedef unsigned long long int"):
33            buf.append("/* %s */\n" % line.strip())
34            continue
35        ## remove #include "brg_endian.h"
36        #if "brg_endian.h" in line:
37        #    buf.append("/* %s */\n" % line.strip())
38        #    continue
39        # transform C++ comments into ANSI C comments
40        line = CPP1.sub(r"/* \1 */", line)
41        line = CPP2.sub(r" /* \1 */", line)
42        buf.append(line)
43    return "".join(buf)
44
45for name in getfiles():
46    with open(name) as f:
47        res = cleanup(f)
48    with open(name, "w") as f:
49        f.write(res)
50