aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/deps/pybind11/remove_comments.py
blob: 1cc60ef35a83031f6c04fece8acb2e18b764387c (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
46
47
48
49
50
51
52
53
#!/usr/bin/python3
"""
Simple program to remove all comments from pybind11 files.
It will retain the copyright header.
"""
import re
import os

# Note: This is the default header of the PyBind11 files, not of this file!
DEFAULT_HEADER="""/*
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/
"""

def comment_remover(text):
    """
    From:
    https://stackoverflow.com/questions/241327/python-snippet-to-remove-c-and-c-comments
    """
    def replacer(match):
        s = match.group(0)
        if s.startswith('/'):
            return " " # note: a space and not an empty string
        else:
            return s
    pattern = re.compile(
        r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
        re.DOTALL | re.MULTILINE
    )
    return re.sub(pattern, replacer, text)

def remove_trailing_whitespace(text):
    """ Remove, uh, trailing whitespace. """
    pattern = re.compile(r' +$', re.DOTALL | re.MULTILINE)
    return re.sub(pattern, '', text)


def main():
    """ Go Go Go! """
    for root, _, files in os.walk("include/pybind11"):
        for fname in files:
            path = os.path.join(root, fname)
            print("Modifying {}...".format(path))
            new_file = comment_remover(open(os.path.join(root, fname)).read())
            new_file = remove_trailing_whitespace(new_file)
            open(os.path.join(root, fname), 'w').write(
                DEFAULT_HEADER + new_file)

if __name__ == "__main__":
    main()