import io
p='.github/ci/classify-changes.py'
s=io.open(p,encoding='utf-8',newline='').read()
old=s[s.index('def is_code_path(path):'):s.index('def classify_files(files):')]
new='''# Markdown that is compiled into a binary or test is CODE, whatever its
# extension says. The selftest re-derives this set from every
# `include_str!`/`include_bytes!` of a `.md` in the tree and fails if they
# disagree, so a new compiled-in doc cannot slip through as "docs".
COMPILED_MARKDOWN = frozenset({"docs/ER-SKELETON.md"})


def is_docs_path(path):
    """True ONLY for paths positively known not to affect lint or unit:
    the declarative traceable-reqs.toml registry, repo-root Markdown, and
    Markdown under docs/ that nothing compiles in. Everything else is code,
    including .github/** (a CI change decides whether units run),
    docs-site/** (built and link-checked), crates/** and any path this list
    does not name."""
    if path == "traceable-reqs.toml":
        return True
    if not path.endswith(".md") or path in COMPILED_MARKDOWN:
        return False
    if "/" not in path:
        return True
    return path.startswith("docs/")


'''
s=s.replace(old,new)
s=s.replace('''    code_files = [f for f in files if is_code_path(f)]''','''    code_files = [f for f in files if not is_docs_path(f)]''')
io.open(p,'w',encoding='utf-8',newline='').write(s)

p='.github/ci/classify-changes-selftest.py'
s=io.open(p,encoding='utf-8',newline='').read()
anchor='''# [unit->REQ-CI-PUSH-DOCS-ONLY-THIN]
class OtherEventsAndErrors(unittest.TestCase):'''
add='''# [unit->REQ-CI-PUSH-DOCS-ONLY-THIN]
class DocsSetIsPositive(unittest.TestCase):
    def test_docs_set(self):
        for p in ["README.md", "CHANGELOG.md", "docs/INFRA-REGISTER.md",
                  "docs/adr/0050-golden-ci-merge-integration.md", "traceable-reqs.toml"]:
            self.assertTrue(cc.is_docs_path(p), p)

    def test_everything_else_is_code(self):
        for p in [".github/workflows/ci.yml", ".github/ci/classify-changes.py",
                  ".github/PULL_REQUEST_TEMPLATE.md", "docs-site/src/a.md",
                  "docs/ER-SKELETON.md", "crates/spt-daemon/tests/fixtures/enlyzeam/README.md",
                  "releases-repo/SYNC.md", "docs/instruments/ir21/bindeps-probe/Cargo.toml",
                  "Cargo.toml", "Cargo.lock", "crates/spt/src/cli.rs", "notes.txt"]:
            self.assertFalse(cc.is_docs_path(p), p)

    def test_a_ci_only_push_runs_units(self):
        g = FakeGit({(BEFORE, SHA): [".github/workflows/ci.yml", "docs/a.md"]}, reachable_after=1)
        self.assertEqual(push(g)[0], "CLASSIFY code=true reason=code-path:.github/workflows/ci.yml")

    def test_compiled_markdown_matches_the_tree(self):
        # Every .md the Rust tree compiles in must be in COMPILED_MARKDOWN,
        # and nothing else may be: the list is derived, not remembered.
        root = Path(__file__).resolve().parents[2]
        pat = re.compile(r'include_(?:str|bytes)!\(\s*"([^"]+\.md)"')
        found = set()
        for rs in root.rglob("*.rs"):
            parts = rs.relative_to(root).parts
            if parts[0] in ("target", ".worktrees") or "target" in parts:
                continue
            for rel in pat.findall(rs.read_text(encoding="utf-8", errors="replace")):
                found.add((rs.parent / rel).resolve().relative_to(root).as_posix())
        self.assertTrue(found, "control: the tree compiles in at least one .md today")
        self.assertEqual(found, set(cc.COMPILED_MARKDOWN))


'''
assert s.count(anchor)==1
s=s.replace(anchor, add+anchor)
s=s.replace('import os\nfrom pathlib import Path\n','import os\nfrom pathlib import Path\nimport re\n')
io.open(p,'w',encoding='utf-8',newline='').write(s)
print('ok')
