add_boilerplate.py 1.6 KB

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