|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import re |
| 6 | +from subprocess import check_call |
| 7 | + |
| 8 | +BOOST_NAME = "openmethod" |
| 9 | +BOOST_INCLUDE = Path("include", "boost", BOOST_NAME) |
| 10 | +YOMM2_INCLUDE = Path("include", "yorel", "yomm2") |
| 11 | +YOMM2_TESTS = Path("tests") |
| 12 | +SKIP = "cute keywords benchmark pss1 lab manual_call generator".split() |
| 13 | +REPLACE = ( |
| 14 | + ("YOREL_YOMM2_", f"BOOST_{BOOST_NAME.upper()}_"), |
| 15 | + ("#include <yorel/yomm2/", f"#include <boost/{BOOST_NAME}/"), |
| 16 | + ("<boost/openmethod/keywords.hpp>", "<boost/openmethod.hpp>"), |
| 17 | + ("yorel", "boost"), |
| 18 | + ("yomm2", "openmethod"), |
| 19 | + ("YOMM2_DECLARE|declare_method", "BOOST_OPENMETHOD"), |
| 20 | + ("YOMM2_DEFINE|define_method", "BOOST_OPENMETHOD_OVERRIDE"), |
| 21 | + ("YOMM2_CLASS(ES)?|register_class(es)?", "BOOST_OPENMETHOD_CLASSES"), |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def skip(path): |
| 26 | + for skip in SKIP: |
| 27 | + if skip in str(path): |
| 28 | + return True |
| 29 | + |
| 30 | + return False |
| 31 | + |
| 32 | + |
| 33 | +writeable = [] |
| 34 | + |
| 35 | +for from_path in list(YOMM2_INCLUDE.rglob("*.hpp")) + list(YOMM2_TESTS.rglob("*.?pp")): |
| 36 | + if skip(from_path): |
| 37 | + continue |
| 38 | + |
| 39 | + with from_path.open() as f: |
| 40 | + content = f.read() |
| 41 | + |
| 42 | + content = re.sub(r'#include "(yorel/yomm2/[^"])+"', r"#include <\1>", content) |
| 43 | + |
| 44 | + for replace in REPLACE: |
| 45 | + content = re.sub(*replace, content) |
| 46 | + |
| 47 | + to_path = Path( |
| 48 | + str(from_path) |
| 49 | + .replace("yorel/yomm2", f"boost/{BOOST_NAME}") |
| 50 | + .replace("tests/", "test/") |
| 51 | + ) |
| 52 | + |
| 53 | + print(f"{from_path} -> {to_path}...", end="") |
| 54 | + |
| 55 | + to_path.parent.mkdir(parents=True, exist_ok=True) |
| 56 | + |
| 57 | + if to_path.exists(): |
| 58 | + if os.access(to_path, os.W_OK): |
| 59 | + print("file is writeable, skipping") |
| 60 | + writeable.append(to_path) |
| 61 | + continue |
| 62 | + os.chmod(to_path, 0o744) |
| 63 | + |
| 64 | + with to_path.open("w") as f: |
| 65 | + f.write(content) |
| 66 | + |
| 67 | + check_call(["clang-format", "-i", str(to_path)]) |
| 68 | + |
| 69 | + os.chmod(to_path, 0o444) |
| 70 | + print("done") |
| 71 | + |
| 72 | +if writeable: |
| 73 | + print("Skipped because writeable:", *writeable) |
0 commit comments