Add monthly refresh job (scripts/refresh.py)
Browse files- README.md +5 -1
- scripts/build_snapshot.py +26 -9
- scripts/refresh.py +273 -0
README.md
CHANGED
|
@@ -69,6 +69,9 @@ for impersonation or nuisance testing.
|
|
| 69 |
- `schema/bluetooth_assigned_numbers.schema.json`: assigned-number schema.
|
| 70 |
- `scripts/build_snapshot.py`: rebuilds the tables from a fresh download of
|
| 71 |
the upstream registries.
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
Current vendor identifier records: `6619` (6489 OUI, 24 Company
|
| 74 |
Identifiers, 106 member UUIDs).
|
|
@@ -147,7 +150,8 @@ marked `2026-11-25` was assigned upstream after `2026-05-25`, so filtering on
|
|
| 147 |
it lists new allocations between two snapshots. Rows present in the first
|
| 148 |
snapshot carry `2026-05-25`, which only means "on or before that date".
|
| 149 |
|
| 150 |
-
Each snapshot is tagged in this repository
|
|
|
|
| 151 |
Pin a tag when you need a reproducible result:
|
| 152 |
|
| 153 |
```python
|
|
|
|
| 69 |
- `schema/bluetooth_assigned_numbers.schema.json`: assigned-number schema.
|
| 70 |
- `scripts/build_snapshot.py`: rebuilds the tables from a fresh download of
|
| 71 |
the upstream registries.
|
| 72 |
+
- `scripts/refresh.py`: the monthly refresh job. It downloads the registries,
|
| 73 |
+
rebuilds the tables when an upstream file changed, and opens a pull request
|
| 74 |
+
on this repository for review; nothing is merged automatically.
|
| 75 |
|
| 76 |
Current vendor identifier records: `6619` (6489 OUI, 24 Company
|
| 77 |
Identifiers, 106 member UUIDs).
|
|
|
|
| 150 |
it lists new allocations between two snapshots. Rows present in the first
|
| 151 |
snapshot carry `2026-05-25`, which only means "on or before that date".
|
| 152 |
|
| 153 |
+
Each snapshot is tagged in this repository as `v<snapshot date>` (for
|
| 154 |
+
example `v2026-05-25`, `v2026-11-25`; see the Changelog for the full list).
|
| 155 |
Pin a tag when you need a reproducible result:
|
| 156 |
|
| 157 |
```python
|
scripts/build_snapshot.py
CHANGED
|
@@ -266,17 +266,34 @@ def build_manifest(old_manifest, date, hashes, vi, bn):
|
|
| 266 |
return manifest
|
| 267 |
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
def main():
|
| 270 |
ap = argparse.ArgumentParser()
|
| 271 |
ap.add_argument("--upstream", required=True, type=Path)
|
| 272 |
ap.add_argument("--date", required=True)
|
| 273 |
ap.add_argument("--seed", type=Path, help="record_id,first_seen_snapshot CSV")
|
| 274 |
args = ap.parse_args()
|
|
|
|
|
|
|
| 275 |
|
| 276 |
-
|
| 277 |
-
hashes =
|
| 278 |
-
|
| 279 |
-
seed = {r["record_id"]: r[FIRST_SEEN] for r in read_csv(args.seed)} if args.seed else {}
|
| 280 |
|
| 281 |
old_manifest = read_csv(DATA / "source_manifest.csv")
|
| 282 |
old_appearance_sha = {m["source_sha256"] for m in old_manifest if m["source_url"].endswith(APPEARANCE_FILE)}
|
|
@@ -287,17 +304,17 @@ def main():
|
|
| 287 |
old_vi = read_csv(DATA / "vendor_identifiers.csv")
|
| 288 |
old_bn = read_csv(DATA / "bluetooth_assigned_numbers.csv")
|
| 289 |
|
| 290 |
-
vi = build_vendor_identifiers(
|
| 291 |
-
bn = build_assigned_numbers(
|
| 292 |
-
vi = stamp_first_seen(vi, old_vi, seed,
|
| 293 |
-
bn = stamp_first_seen(bn, old_bn, seed,
|
| 294 |
write_csv("vendor_identifiers.csv", vi, with_column(list(old_vi[0].keys()), FIRST_SEEN))
|
| 295 |
write_csv("bluetooth_assigned_numbers.csv", bn, with_column(list(old_bn[0].keys()), FIRST_SEEN))
|
| 296 |
|
| 297 |
summary, summary_fields = build_summary(read_csv(DATA / "vendor_summary.csv"), vi)
|
| 298 |
write_csv("vendor_summary.csv", summary, summary_fields)
|
| 299 |
|
| 300 |
-
manifest = build_manifest(old_manifest,
|
| 301 |
write_csv("source_manifest.csv", manifest, list(old_manifest[0].keys()))
|
| 302 |
|
| 303 |
print("vendor_identifiers", len(vi), "bluetooth_assigned_numbers", len(bn))
|
|
|
|
| 266 |
return manifest
|
| 267 |
|
| 268 |
|
| 269 |
+
UPSTREAM_FILES = [COMPANY_FILE, MEMBER_FILE, APPEARANCE_FILE] + [rel for rel, _ in SIG_TABLES.values()]
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
def upstream_urls():
|
| 273 |
+
"""Map local upstream file name -> download URL."""
|
| 274 |
+
urls = {"oui.csv": OUI_URL}
|
| 275 |
+
urls.update({rel.replace("/", "_"): SIG_BASE + rel for rel in UPSTREAM_FILES})
|
| 276 |
+
return urls
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def upstream_hashes(upstream):
|
| 280 |
+
hashes = {rel: sha256(local_name(upstream, rel)) for rel in UPSTREAM_FILES}
|
| 281 |
+
hashes["oui"] = sha256(upstream / "oui.csv")
|
| 282 |
+
return hashes
|
| 283 |
+
|
| 284 |
+
|
| 285 |
def main():
|
| 286 |
ap = argparse.ArgumentParser()
|
| 287 |
ap.add_argument("--upstream", required=True, type=Path)
|
| 288 |
ap.add_argument("--date", required=True)
|
| 289 |
ap.add_argument("--seed", type=Path, help="record_id,first_seen_snapshot CSV")
|
| 290 |
args = ap.parse_args()
|
| 291 |
+
build(upstream, date, args.seed)
|
| 292 |
+
|
| 293 |
|
| 294 |
+
def build(upstream, date, seed_path=None):
|
| 295 |
+
hashes = upstream_hashes(upstream)
|
| 296 |
+
seed = {r["record_id"]: r[FIRST_SEEN] for r in read_csv(seed_path)} if seed_path else {}
|
|
|
|
| 297 |
|
| 298 |
old_manifest = read_csv(DATA / "source_manifest.csv")
|
| 299 |
old_appearance_sha = {m["source_sha256"] for m in old_manifest if m["source_url"].endswith(APPEARANCE_FILE)}
|
|
|
|
| 304 |
old_vi = read_csv(DATA / "vendor_identifiers.csv")
|
| 305 |
old_bn = read_csv(DATA / "bluetooth_assigned_numbers.csv")
|
| 306 |
|
| 307 |
+
vi = build_vendor_identifiers(upstream, date, VendorMatcher(vendors), hashes)
|
| 308 |
+
bn = build_assigned_numbers(upstream, date, old_bn, hashes)
|
| 309 |
+
vi = stamp_first_seen(vi, old_vi, seed, date)
|
| 310 |
+
bn = stamp_first_seen(bn, old_bn, seed, date)
|
| 311 |
write_csv("vendor_identifiers.csv", vi, with_column(list(old_vi[0].keys()), FIRST_SEEN))
|
| 312 |
write_csv("bluetooth_assigned_numbers.csv", bn, with_column(list(old_bn[0].keys()), FIRST_SEEN))
|
| 313 |
|
| 314 |
summary, summary_fields = build_summary(read_csv(DATA / "vendor_summary.csv"), vi)
|
| 315 |
write_csv("vendor_summary.csv", summary, summary_fields)
|
| 316 |
|
| 317 |
+
manifest = build_manifest(old_manifest, date, hashes, vi, bn)
|
| 318 |
write_csv("source_manifest.csv", manifest, list(old_manifest[0].keys()))
|
| 319 |
|
| 320 |
print("vendor_identifiers", len(vi), "bluetooth_assigned_numbers", len(bn))
|
scripts/refresh.py
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Scheduled refresh: fetch upstream, rebuild, describe the change, open a PR.
|
| 3 |
+
|
| 4 |
+
python3 scripts/refresh.py fetch DIR
|
| 5 |
+
Download the upstream registries into DIR.
|
| 6 |
+
python3 scripts/refresh.py run --upstream DIR --body FILE [--date YYYY-MM-DD]
|
| 7 |
+
Rebuild the tables in place if any upstream file changed, update the
|
| 8 |
+
README, and write a PR description to FILE. Prints changed=true|false
|
| 9 |
+
(also to $GITHUB_OUTPUT when set).
|
| 10 |
+
python3 scripts/refresh.py tag
|
| 11 |
+
Tag the snapshot on main as v<snapshot date> if it has no tag yet.
|
| 12 |
+
python3 scripts/refresh.py publish --body FILE [--dry-run]
|
| 13 |
+
Close older open refresh PRs and open a new PR with the rebuilt files.
|
| 14 |
+
|
| 15 |
+
fetch and run need only the standard library; tag and publish need
|
| 16 |
+
huggingface_hub and an HF_TOKEN that can open PRs and create tags on the repo.
|
| 17 |
+
"""
|
| 18 |
+
import argparse
|
| 19 |
+
import datetime
|
| 20 |
+
import os
|
| 21 |
+
import re
|
| 22 |
+
import sys
|
| 23 |
+
import time
|
| 24 |
+
import urllib.request
|
| 25 |
+
from collections import Counter, defaultdict
|
| 26 |
+
from pathlib import Path
|
| 27 |
+
|
| 28 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 29 |
+
import build_snapshot as bs # noqa: E402
|
| 30 |
+
|
| 31 |
+
REPO_ID = "ictrun/wireless-vendor-identifiers"
|
| 32 |
+
README = bs.ROOT / "README.md"
|
| 33 |
+
PR_TITLE_PREFIX = "Automated snapshot refresh"
|
| 34 |
+
UPLOAD_PATTERNS = ["README.md", "data/*.csv"]
|
| 35 |
+
IGNORED = {"source_snapshot_date", "source_sha256", bs.FIRST_SEEN}
|
| 36 |
+
# Refuse to publish when upstream looks truncated rather than updated.
|
| 37 |
+
MAX_REMOVED = 50
|
| 38 |
+
MIN_KEPT_RATIO = 0.9
|
| 39 |
+
NAMESPACE_LABELS = {
|
| 40 |
+
"ieee_oui_ma_l": "IEEE OUI/MA-L assignments",
|
| 41 |
+
"bluetooth_sig_company_identifier": "Bluetooth Company Identifiers",
|
| 42 |
+
"bluetooth_sig_member_uuid": "Bluetooth member UUIDs",
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# ---------------------------------------------------------------- fetch
|
| 47 |
+
|
| 48 |
+
def fetch(target):
|
| 49 |
+
target.mkdir(parents=True, exist_ok=True)
|
| 50 |
+
for name, url in bs.upstream_urls().items():
|
| 51 |
+
body = download(url)
|
| 52 |
+
if not body.strip():
|
| 53 |
+
raise SystemExit("empty download: " + url)
|
| 54 |
+
if name == "oui.csv" and not body.lstrip(b"\xef\xbb\xbf").startswith(b"Registry,"):
|
| 55 |
+
raise SystemExit("unexpected oui.csv header")
|
| 56 |
+
(target / name).write_bytes(body)
|
| 57 |
+
print("fetched", name, len(body))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def download(url, attempts=4):
|
| 61 |
+
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0 (dataset refresh)"})
|
| 62 |
+
for i in range(attempts):
|
| 63 |
+
try:
|
| 64 |
+
with urllib.request.urlopen(req, timeout=60) as resp:
|
| 65 |
+
return resp.read()
|
| 66 |
+
except OSError as exc:
|
| 67 |
+
if i == attempts - 1:
|
| 68 |
+
raise
|
| 69 |
+
print("retry", url, exc)
|
| 70 |
+
time.sleep(10 * (i + 1))
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# ---------------------------------------------------------------- run
|
| 74 |
+
|
| 75 |
+
def changed_sources(upstream):
|
| 76 |
+
hashes = bs.upstream_hashes(upstream)
|
| 77 |
+
published = {m["source_url"]: m["source_sha256"] for m in bs.read_csv(bs.DATA / "source_manifest.csv")}
|
| 78 |
+
current = {bs.OUI_URL: hashes["oui"]}
|
| 79 |
+
current.update({bs.SIG_BASE + rel: hashes[rel] for rel in bs.UPSTREAM_FILES})
|
| 80 |
+
return [url for url, sha in current.items() if published.get(url) != sha]
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def comparable(row):
|
| 84 |
+
return {k: v for k, v in row.items() if k not in IGNORED}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def diff_table(old, new, name_field):
|
| 88 |
+
old_by_id = {r["record_id"]: r for r in old}
|
| 89 |
+
new_by_id = {r["record_id"]: r for r in new}
|
| 90 |
+
added = [r for r in new if r["record_id"] not in old_by_id]
|
| 91 |
+
removed = [r for r in old if r["record_id"] not in new_by_id]
|
| 92 |
+
renamed = [
|
| 93 |
+
(old_by_id[k], new_by_id[k]) for k in old_by_id
|
| 94 |
+
if k in new_by_id and comparable(old_by_id[k]) != comparable(new_by_id[k])
|
| 95 |
+
]
|
| 96 |
+
if len(removed) > MAX_REMOVED or len(new) < MIN_KEPT_RATIO * len(old):
|
| 97 |
+
raise SystemExit("refusing to publish: %d of %d rows removed" % (len(removed), len(old)))
|
| 98 |
+
return {"added": added, "removed": removed, "renamed": renamed, "name_field": name_field}
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def vendor_breakdown(rows):
|
| 102 |
+
counts = Counter(r["vendor_display_name"] for r in rows)
|
| 103 |
+
return ", ".join("%s %d" % (v, n) for v, n in counts.most_common())
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def collapsible(title, lines):
|
| 107 |
+
return "<details><summary>%s</summary>\n\n%s\n\n</details>\n" % (title, "\n".join(lines))
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def describe(vi_diff, bn_diff):
|
| 111 |
+
"""Return (changelog sentence, markdown details)."""
|
| 112 |
+
parts, details = [], []
|
| 113 |
+
by_ns = defaultdict(list)
|
| 114 |
+
for r in vi_diff["added"]:
|
| 115 |
+
by_ns[r["identifier_namespace"]].append(r)
|
| 116 |
+
for ns, label in NAMESPACE_LABELS.items():
|
| 117 |
+
rows = by_ns.get(ns, [])
|
| 118 |
+
if rows:
|
| 119 |
+
parts.append("%d %s (%s)" % (len(rows), label, vendor_breakdown(rows)))
|
| 120 |
+
details.append(collapsible("%s: +%d" % (label, len(rows)), [
|
| 121 |
+
"- `%s` %s — %s" % (r["identifier_value"], r["vendor_display_name"], r["assignment_name"])
|
| 122 |
+
for r in rows]))
|
| 123 |
+
if bn_diff["added"]:
|
| 124 |
+
listed = ", ".join("`%s` %s" % (r["assigned_number_value"], r["name"]) for r in bn_diff["added"][:12])
|
| 125 |
+
more = "" if len(bn_diff["added"]) <= 12 else ", and %d more" % (len(bn_diff["added"]) - 12)
|
| 126 |
+
parts.append("%d Bluetooth assigned numbers (%s%s)" % (len(bn_diff["added"]), listed, more))
|
| 127 |
+
details.append(collapsible("Bluetooth assigned numbers: +%d" % len(bn_diff["added"]), [
|
| 128 |
+
"- %s `%s` %s" % (r["assigned_number_namespace"], r["assigned_number_value"], r["name"])
|
| 129 |
+
for r in bn_diff["added"]]))
|
| 130 |
+
sentence = "refreshed from upstream. " + ("Added " + "; ".join(parts) + "." if parts else "No new records.")
|
| 131 |
+
review = []
|
| 132 |
+
for d in (vi_diff, bn_diff):
|
| 133 |
+
review += ["- removed `%s` (%s)" % (r["record_id"], r[d["name_field"]]) for r in d["removed"]]
|
| 134 |
+
review += ["- renamed `%s`: %s → %s" % (o["record_id"], o[d["name_field"]], n[d["name_field"]])
|
| 135 |
+
for o, n in d["renamed"]]
|
| 136 |
+
if review:
|
| 137 |
+
sentence += " %d records were removed or renamed upstream." % len(review)
|
| 138 |
+
details.insert(0, "### Needs review: removed or renamed upstream\n\n%s\n" % "\n".join(review))
|
| 139 |
+
else:
|
| 140 |
+
sentence += " No records were removed or renamed."
|
| 141 |
+
return sentence, "\n".join(details)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def wrap(text, width=78, indent=" "):
|
| 145 |
+
words, lines, line = text.split(), [], ""
|
| 146 |
+
for w in words:
|
| 147 |
+
if line and len(line) + 1 + len(w) > width - len(indent):
|
| 148 |
+
lines.append(line)
|
| 149 |
+
line = w
|
| 150 |
+
else:
|
| 151 |
+
line = (line + " " + w).strip()
|
| 152 |
+
lines.append(line)
|
| 153 |
+
return ("\n" + indent).join(lines)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def update_readme(date, vi, bn, sentence):
|
| 157 |
+
text = README.read_text(encoding="utf-8")
|
| 158 |
+
ns = Counter(r["identifier_namespace"] for r in vi)
|
| 159 |
+
replacements = [
|
| 160 |
+
(r"Current vendor identifier records: `\d+`.*?\)\.\n",
|
| 161 |
+
"Current vendor identifier records: `%d` (%d OUI, %d Company\nIdentifiers, %d member UUIDs).\n"
|
| 162 |
+
% (len(vi), ns["ieee_oui_ma_l"], ns["bluetooth_sig_company_identifier"],
|
| 163 |
+
ns["bluetooth_sig_member_uuid"])),
|
| 164 |
+
(r"Current Bluetooth assigned-number records: `\d+`\.",
|
| 165 |
+
"Current Bluetooth assigned-number records: `%d`." % len(bn)),
|
| 166 |
+
(r"Snapshot date: `[\d-]+`\.", "Snapshot date: `%s`." % date),
|
| 167 |
+
(r"note(\s+)= \{Snapshot v[\d-]+\}", r"note\g<1>= {Snapshot v%s}" % date),
|
| 168 |
+
(r"## Changelog\n\n", "## Changelog\n\n- " + wrap("`%s`: %s" % (date, sentence)) + "\n"),
|
| 169 |
+
]
|
| 170 |
+
for pattern, repl in replacements:
|
| 171 |
+
text, n = re.subn(pattern, repl, text, count=1, flags=re.S)
|
| 172 |
+
if n != 1:
|
| 173 |
+
raise SystemExit("README pattern not found: " + pattern)
|
| 174 |
+
README.write_text(text, encoding="utf-8")
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def run(upstream, date, body_path):
|
| 178 |
+
changed = changed_sources(upstream)
|
| 179 |
+
if not changed:
|
| 180 |
+
print("no upstream change")
|
| 181 |
+
return False
|
| 182 |
+
old_vi = bs.read_csv(bs.DATA / "vendor_identifiers.csv")
|
| 183 |
+
old_bn = bs.read_csv(bs.DATA / "bluetooth_assigned_numbers.csv")
|
| 184 |
+
bs.build(upstream, date)
|
| 185 |
+
vi = bs.read_csv(bs.DATA / "vendor_identifiers.csv")
|
| 186 |
+
bn = bs.read_csv(bs.DATA / "bluetooth_assigned_numbers.csv")
|
| 187 |
+
vi_diff = diff_table(old_vi, vi, "assignment_name")
|
| 188 |
+
bn_diff = diff_table(old_bn, bn, "name")
|
| 189 |
+
sentence, details = describe(vi_diff, bn_diff)
|
| 190 |
+
update_readme(date, vi, bn, sentence)
|
| 191 |
+
body = "\n".join([
|
| 192 |
+
"Snapshot `%s`, built by `scripts/refresh.py`." % date, "",
|
| 193 |
+
"%s" % sentence[0].upper() + sentence[1:], "",
|
| 194 |
+
"Rows: vendor_identifiers %d → %d, bluetooth_assigned_numbers %d → %d." % (
|
| 195 |
+
len(old_vi), len(vi), len(old_bn), len(bn)), "",
|
| 196 |
+
"Upstream files that changed:", "",
|
| 197 |
+
"\n".join("- " + u for u in changed), "",
|
| 198 |
+
details,
|
| 199 |
+
"After merging, run the workflow again (or wait for the next run) to tag `v%s`." % date,
|
| 200 |
+
])
|
| 201 |
+
Path(body_path).write_text(body, encoding="utf-8")
|
| 202 |
+
return True
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
# ---------------------------------------------------------------- hub
|
| 206 |
+
|
| 207 |
+
def tag():
|
| 208 |
+
from huggingface_hub import HfApi
|
| 209 |
+
api = HfApi()
|
| 210 |
+
date = bs.read_csv(bs.DATA / "source_manifest.csv")[0]["snapshot_date"]
|
| 211 |
+
name = "v" + date
|
| 212 |
+
tags = {t.name for t in api.list_repo_refs(REPO_ID, repo_type="dataset").tags}
|
| 213 |
+
if name in tags:
|
| 214 |
+
print("tag exists", name)
|
| 215 |
+
return
|
| 216 |
+
api.create_tag(REPO_ID, tag=name, repo_type="dataset", tag_message="Snapshot " + date)
|
| 217 |
+
print("created tag", name)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def publish(body_path, dry_run):
|
| 221 |
+
from huggingface_hub import HfApi
|
| 222 |
+
api = HfApi()
|
| 223 |
+
date = bs.read_csv(bs.DATA / "source_manifest.csv")[0]["snapshot_date"]
|
| 224 |
+
title = "%s %s" % (PR_TITLE_PREFIX, date)
|
| 225 |
+
stale = [d for d in api.get_repo_discussions(REPO_ID, repo_type="dataset")
|
| 226 |
+
if d.is_pull_request and d.status == "open" and d.title.startswith(PR_TITLE_PREFIX)]
|
| 227 |
+
if dry_run:
|
| 228 |
+
print("would close", [d.num for d in stale], "and open", title)
|
| 229 |
+
return
|
| 230 |
+
info = api.upload_folder(
|
| 231 |
+
repo_id=REPO_ID, repo_type="dataset", folder_path=str(bs.ROOT),
|
| 232 |
+
allow_patterns=UPLOAD_PATTERNS, commit_message=title,
|
| 233 |
+
commit_description=Path(body_path).read_text(encoding="utf-8"), create_pr=True,
|
| 234 |
+
)
|
| 235 |
+
print("opened", info.pr_url)
|
| 236 |
+
for d in stale:
|
| 237 |
+
api.comment_discussion(REPO_ID, d.num, "Superseded by %s." % info.pr_url, repo_type="dataset")
|
| 238 |
+
api.change_discussion_status(REPO_ID, d.num, "closed", repo_type="dataset")
|
| 239 |
+
print("closed", d.num)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def main():
|
| 243 |
+
ap = argparse.ArgumentParser()
|
| 244 |
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
| 245 |
+
f = sub.add_parser("fetch")
|
| 246 |
+
f.add_argument("dir", type=Path)
|
| 247 |
+
r = sub.add_parser("run")
|
| 248 |
+
r.add_argument("--upstream", required=True, type=Path)
|
| 249 |
+
r.add_argument("--body", required=True)
|
| 250 |
+
r.add_argument("--date", default=datetime.datetime.now(datetime.timezone.utc).date().isoformat())
|
| 251 |
+
sub.add_parser("tag")
|
| 252 |
+
p = sub.add_parser("publish")
|
| 253 |
+
p.add_argument("--body", required=True)
|
| 254 |
+
p.add_argument("--dry-run", action="store_true")
|
| 255 |
+
args = ap.parse_args()
|
| 256 |
+
|
| 257 |
+
if args.cmd == "fetch":
|
| 258 |
+
fetch(args.dir)
|
| 259 |
+
elif args.cmd == "run":
|
| 260 |
+
changed = run(args.upstream, args.date, args.body)
|
| 261 |
+
line = "changed=%s" % str(changed).lower()
|
| 262 |
+
print(line)
|
| 263 |
+
if os.environ.get("GITHUB_OUTPUT"):
|
| 264 |
+
with open(os.environ["GITHUB_OUTPUT"], "a") as fh:
|
| 265 |
+
fh.write(line + "\n")
|
| 266 |
+
elif args.cmd == "tag":
|
| 267 |
+
tag()
|
| 268 |
+
else:
|
| 269 |
+
publish(args.body, args.dry_run)
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
if __name__ == "__main__":
|
| 273 |
+
main()
|