Created
October 3, 2026 10:41
-
-
Save bryanseah234/b6e8c9518c7fd1816f7f0503a2010200 to your computer and use it in GitHub Desktop.
nusnet-id2021-code (Python generator for NUSNET-style identifier data)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The generator now uses the Python standard library only. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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