Skip to content

Instantly share code, notes, and snippets.

@bryanseah234
Created October 3, 2026 10:41
Show Gist options
  • Select an option

  • Save bryanseah234/b6e8c9518c7fd1816f7f0503a2010200 to your computer and use it in GitHub Desktop.

Select an option

Save bryanseah234/b6e8c9518c7fd1816f7f0503a2010200 to your computer and use it in GitHub Desktop.
nusnet-id2021-code (Python generator for NUSNET-style identifier data)
"""Generate legacy NUSNET checksum strings only on explicit command execution.The calculator preserves the original JavaScript matching/checksum behavior.Generated strings do not establish that an account exists."""import argparsefrom pathlib import Pathimport refrom typing import List, OptionalIDENTIFIER = re.compile(r"^A[0-9]{7}|U[0-9]{6,7}")CHECKSUMS = "YXWURNMLJHEAB"WEIGHTS = {"A": (1, 1, 1, 1, 1, 1), "U": (0, 1, 3, 1, 2, 7)}STOP = 10_000_000def calculate(identifier: str) -> Optional[str]: """Return the historical checksum, or None when the legacy pattern misses.""" matched = IDENTIFIER.search(identifier.upper()) if matched is None: return None value = matched.group(0) if value[0] == "U" and len(value) == 8: value = value[:3] + value[4:] total = sum(weight * int(digit) for weight, digit in zip(WEIGHTS[value[0]], value[-6:])) return value + CHECKSUMS[total % len(CHECKSUMS)]def generate_ids(prefix: str, destination: Path, start: int, stop: int, quiet: bool = False) -> int: """Append one bounded pass, preserving existing bytes and opening once.""" if prefix not in WEIGHTS or not 1 <= start <= stop <= STOP: raise ValueError("Use A or U and 1 <= start <= stop <= 10000000") if start == stop: return 0 with destination.open("a", encoding="utf-8") as output: for number in range(start, stop): value = calculate(prefix + str(number).zfill(7)) output.write(value + "\n") if not quiet: print(value) return stop - startdef main(argv: Optional[List[str]] = None) -> None: """Parse explicit generation bounds; importing this module performs no I/O.""" parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--prefix", choices=("A", "U", "both"), default="both") parser.add_argument("--start", type=int, default=1) parser.add_argument("--stop", type=int, default=STOP, help="Exclusive upper bound") parser.add_argument("--output-dir", type=Path, default=Path(".")) parser.add_argument("--quiet", action="store_true") args = parser.parse_args(argv) if not 1 <= args.start <= args.stop <= STOP: parser.error("Use 1 <= start <= stop <= 10000000") args.output_dir.mkdir(parents=True, exist_ok=True) prefixes = ("A", "U") if args.prefix == "both" else (args.prefix,) for prefix in prefixes: generate_ids(prefix, args.output_dir / ("nusnetid" + prefix + ".txt"), args.start, args.stop, args.quiet)if __name__ == "__main__": main()
# The generator now uses the Python standard library only.

Repository StandardsThis document is the authoritative list of standards every repository in thisworkspace is expected to follow. New repositories created fromtheprawntemplate inherit the baseline automatically; this file records whatthat baseline is, what is required vs. optional, and the packaging pattern forcontainer repositories.It exists so that humans and automation agents working across the fleet have asingle source of truth to conform to, rather than relying on implicitper-repository conventions.Last reviewed: 2026-09-27.## ScopeApplies to all owned repositories (currently 79: 71 non-container, 7 container,plus one history-managed repository). "Container repository" means a repositorythat builds and publishes one or more container images to GitHub ContainerRegistry (GHCR, ghcr.io).## 1. Security scanning (REQUIRED for every repo)Every repository must carry these GitHub Actions workflows. They are present intheprawntemplate and propagate to new repositories:| Workflow | File | Purpose || --- | --- | --- || CodeQL | .github/workflows/codeql.yml | Static analysis / code scanning || Semgrep | .github/workflows/semgrep.yml | Pattern-based SAST || TruffleHog | .github/workflows/trufflehog.yml | Secret scanning || Dependency Review | .github/workflows/dependency-review.yml | Blocks vulnerable dependency changes in PRs || Scorecard | .github/workflows/scorecard.yml | OpenSSF supply-chain posture || LFS Guard | .github/workflows/lfs-guard.yml | Blocks accidental large/binary commits |A repository is security-compliant when all six are present and enabled.## 2. Automation and CI (REQUIRED)- .github/workflows/ci.yml — project checks; adapt commands to the stack.- .github/dependabot.yml + .github/workflows/dependabot-auto-merge.yml and .github/workflows/auto-merge-bots.yml — dependency updates with guarded auto-merge.- .github/workflows/labeler.yml, greetings.yml, heartbeat.yml, summary.yml — repository hygiene and activity signals.- .github/workflows/deploy.yml — deployment (or a clear statement that the repo is not deployed).## 3. Governance and hygiene files (REQUIRED unless noted)| File | Requirement || --- | --- || LICENSE | Required — Apache-2.0. || NOTICE | Required — accompanies Apache-2.0. || SECURITY.md | Required. Use GitHub private "Report a vulnerability"; never a personal email. || CONTRIBUTING.md | Required. || AGENTS.md | Required — instructions for automation agents. || .github/dependabot.yml | Required. || .env.example | Required only when the project reads environment variables. Repos with no env config legitimately omit it. || .github/ISSUE_TEMPLATE/*, .github/pull_request_template.md | Required. || .gitignore, .gitattributes | Required. |## 4. Privacy and portability rules (REQUIRED)These reflect the fleet-wide remediation sweep and must hold for every repo:- No secrets in git. Use environment variables and .env.example. TruffleHog enforces this in CI; do not defeat it.- No personal contact details or private operator references in tracked files (READMEs, SECURITY.md, configs, .agents/). Vulnerability reporting goes through GitHub's private channel, not a personal email.- Windows/Linux portability. Launchers and paths must work on both; avoid hardcoded absolute paths and OS-specific-only launch scripts where a portable equivalent exists.- .agents/ hygiene. Never write secrets or personal details into .agents/STATE.md or .agents/JOURNAL.md.- PRs over direct commits to main. Changes land through reviewed pull requests.## 5. Container / GHCR packaging (REQUIRED for container repos; OPT-IN for others)Container repositories publish images to ghcr.io/<owner>/<repo>/<component>.The standard packaging pattern — verified present in all seven current containerrepositories (musicstream, facetracker, FORGE, unifiedcollector, unifiedanalyzer,ticketremaster-b, ctfsolver) — is:| File | Purpose || --- | --- || .github/workflows/docker-publish.yml | Builds and pushes image(s) to GHCR on the release trigger. || .github/scripts/ghcr-retention.cjs | Prunes old GHCR package versions to control storage. || Dockerfile (and variants) | Image build definition(s). |Opt-in scaffolding for these files lives under.github/optional/ so a new repository can adopt thepackaging pattern by copying them into place rather than hand-rolling each time.See .github/optional/README.md for enable steps.Package visibility: package visibility (private vs public) is governed by theorganization's package-creation policy (Org Settings → Packages). Individualcontainer package visibility is a deliberate, per-package decision — especiallyfor privacy-sensitive images (photo/OSINT/security tooling) whose layers becomeworld-pullable when public.## 6. Compliance statusA live compliance matrix across all owned repositories is maintained out-of-band(see repo-standards-compliance-matrix.md in the workspace release artifacts).As of the last review, 76/79 repositories carry the full security-workflow set;the known exceptions are actively-developed repositories that have not yetadopted the baseline. All seven container repositories carry the complete GHCRpipeline.## How to use this document- Creating a new repo: start from theprawntemplate; you inherit sections 1–4 automatically. If it will publish containers, adopt section 5 from .github/optional/.- Auditing an existing repo: check it against sections 1–4 (all repos) and section 5 (container repos). Missing required items are compliance gaps to fix via a reviewed PR.- Agents: treat this file as the conformance target for any repository work.

// Original checksum function retained as an independent offline test oracle.var calculateNUSMatricNumber = function (id) { var matches = id.toUpperCase().match(/^A\d{7}|U\d{6,7}/); if (matches) { var match = matches[0]; // Discard 3rd digit from U-prefixed NUSNET ID if (match[0] === 'U' && match.length === 8) { match = match.slice(0, 3) + match.slice(4); } var weights = { U: [0, 1, 3, 1, 2, 7], A: [1, 1, 1, 1, 1, 1] }[match[0]]; for (var i = 0, sum = 0, digits = match.slice(-6); i < 6; i++) { sum += weights[i] * digits[i]; } return match + 'YXWURNMLJHEAB'[sum % 13]; } };const input = JSON.parse(require("node:fs").readFileSync(0, "utf8"));process.stdout.write(JSON.stringify(input.map(calculateNUSMatricNumber)));
"""Offline generator checks; never touch retained output files or live services."""import contextlibimport importlib.utilimport ioimport jsonfrom pathlib import Pathimport subprocessimport sysimport tempfileimport typesimport unittestfrom unittest import mockROOT = Path(__file__).resolve().parents[1]def load_generator(): """Load with an I/O trap, including when testing the original unsafe module.""" spec = importlib.util.spec_from_file_location("generator_fixture", ROOT / "generate.py") module = importlib.util.module_from_spec(spec) legacy_stub = types.SimpleNamespace(eval_js=lambda _script: lambda _value: "synthetic") with mock.patch.dict(sys.modules, {"js2py": legacy_stub}): with mock.patch("builtins.open", side_effect=AssertionError("Generation during import")): with contextlib.redirect_stdout(io.StringIO()) as captured: spec.loader.exec_module(module) return module, captured.getvalue()class GeneratorTests(unittest.TestCase): """Compare old checksums independently and test only bounded temporary output.""" def test_import_does_not_generate(self): """Importing helpers must never start a multi-million-line write.""" _module, output = load_generator() self.assertEqual(output, "") def test_checksum_matches_original_javascript(self): """Exercise both prefixes, discarded digits, casing and legacy matching.""" module, _output = load_generator() samples = ["", "wrong", "A123", "a1234567", "U123456", "u1234567", "beforeU123456after", " A1234567", "A12345678", "U12345678", "A1234567", "U0000000", "A0000000"] for prefix in ("A", "U"): for number in (1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 99999, 100000, 999999, 1000000, 7654321, 9999999): samples.append(prefix + str(number).zfill(7)) reference = subprocess.run( ["node", str(ROOT / "tests" / "fixtures" / "legacy-checksum.cjs")], input=json.dumps(samples), capture_output=True, text=True, check=True, ) self.assertEqual([module.calculate(value) for value in samples], json.loads(reference.stdout)) def test_bounded_generation_appends_once_and_preserves_existing_bytes(self): """Explicit execution adds one pass to each file instead of two.""" module, _output = load_generator() with tempfile.TemporaryDirectory() as directory: target = Path(directory) for prefix in ("A", "U"): (target / ("nusnetid" + prefix + ".txt")).write_bytes(b"retained\n") result = subprocess.run( [sys.executable, str(ROOT / "generate.py"), "--start", "1", "--stop", "4", "--output-dir", directory, "--quiet"], capture_output=True, text=True, check=True, ) self.assertEqual(result.stdout, "") for prefix in ("A", "U"): expected = "retained\n" + "".join( module.calculate(prefix + str(value).zfill(7)) + "\n" for value in range(1, 4) ) self.assertEqual((target / ("nusnetid" + prefix + ".txt")).read_text(), expected) def test_invalid_range_does_not_create_output(self): """Reject invalid bounds before opening either destination.""" with tempfile.TemporaryDirectory() as directory: result = subprocess.run( [sys.executable, str(ROOT / "generate.py"), "--start", "4", "--stop", "1", "--output-dir", directory], capture_output=True, text=True, check=False, ) self.assertNotEqual(result.returncode, 0) self.assertEqual(list(Path(directory).iterdir()), []) def test_prefix_selection_and_stdout_match_written_rows(self): """A limited single-prefix command leaves the other destination absent.""" with tempfile.TemporaryDirectory() as directory: result = subprocess.run( [sys.executable, str(ROOT / "generate.py"), "--prefix", "U", "--start", "12", "--stop", "15", "--output-dir", directory], capture_output=True, text=True, check=True, ) target = Path(directory) self.assertFalse((target / "nusnetidA.txt").exists()) rows = (target / "nusnetidU.txt").read_text() self.assertEqual(rows, result.stdout) self.assertEqual(len(rows.splitlines()), 3)if __name__ == "__main__": unittest.main()
"""Preserve the shared bot-merge safety gates while cleaning up its Python style."""import copyimport importlib.utilfrom pathlib import Pathimport unittestfrom unittest.mock import patchPOLICY_PATH = Path(__file__).resolve().parents[1] / ".github/scripts/checked-bot-merge.py"SPEC = importlib.util.spec_from_file_location("merge_policy", POLICY_PATH)POLICY = importlib.util.module_from_spec(SPEC)SPEC.loader.exec_module(POLICY)class MergePolicyTests(unittest.TestCase): """Use synthetic metadata and intercept every possible GitHub operation.""" def setUp(self): self.pr = { "state": "open", "draft": False, "user": {"login": "dependabot[bot]"}, "head": {"sha": "a" * 40, "repo": {"full_name": "fixture/app"}}, "base": {"ref": "main", "repo": {"full_name": "fixture/app", "default_branch": "main"}}, "mergeable": True, "mergeable_state": "clean", } self.build = {"name": "Build", "workflow": "Build Check", "bucket": "pass", "state": "SUCCESS"} def decide(self, metadata=None, required=None, checks=None): """Supply the same independent metadata/check contract as the hosted caller.""" return POLICY.decision(metadata if metadata is not None else self.pr, required if required is not None else [self.build], checks if checks is not None else [self.build], "a" * 40, POLICY.BOTS["dependabot"]) def test_only_eligible_current_bot_heads_pass(self): """Keep ownership, default branch, draft, identity and mergeability gates.""" self.assertIsNone(self.decide()) invalid = [{"state": "closed"}, {"draft": True}, {"user": {"login": "human"}}, {"head": {"sha": "b" * 40, "repo": self.pr["head"]["repo"]}}, {"head": {"sha": "a" * 40, "repo": {"full_name": "fork/app"}}}, {"base": {"ref": "other", "repo": self.pr["base"]["repo"]}}, {"mergeable": None}] invalid += [{"mergeable_state": value} for value in ("unknown", "blocked", "dirty", "behind", "unstable", None)] for change in invalid: with self.subTest(change=change): self.assertIsNotNone(self.decide(metadata=dict(self.pr, **change))) def test_required_build_and_every_check_must_pass(self): """An absent, skipped, pending or failed required check never grants a merge.""" self.assertIsNotNone(self.decide(required=[])) self.assertIsNotNone(self.decide(checks=[])) self.assertIsNotNone(self.decide(required=[dict(self.build, workflow="Unrelated")])) for value in ("pending", "fail", "cancel", "skipping", None): self.assertIsNotNone(self.decide(required=[dict(self.build, bucket=value)])) for value in ("pending", "fail", "cancel", None): self.assertIsNotNone(self.decide(checks=[self.build, {"bucket": value}])) for value in ("NEUTRAL", "SKIPPED"): self.assertIsNotNone(self.decide(checks=[dict(self.build, state=value)])) self.assertIsNone(self.decide(checks=[self.build, {"bucket": "skipping"}])) def test_changed_head_or_api_failure_never_requests_merge(self): """The final metadata re-read protects against a concurrently updated PR.""" changed = copy.deepcopy(self.pr) changed["head"]["sha"] = "b" * 40 responses = [self.pr, [self.build], [self.build], changed] with patch.object(POLICY, "gh", side_effect=responses) as api: result = POLICY.inspect_and_merge("fixture/app", 1, POLICY.BOTS["dependabot"]) self.assertEqual(result, "PR head changed") self.assertEqual(api.call_count, 4) with patch.object(POLICY, "gh", side_effect=[self.pr, RuntimeError("offline")]): with self.assertRaises(RuntimeError): POLICY.inspect_and_merge("fixture/app", 1, POLICY.BOTS["dependabot"]) def test_success_uses_exact_sha_and_ordinary_merge_rules(self): """Keep the exact-head REST request without force or administrator bypass.""" responses = [self.pr, [self.build], [self.build], self.pr, {"merged": True}] with patch.object(POLICY, "gh", side_effect=responses) as api: result = POLICY.inspect_and_merge("fixture/app", 1, POLICY.BOTS["dependabot"]) self.assertEqual(result, "merged checked head") self.assertEqual(api.call_args.args, ( "api", "--method", "PUT", "repos/fixture/app/pulls/1/merge", "-f", "merge_method=squash", "-f", "sha=" + "a" * 40, )) def test_dry_run_never_requests_merge(self): """Inspecting an eligible head is still read-only when dry-run is selected.""" with patch.object(POLICY, "gh", side_effect=[self.pr, [self.build], [self.build], self.pr]) as api: result = POLICY.inspect_and_merge("fixture/app", 1, POLICY.BOTS["dependabot"], dry_run=True) self.assertIn("dry run", result) self.assertEqual(api.call_count, 4)if __name__ == "__main__": unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment