Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / histogram / tools / add_boilerplate.py
1 #!/usr/bin/env python3
2
3 # Copyright Hans Dembinski 2019
4 # Distributed under the Boost Software License, Version 1.0.
5 # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
6
7 import sys
8 from os.path import abspath, join
9 import re
10 import datetime
11
12 project_dir = "/".join(abspath(__file__).split("/")[:-2])
13
14 filename = abspath(sys.argv[1])
15
16 copyright = """// Copyright Hans Dembinski {}
17 //
18 // Distributed under the Boost Software License, Version 1.0.
19 // (See accompanying file LICENSE_1_0.txt
20 // or copy at http://www.boost.org/LICENSE_1_0.txt)
21
22 """.format(datetime.datetime.today().year)
23
24 if filename.endswith(".hpp"):
25     with open(filename) as f:
26         content = f.read()
27     if not content.startswith("// Copyright"):
28         content = copyright + content
29
30     sub = filename[len(project_dir) + 1:]
31     if sub.startswith("include/boost/"):
32         sub = sub[len("include/boost/"):]
33     if sub.startswith("test/"):
34         sub = "histogram/" + sub
35     guard_name = "BOOST_" + sub.replace(".", "_").replace("/", "_").upper()
36
37     if guard_name not in content:
38         lines = content.split("\n")
39         for end, line in enumerate(lines):
40             if line.startswith("//"):
41                 continue
42             break
43         for start in range(end, len(lines)):
44             if lines[start] != "":
45                 break
46         lines = lines[:end] + ["", "#ifndef " + guard_name, "#define " + guard_name, ""] + lines[start:]
47         while lines[-1] == "":
48             lines.pop()
49         lines += ["", "#endif // " + guard_name, ""]
50         content = "\n".join(lines)
51
52     with open(filename, "w") as f:
53         f.write(content)