From 51bbf548c9b442d0b53b6c8de5f89403de274424 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Sun, 3 Feb 2019 11:37:30 +0100 Subject: deps: Add PyBind11 --- host/lib/deps/pybind11/remove_comments.py | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 host/lib/deps/pybind11/remove_comments.py (limited to 'host/lib/deps/pybind11/remove_comments.py') diff --git a/host/lib/deps/pybind11/remove_comments.py b/host/lib/deps/pybind11/remove_comments.py new file mode 100644 index 000000000..987c41e9d --- /dev/null +++ b/host/lib/deps/pybind11/remove_comments.py @@ -0,0 +1,55 @@ +#!/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 + + 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() -- cgit v1.2.3