Files
rengine/scripts/fetch_port_registry.py
T
Yogesh OjhaandClaude Opus 5 2cb8d73280 style: strip explanatory comments across the codebase
The comment rule in CLAUDE.md is one line maximum, never narration: no
multi-line blocks, no module-header explainers, no sentences that explain or
justify the code to the reader. The tree had drifted a long way from it, and
most of the drift is mine.

Removed:
  232  lines of multi-line comment blocks
  203  single-line comments that argued instead of stating
  154  comments joined by so/because/otherwise/rather-than
  143  narrating comments in the frontend
  112  test docstrings (the test name already says what it asserts)
   25  test module docstrings and 25 decorative section dividers

Collapsed:
  324  multi-line docstrings to one factual line
  271  one-line docstrings that had a justifying clause bolted on

Kept: functional directives, section dividers, short field labels, alembic
revision headers, and the eight FastAPI route docstrings that render in /docs.
Where a fact was load-bearing it is in CLAUDE.md, which is where it belongs.

284 python tests and 34 frontend tests pass, ruff and ruff-format clean across
ten packages, svelte-check 0 errors, prettier and eslint clean, app.main imports.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
2026-09-11 14:11:23 +05:30

74 lines
2.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""Regenerate shared/definitions/data/port_registry.json from the IANA port registry."""
from __future__ import annotations
import csv
import io
import json
import re
import sys
import urllib.request
from pathlib import Path
SOURCE = (
"https://www.iana.org/assignments/service-names-port-numbers/"
"service-names-port-numbers.csv"
)
TARGET = (
Path(__file__).resolve().parents[1] / "shared/definitions/data/port_registry.json"
)
PROTOCOLS = ("tcp",)
MAX_DESCRIPTION = 120
UNASSIGNED = {"", "reserved", "unassigned", "de-registered", "deregistered"}
_ALNUM = re.compile(r"[^a-z0-9]")
_HOUSEKEEPING = re.compile(
r"\s*IANA assigned this well-formed service name as a replacement for.*$", re.I
)
def _same(a: str, b: str) -> bool:
return _ALNUM.sub("", a.lower()) == _ALNUM.sub("", b.lower())
def build(raw: str) -> dict[str, list[str]]:
out: dict[str, list[str]] = {}
for row in csv.DictReader(io.StringIO(raw)):
if (row["Transport Protocol"] or "").strip().lower() not in PROTOCOLS:
continue
number = (row["Port Number"] or "").strip()
name = (row["Service Name"] or "").strip().lower()
description = _HOUSEKEEPING.sub(
"", " ".join((row["Description"] or "").split())
)
if not number.isdigit() or not name or number in out:
continue
if description.lower() in UNASSIGNED or len(description) > MAX_DESCRIPTION:
continue
out[number] = [name] if _same(description, name) else [name, description]
return out
def main() -> int:
with urllib.request.urlopen(SOURCE, timeout=60) as response: # noqa: S310
raw = response.read().decode("utf-8")
registry = build(raw)
if len(registry) < 4000: # noqa: PLR2004
print(
f"refusing to write a suspiciously small registry ({len(registry)} ports)"
)
return 1
TARGET.parent.mkdir(parents=True, exist_ok=True)
TARGET.write_text(
json.dumps(registry, separators=(",", ":"), sort_keys=True) + "\n",
encoding="utf-8",
)
described = sum(1 for entry in registry.values() if len(entry) > 1)
print(f"{TARGET}: {len(registry)} ports, {described} with a description")
return 0
if __name__ == "__main__":
sys.exit(main())