#!/usr/bin/env python

import hashlib
import re
from pathlib import Path
from typing import List

FIXTURE_BASE = Path("hdeps/tests/fixtures/")

# This especially terrible html "parsing" is intended to roundtrip with edits.
TERRIBLE_ANCHOR_RE = re.compile(r"(\s*<a)(.*?)(/?>.*)", re.S)
TERRIBLE_ATTR_RE = re.compile(r'([\w-]+)(=)"(.*?)"')


class NotAnAnchor(Exception):
    pass


class Anchor:
    def __init__(self, text: str) -> None:
        m = TERRIBLE_ANCHOR_RE.match(text)
        if m is None:
            raise NotAnAnchor()
        self.header = m.group(1)
        self.trailer = m.group(3)

        self.attrs: Dict[str, str] = {}
        for attr in TERRIBLE_ATTR_RE.finditer(m.group(2)):
            self.attrs[attr.group(1)] = attr.group(3)

    def __str__(self):
        buf: List[str] = []
        buf.append(self.header)
        for k, v in self.attrs.items():
            buf.append(f' {k}="{v}"')
        buf.append(self.trailer)
        return "".join(buf)


def main():
    for p in FIXTURE_BASE.glob("*.html"):
        print("Process", p)
        buf: List[str] = []
        with open(p) as f:
            for line in f:
                try:
                    a = Anchor(line)
                except NotAnAnchor:
                    buf.append(line)
                    continue

                if a.attrs["href"].endswith(".whl"):
                    metadata_file = FIXTURE_BASE / (
                        a.attrs["href"].strip("/") + ".metadata"
                    )
                    if not metadata_file.exists():
                        buf.append(line)
                        continue

                    new_sha1 = hashlib.sha1(
                        metadata_file.read_bytes().replace(b"\r\n", b"\n")
                    ).hexdigest()
                    old_attr = a.attrs.get("data-core-metadata")
                    new_attr = f"sha1={new_sha1}"
                    if old_attr != new_attr:
                        print(f"  {a.attrs['href']} {old_attr} -> {new_attr}")
                        a.attrs["data-core-metadata"] = new_attr
                    buf.append(str(a))
                else:
                    buf.append(line)
                    continue

        p.write_text("".join(buf))


if __name__ == "__main__":
    main()
