Skip to content

Instantly share code, notes, and snippets.

@gsnedders
Created February 13, 2025 05:49
Show Gist options
  • Save gsnedders/00f39772ab02b7155b99858df0114a1a to your computer and use it in GitHub Desktop.
Save gsnedders/00f39772ab02b7155b99858df0114a1a to your computer and use it in GitHub Desktop.
# coding: utf-8
import os
import re
import sys
from itertools import groupby
from pathlib import Path, PurePosixPath
from urllib.parse import urlsplit, urlunsplit
import yaml
sys.path.insert(0, os.path.abspath("../web-platform-tests"))
from tools.manifest import manifest
label = "interop-2025-webcompat"
dir_to_label = "css/css-ui/compute-kind-widget-generated"
interop_eligible_test_types = {"testharness", "reftest", "crashtest", "wdspec"}
digit_re = re.compile("([0-9]+)")
def natsortkey(string_to_split):
split = digit_re.split(string_to_split)
split[1::2] = [(int(i), i) for i in split[1::2]]
return split
def appropriate_meta(url):
u = urlsplit(url)
u_path = PurePosixPath(u.path)
meta_dir = Path(*u_path.parent.relative_to("/").parts)
test_name = urlunsplit(u._replace(path=str(u_path.name)))
return meta_dir, test_name
class BetterIndentSafeDumper(yaml.SafeDumper):
def write_indicator(
self, indicator, need_whitespace, whitespace=False, indention=False
):
if self.whitespace or not need_whitespace:
if indicator == "-":
data = " " * (self.best_indent - 2) + indicator
else:
data = indicator
else:
data = " " + indicator
self.whitespace = whitespace
self.indention = self.indention and indention
self.column += len(data)
self.open_ended = False
if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
m = manifest.load_and_update(
"../web-platform-tests", "../web-platform-tests/MANIFEST.json", "/", update=True
)
to_label = set()
for item in m.iterdir(dir_to_label):
if item.item_type in interop_eligible_test_types:
to_label.add(item.url)
for meta_dir, tests in groupby(
map(
appropriate_meta,
sorted(to_label, key=lambda x: tuple(map(natsortkey, x.split("/")))),
),
key=lambda x: x[0],
):
meta_dir.mkdir(parents=True, exist_ok=True)
meta_file = meta_dir / "META.yml"
if meta_file.is_file():
with meta_file.open("rb") as f:
meta_data = yaml.safe_load(f)
if meta_data is None:
meta_data = {}
else:
meta_data = {}
links = meta_data.setdefault("links", [])
label_links = [l for l in links if l.get("label") == label]
already_labelled = {
result["test"] for label_link in label_links for result in label_link["results"]
}
if "*" in already_labelled:
continue
for _, t in tests:
if t in already_labelled:
continue
if not label_links:
links.append({"label": label, "results": []})
label_links.append(links[-1])
label_links[-1]["results"].append({"test": t})
with meta_file.open("w") as f:
yaml.dump(
meta_data,
f,
indent=6,
default_flow_style=False,
sort_keys=False,
Dumper=BetterIndentSafeDumper,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment