File size: 3,195 Bytes
4bc63c7 74acaf8 4bc63c7 74acaf8 4bc63c7 74acaf8 4bc63c7 74acaf8 4bc63c7 74acaf8 4bc63c7 74acaf8 4bc63c7 | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | """Check that nothing outside datasets/thinkingpython was touched, and that the
bundle never reaches outside itself.
A changed path outside the bundle is a failure unless the bundle itself declares it
waived in confine_waivers.json. The waiver is data, not a code change: it names the
path, why it is outside the dataset work, and who accepted it. Waived paths are printed
so a reader of the gate output still sees them.
"""
import json
import os
import subprocess
import sys
BUNDLE = os.path.join("datasets", "thinkingpython")
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(BUNDLE)))
WAIVERS = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"confine_waivers.json")
def waivers() -> dict:
"""Declared exceptions: path -> reason. Missing file means no exceptions."""
if not os.path.isfile(WAIVERS):
return {}
with open(WAIVERS, "r", encoding="utf-8") as handle:
data = json.load(handle)
return dict((item["path"], item.get("reason", "")) for item in data.get("waived", []))
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("\\", "/")
waived = waivers()
offenders = []
declared = []
for path in changed_files(root):
if path.startswith(bundle + "/"):
continue
if path in waived:
declared.append(path)
continue
offenders.append(path)
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 waived_outside=%d"
% ("PASS" if not offenders else "FAIL", inside, len(offenders), len(declared)))
for item in offenders[:10]:
print(" offender: %s" % item)
for item in declared:
print(" waived: %s (%s)" % (item, waived[item]))
return 0 if not offenders else 1
if __name__ == "__main__":
raise SystemExit(main())
|