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/env python3
import re
import sys
import pathlib
def replace(makefile, included):
if included == "../Makefile.common":
return "../../Makefile.common"
if included == "../Makefile.aws_array_list":
return "../../aws_array_list/Makefile"
if included == "../Makefile.aws_string":
return "../../aws_string/Makefile"
if included == "../Makefile.aws_byte_buf":
return "../../aws_byte_buf/Makefile"
if included == "../Makefile.aws_linked_list":
return "../../aws_linked_list/Makefile"
if included == "../Makefile.aws_hash_table":
return "../../aws_hash_table/Makefile"
if included == "../Makefile.aws_priority_queue_sift":
return "../../aws_priority_queue_sift/Makefile"
return None
ok = True
pat = re.compile("include\s+(?P<path>[-/.\w]+)")
for fyle in pathlib.Path(".").rglob("Makefile"):
this_ok = True
buf = []
with open(fyle) as handle:
for line in handle:
line = line.rstrip()
m = pat.match(line)
if not m:
buf.append(line)
continue
included = fyle.parent / m["path"]
if included.exists():
buf.append(line)
else:
rep = replace(fyle, m["path"])
if rep and (fyle.parent / rep).exists():
buf.append(f"include {rep}")
else:
print(f"{fyle}: {line}", file=sys.stderr)
ok = False
if this_ok:
with open(fyle, "w") as handle:
print("\n".join(buf), file=handle)
if not ok:
sys.exit(1)
|