lingologos-thinkingpython / tools /selfcheck_confine.py
tailot's picture
thinking-python: reference implementation, corpus and gates
4bc63c7 verified
Raw History Blame Contribute Delete
2.09 kB
"""Check that nothing outside datasets/thinkingpython was touched, and that the
bundle never reaches outside itself."""
import os
import subprocess
import sys
BUNDLE = os.path.join("datasets", "thinkingpython")
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(BUNDLE)))
def repo_root() -> str:
out = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True,
text=True, cwd=os.path.dirname(os.path.abspath(__file__)))
return out.stdout.strip()
def changed_files(root: str) -> list:
files = []
out = subprocess.run(["git", "status", "--porcelain"], capture_output=True, text=True,
cwd=root)
for line in out.stdout.splitlines():
path = line[3:].strip().strip('"')
if " -> " in path:
path = path.split(" -> ")[-1]
files.append(path.replace("\\", "/"))
return files
def main() -> int:
root = repo_root()
if not root:
print("confine=SKIP reason=no_git_repository")
return 0
bundle = BUNDLE.replace("\\", "/")
offenders = [f for f in changed_files(root) if not f.startswith(bundle + "/")]
inside = 0
for dirpath, dirnames, filenames in os.walk(os.path.join(root, bundle)):
dirnames[:] = [d for d in dirnames if d != "__pycache__"]
for name in filenames:
if not name.endswith(".py"):
continue
inside += 1
with open(os.path.join(dirpath, name), "r", encoding="utf-8") as handle:
text = handle.read()
for token in ("datasets" + "/" + "multi" + "lingual", os.pardir + os.sep,
os.pardir + "/" + os.pardir):
if token in text:
offenders.append("%s mentions %s" % (name, token))
print("confine=%s python_files=%d offenders=%d"
% ("PASS" if not offenders else "FAIL", inside, len(offenders)))
for item in offenders[:10]:
print(" %s" % item)
return 0 if not offenders else 1
if __name__ == "__main__":
raise SystemExit(main())