Light, fluffy, and always free - The AWS Local Emulator alternative
chore: add the package rename tool (report and apply) (#6556)
Hector Ventura383315194ad6
diff --git a/.github/workflows/partition-literals.yml b/.github/workflows/partition-literals.ymlindex 16c9059431..e8b472d927 100644--- a/.github/workflows/partition-literals.yml+++ b/.github/workflows/partition-literals.yml@@ -17,6 +17,7 @@ on:- 'src/main/java/**'- 'src/main/resources/aws/**'- 'tools/partition/**'+ - 'tools/rename/**'- 'tools/aws/**'- 'tools/slr/**'- 'src/main/resources/iam/service-linked-roles.tsv'@@ -32,6 +33,7 @@ on:- 'src/main/java/**'- 'src/main/resources/aws/**'- 'tools/partition/**'+ - 'tools/rename/**'- 'tools/aws/**'- 'tools/slr/**'- 'src/main/resources/iam/service-linked-roles.tsv'@@ -68,6 +70,9 @@ jobs:- name: Run partition tooling testsrun: make partition-test+ - name: Run package rename tool tests+ run: make rename-test+- name: Check partition literals against the baselinerun: make partition-checkdiff --git a/Makefile b/Makefileindex 1f999c92ee..0c50e76591 100644--- a/Makefile+++ b/Makefile@@ -59,7 +59,7 @@ PREFIX ?= $(HOME)/.local.DEFAULT_GOAL := help.PHONY: help dev build test native native-host run-native native-install native-image native-up native-down native-logs clean-sidecars clean-volumes compat docker-tests \- docs-sync docs-check docs-test partition-check partition-baseline partition-audit partition-test \+ docs-sync docs-check docs-test partition-check partition-baseline partition-audit partition-test rename-test \slr-check slr-table slr-audit slr-test \aws-data-sync aws-data-check aws-data-test \iam-namespaces-sync iam-namespaces-check iam-namespaces-verify iam-namespaces-test \@@ -198,6 +198,9 @@ partition-audit: ## Print the per-package table of remaining partition literalspartition-test: ## Run the partition tooling's unit tests$(PYTHON) -m pytest tools/partition -q+rename-test: ## Run the package rename tool's unit tests (tools/rename, reuses the partition lexer)+ $(PYTHON) -m pytest tools/rename -q+slr-check: ## CI gate: the service-linked role table must match the vendored AWS data@$(PYTHON) tools/slr/service_linked_roles.py --check || { \echo ""; \diff --git a/tools/rename/rename_package.py b/tools/rename/rename_package.pynew file mode 100755index 0000000000..88f4cdef33--- /dev/null+++ b/tools/rename/rename_package.py@@ -0,0 +1,317 @@+#!/usr/bin/env python3+"""Move Floci's Java package from io.github.hectorvent.floci to io.floci.aws, and measure what is left.++The rename lands as one mechanical cut, prepared by smaller PRs that take out everything a+text rewrite should not have to carry: inline fully-qualified names, hardcoded source paths,+class names written as strings. This tool is both halves of that:++- `report` counts every reference to the old package by where it sits, so each prep PR can+ show its count going down and the cut can be checked for leftovers;+- `apply` performs the cut, in the two commits it is landed as: `--moves-only` renames the+ directories with `git mv` (content untouched, so every file is a 100% rename), then+ `--rewrite-only` rewrites the references in tracked text files.++The scan is pattern based and re-reads the tree on every run, so code merged between now and+the cut is covered without editing this file.++Run from anywhere in the repo:+ python3 tools/rename/rename_package.py report # counts by category+ python3 tools/rename/rename_package.py report --files 20 # plus the 20 files with most non-import hits+ python3 tools/rename/rename_package.py apply --moves-only+ python3 tools/rename/rename_package.py apply --rewrite-only+"""+from __future__ import annotations++import argparse+import json+import re+import subprocess+import sys+from collections import Counter, defaultdict+from dataclasses import dataclass+from pathlib import Path++sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "partition"))+from partition_literals import code_text # noqa: E402 the same comment/string lexer as the partition gate++REPO_ROOT = Path(__file__).resolve().parents[2]++OLD_PACKAGE = "io.github.hectorvent.floci"+NEW_PACKAGE = "io.floci.aws"+OLD_GROUP = "io.github.hectorvent"+NEW_GROUP = "io.floci"+++def _path_form(package: str) -> str:+ return package.replace(".", "/")+++# Text rewrites, applied in order to every tracked text file outside EXCLUDED. The package forms+# go first so the groupId rule below only ever sees what is left of the old group.+REWRITES: list[tuple[str, str]] = [+ (OLD_PACKAGE, NEW_PACKAGE),+ (_path_form(OLD_PACKAGE), _path_form(NEW_PACKAGE)),+ # META-INF/native-image/<groupId>/<artifactId>: the artifactId stays floci.+ (f"native-image/{OLD_GROUP}/floci", f"native-image/{NEW_GROUP}/floci"),+ (f"<groupId>{OLD_GROUP}</groupId>", f"<groupId>{NEW_GROUP}</groupId>"),+]++# Directory moves: any tracked path segment run that matches the left side moves to the right.+MOVES: list[tuple[str, str]] = [+ (f"/{_path_form(OLD_PACKAGE)}/", f"/{_path_form(NEW_PACKAGE)}/"),+ (f"/native-image/{OLD_GROUP}/floci/", f"/native-image/{NEW_GROUP}/floci/"),+]++# The changelog records history under the names it had; this tool names the old package on purpose.+EXCLUDED_FILES = {"CHANGELOG.md"}+EXCLUDED_DIRS = ("tools/rename/",)++CATEGORIES = ("package", "import", "code", "string", "comment", "non-java", "other")+CATEGORY_HELP = {+ "package": "package declarations (rewritten by the cut, unavoidable)",+ "import": "import lines (rewritten by the cut, unavoidable)",+ "code": "inline fully-qualified names in code (phase 3 turns the clash-free ones into imports)",+ "string": "inside string literals: class names, source paths, logger names, @MethodSource",+ "comment": "inside comments, javadoc or text blocks",+ "non-java": "tracked non-Java files: build, resources, tools, docs",+ "other": f"the old group {OLD_GROUP} outside the forms the cut rewrites; must be zero before the cut",+}++# Longest form first: an alternation takes the first alternative that matches at a position, so a+# package reference is one hit, never also a group hit.+OCCURRENCE = re.compile(+ "|".join(re.escape(form) for form in (OLD_PACKAGE, _path_form(OLD_PACKAGE), OLD_GROUP, _path_form(OLD_GROUP))))+++@dataclass(frozen=True)+class Hit:+ path: str+ line: int+ category: str+++def tracked_files(root: Path) -> list[str]:+ out = subprocess.run(["git", "ls-files", "-z"], cwd=root, check=True, capture_output=True).stdout+ return [p for p in out.decode("utf-8").split("\0") if p]+++def is_excluded(path: str) -> bool:+ return path in EXCLUDED_FILES or path.startswith(EXCLUDED_DIRS)+++def read_text(path: Path) -> str | None:+ try:+ return path.read_text(encoding="utf-8")+ except (UnicodeDecodeError, IsADirectoryError, FileNotFoundError):+ return None+++def _rewritten_by_cut(match: str, text: str, start: int) -> bool:+ """Whether a match of the old group is part of a form REWRITES covers."""+ for old, _new in REWRITES:+ begin = max(0, start - len(old))+ window = text[begin:start + len(old)]+ offset = window.find(old)+ while offset >= 0:+ if begin + offset <= start < begin + offset + len(old):+ return True+ offset = window.find(old, offset + 1)+ return False+++def _string_spans(code_line: str) -> list[tuple[int, int]]:+ """Start/end columns of the double-quoted literals on one line of code_text output."""+ spans: list[tuple[int, int]] = []+ i = 0+ while i < len(code_line):+ char = code_line[i]+ if char == "'":+ j = i + 1+ while j < len(code_line) and code_line[j] != "'":+ j += 2 if code_line[j] == "\\" else 1+ i = j + 1+ elif char == '"':+ j = i + 1+ while j < len(code_line) and code_line[j] != '"':+ j += 2 if code_line[j] == "\\" else 1+ spans.append((i, j + 1))+ i = j + 1+ else:+ i += 1+ return spans+++def classify_java(path: str, text: str) -> list[Hit]:+ hits: list[Hit] = []+ code = code_text(text)+ lines = text.split("\n")+ code_lines = code.split("\n")+ line_starts = [0]+ for line in lines[:-1]:+ line_starts.append(line_starts[-1] + len(line) + 1)+ for match in OCCURRENCE.finditer(text):+ start = match.start()+ line_index = _line_of(line_starts, start)+ column = start - line_starts[line_index]+ if not _rewritten_by_cut(match.group(), text, start):+ hits.append(Hit(path, line_index + 1, "other"))+ continue+ if code[start] != text[start]:+ category = "comment"+ else:+ stripped = code_lines[line_index].lstrip()+ if stripped.startswith("package "):+ category = "package"+ elif stripped.startswith("import "):+ category = "import"+ elif any(begin < column < end for begin, end in _string_spans(code_lines[line_index])):+ category = "string"+ else:+ category = "code"+ hits.append(Hit(path, line_index + 1, category))+ return hits+++def _line_of(line_starts: list[int], offset: int) -> int:+ low, high = 0, len(line_starts) - 1+ while low < high:+ mid = (low + high + 1) // 2+ if line_starts[mid] <= offset:+ low = mid+ else:+ high = mid - 1+ return low+++def classify_other(path: str, text: str) -> list[Hit]:+ hits: list[Hit] = []+ for number, line in enumerate(text.split("\n"), start=1):+ for match in OCCURRENCE.finditer(line):+ category = "non-java" if _rewritten_by_cut(match.group(), line, match.start()) else "other"+ hits.append(Hit(path, number, category))+ return hits+++def scan(root: Path) -> list[Hit]:+ hits: list[Hit] = []+ for path in tracked_files(root):+ if is_excluded(path):+ continue+ text = read_text(root / path)+ if text is None or not OCCURRENCE.search(text):+ continue+ hits.extend(classify_java(path, text) if path.endswith(".java") else classify_other(path, text))+ return hits+++def moved_path(path: str) -> str | None:+ framed = "/" + path+ for old, new in MOVES:+ if old in framed:+ return framed.replace(old, new, 1)[1:]+ return None+++def report(root: Path, top_files: int, as_json: bool) -> int:+ hits = scan(root)+ counts = Counter(hit.category for hit in hits)+ files_by_category: dict[str, set[str]] = defaultdict(set)+ for hit in hits:+ files_by_category[hit.category].add(hit.path)+ moves = sum(1 for path in tracked_files(root) if moved_path(path) and not is_excluded(path))+ per_file = Counter(hit.path for hit in hits if hit.category in ("code", "string", "comment", "other"))+ if as_json:+ print(json.dumps({+ "moves": moves,+ "categories": {c: {"occurrences": counts[c], "files": len(files_by_category[c])} for c in CATEGORIES},+ "top_files": per_file.most_common(top_files),+ }, indent=2))+ return 0+ print(f"Files the cut moves: {moves}")+ print(f"{'category':<10} {'hits':>7} {'files':>6} meaning")+ for category in CATEGORIES:+ print(f"{category:<10} {counts[category]:>7} {len(files_by_category[category]):>6} {CATEGORY_HELP[category]}")+ if top_files:+ print(f"\nFiles with the most code, string, comment or other hits (top {top_files}):")+ for path, count in per_file.most_common(top_files):+ print(f"{count:>5} {path}")+ if counts["other"]:+ print("\nOld-group references the cut would not rewrite:")+ for hit in hits:+ if hit.category == "other":+ print(f" {hit.path}:{hit.line}")+ return 0+++def apply_moves(root: Path) -> int:+ targets: dict[str, str] = {}+ for path in tracked_files(root):+ if is_excluded(path):+ continue+ new = moved_path(path)+ if new:+ old_dir, new_dir = _moved_directory(path, new)+ targets[old_dir] = new_dir+ for old_dir, new_dir in sorted(targets.items()):+ (root / new_dir).parent.mkdir(parents=True, exist_ok=True)+ subprocess.run(["git", "mv", old_dir, new_dir], cwd=root, check=True)+ print(f"moved {old_dir} -> {new_dir}")+ return 0+++def _moved_directory(old: str, new: str) -> tuple[str, str]:+ """The shortest directory pair that carries the move, so one git mv covers a whole tree."""+ for old_segment, new_segment in MOVES:+ framed = "/" + old+ index = framed.find(old_segment)+ if index >= 0:+ old_dir = framed[1:index + len(old_segment) - 1]+ new_dir = old_dir[:index - 1] + new_segment[:-1] if index > 0 else new_segment[1:-1]+ return old_dir, new_dir+ raise ValueError(f"{old} does not move")+++def apply_rewrites(root: Path) -> int:+ changed = 0+ for path in tracked_files(root):+ if is_excluded(path):+ continue+ file = root / path+ text = read_text(file)+ if text is None:+ continue+ updated = text+ for old, new in REWRITES:+ updated = updated.replace(old, new)+ if updated != text:+ file.write_text(updated, encoding="utf-8")+ changed += 1+ print(f"rewrote {changed} files")+ return 0+++def main(argv: list[str] | None = None) -> int:+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ parser.add_argument("--root", type=Path, default=REPO_ROOT, help="repository root (default: this checkout)")+ commands = parser.add_subparsers(dest="command", required=True)+ report_parser = commands.add_parser("report", help="count references to the old package")+ report_parser.add_argument("--files", type=int, default=0, help="also list the N files with most non-import hits")+ report_parser.add_argument("--json", action="store_true")+ apply_parser = commands.add_parser("apply", help="perform the rename")+ step = apply_parser.add_mutually_exclusive_group()+ step.add_argument("--moves-only", action="store_true", help="git mv the trees, content untouched")+ step.add_argument("--rewrite-only", action="store_true", help="rewrite references in tracked files")+ args = parser.parse_args(argv)++ root = args.root.resolve()+ if args.command == "report":+ return report(root, args.files, args.json)+ if not args.rewrite_only:+ apply_moves(root)+ if not args.moves_only:+ apply_rewrites(root)+ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tools/rename/test_rename_package.py b/tools/rename/test_rename_package.pynew file mode 100644index 0000000000..d877e7836a--- /dev/null+++ b/tools/rename/test_rename_package.py@@ -0,0 +1,185 @@+"""Tests for rename_package.++Run with: pytest tools/rename -q (or: make rename-test)+"""+from __future__ import annotations++import subprocess+from collections import Counter+from pathlib import Path++import pytest++import rename_package as r++OLD = "io.github.hectorvent.floci"+OLD_PATH = "io/github/hectorvent/floci"+++def categories(src: str) -> Counter:+ return Counter(hit.category for hit in r.classify_java("A.java", src))+++# --------------------------------------------------------------------------- #+# Classification+# --------------------------------------------------------------------------- #+def test_package_and_import_lines_are_their_own_categories():+ src = (+ f"package {OLD}.services.sqs;\n"+ f"import {OLD}.core.common.AwsException;\n"+ f"import static {OLD}.core.common.Util.x;\n"+ )+ assert categories(src) == Counter({"package": 1, "import": 2})+++def test_each_inline_reference_counts_even_two_on_one_line():+ src = f"{OLD}.a.Tag t = new {OLD}.a.Tag();\n"+ assert categories(src) == Counter({"code": 2})+++def test_string_literals_in_both_forms_are_strings():+ src = (+ f'String c = "{OLD}.services.appsync.graphql.ReturnDirective";\n'+ f'Path p = Path.of("src/main/java/{OLD_PATH}/services");\n'+ f'@MethodSource("{OLD}.testing.PartitionMatrix#cases")\n'+ )+ assert categories(src) == Counter({"string": 3})+++def test_comments_javadoc_and_text_blocks_are_comments():+ src = (+ f"// see {OLD}.x.Y\n"+ f"/** {{@link {OLD}.x.Y}} */\n"+ f"/* {OLD}.x.Y\n {OLD}.x.Z */\n"+ f'String t = """\n {OLD}.x.Y\n """;\n'+ )+ assert categories(src) == Counter({"comment": 5})+++def test_a_url_in_a_string_does_not_start_a_comment():+ src = f'String u = "https://example.com"; {OLD}.x.Y y = null;\n'+ assert categories(src) == Counter({"code": 1})+++def test_char_literal_quote_does_not_open_a_string():+ src = f"char q = '\"'; {OLD}.x.Y y = null;\n"+ assert categories(src) == Counter({"code": 1})+++def test_reported_line_numbers_are_one_based():+ src = f"package a;\n\nclass A {{ {OLD}.x.Y y; }}\n"+ assert [(hit.line, hit.category) for hit in r.classify_java("A.java", src)] == [(3, "code")]+++def test_non_java_files_count_rewritten_forms_and_flag_the_rest():+ pom = "<groupId>io.github.hectorvent</groupId>\n<p>io.github.hectorvent.floci.arn.x</p>\n"+ assert Counter(h.category for h in r.classify_other("pom.xml", pom)) == Counter({"non-java": 2})+ readme = "see io.github.hectorvent.other and io/github/hectorvent/x\n"+ assert Counter(h.category for h in r.classify_other("README.md", readme)) == Counter({"other": 2})+++def test_old_group_alone_in_java_is_other():+ assert categories('String g = "io.github.hectorvent";\n') == Counter({"other": 1})+++def test_moved_path_covers_both_trees_and_the_native_image_dir():+ assert r.moved_path(f"src/main/java/{OLD_PATH}/a/A.java") == "src/main/java/io/floci/aws/a/A.java"+ assert r.moved_path(f"compatibility-tests/sdk-test-java/src/test/java/{OLD_PATH}/compat/C.java") == \+ "compatibility-tests/sdk-test-java/src/test/java/io/floci/aws/compat/C.java"+ assert r.moved_path("src/main/resources/META-INF/native-image/io.github.hectorvent/floci/reflect-config.json") \+ == "src/main/resources/META-INF/native-image/io.floci/floci/reflect-config.json"+ assert r.moved_path("src/main/java/com/floci/test/T.java") is None+ assert r.moved_path(f"docs/{OLD_PATH}.md") is None+++# --------------------------------------------------------------------------- #+# End to end on a throwaway repository+# --------------------------------------------------------------------------- #+FIXTURE = {+ f"src/main/java/{OLD_PATH}/services/sqs/SqsService.java": (+ f"package {OLD}.services.sqs;\n\n"+ f"import {OLD}.core.common.AwsException;\n\n"+ f"/** Uses {{@link {OLD}.core.common.AwsException}}. */\n"+ f"public class SqsService {{\n"+ f' static final String NAME = "{OLD}.services.sqs.SqsService";\n'+ f" {OLD}.services.sns.model.Tag tag;\n"+ f"}}\n"+ ),+ f"src/main/java/{OLD_PATH}/core/common/AwsException.java": f"package {OLD}.core.common;\nclass AwsException {{}}\n",+ f"src/test/java/{OLD_PATH}/services/sqs/SqsServiceTest.java": (+ f"package {OLD}.services.sqs;\n"+ f'class SqsServiceTest {{ java.nio.file.Path p = java.nio.file.Path.of("src/main/java/{OLD_PATH}"); }}\n'+ ),+ f"compatibility-tests/sdk-test-java/src/test/java/{OLD_PATH}/compat/C.java": f"package {OLD}.compat;\nclass C {{}}\n",+ "src/main/resources/META-INF/native-image/io.github.hectorvent/floci/reflect-config.json": "[]\n",+ "src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource":+ f"{OLD}.config.TlsConfigSource\n",+ "src/main/resources/application.yml": f"args: --initialize-at-run-time={OLD}.services.x.Y\n",+ "pom.xml": (+ "<project><groupId>io.github.hectorvent</groupId><artifactId>floci</artifactId>\n"+ f"<x>{OLD}.arn.fail-on-blank-region</x></project>\n"+ ),+ "tools/docs/services.yaml": f"path: src/main/java/{OLD_PATH}/services/sqs/SqsService.java\n",+ "CHANGELOG.md": f"- moved {OLD}\n",+ "tools/rename/notes.txt": f"the old package is {OLD}\n",+ "src/main/java/com/floci/test/Untouched.java": "package com.floci.test;\n",+}+++def git(root: Path, *args: str) -> str:+ return subprocess.run(["git", *args], cwd=root, check=True, capture_output=True, text=True).stdout+++def repo(tmp_path: Path) -> Path:+ for path, content in FIXTURE.items():+ file = tmp_path / path+ file.parent.mkdir(parents=True, exist_ok=True)+ file.write_text(content, encoding="utf-8")+ git(tmp_path, "init", "-q")+ git(tmp_path, "add", "-A")+ git(tmp_path, "-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false",+ "commit", "-q", "-m", "fixture")+ return tmp_path+++def test_report_counts_the_fixture(repo: Path, capsys):+ hits = r.scan(repo)+ assert Counter(hit.category for hit in hits) == Counter(+ {"package": 4, "import": 1, "code": 1, "string": 2, "comment": 1, "non-java": 5})+ assert all(not hit.path.startswith(("CHANGELOG.md", "tools/rename/")) for hit in hits)+ r.main(["--root", str(repo), "report", "--files", "3"])+ out = capsys.readouterr().out+ assert "Files the cut moves: 5" in out+++def test_moves_only_renames_without_touching_content(repo: Path):+ r.main(["--root", str(repo), "apply", "--moves-only"])+ status = git(repo, "status", "--porcelain")+ assert status and all(line.startswith("R ") for line in status.splitlines()), status+ assert "R src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java -> " \+ "src/main/java/io/floci/aws/services/sqs/SqsService.java" in status+ assert "native-image/io.floci/floci/reflect-config.json" in status+ assert f"package {OLD}.services.sqs;" in (repo / "src/main/java/io/floci/aws/services/sqs/SqsService.java").read_text()+ assert not (repo / "src/main/java" / OLD_PATH).exists()+++def test_full_apply_leaves_only_the_excluded_files(repo: Path):+ r.main(["--root", str(repo), "apply"])+ assert r.scan(repo) == []+ leftovers = git(repo, "grep", "-l", "-e", "io.github.hectorvent", "-e", "io/github/hectorvent").split()+ assert sorted(leftovers) == ["CHANGELOG.md", "tools/rename/notes.txt"]+ service = (repo / "src/main/java/io/floci/aws/services/sqs/SqsService.java").read_text()+ assert service.startswith("package io.floci.aws.services.sqs;\n\nimport io.floci.aws.core.common.AwsException;")+ assert '"io.floci.aws.services.sqs.SqsService"' in service+ assert "<groupId>io.floci</groupId><artifactId>floci</artifactId>" in (repo / "pom.xml").read_text()+ assert "io.floci.aws.arn.fail-on-blank-region" in (repo / "pom.xml").read_text()+ assert "src/main/java/io/floci/aws/services/sqs" in (repo / "tools/docs/services.yaml").read_text()+ assert (repo / "src/main/java/com/floci/test/Untouched.java").read_text() == "package com.floci.test;\n"+++def test_rewrite_is_idempotent(repo: Path):+ r.main(["--root", str(repo), "apply"])+ before = git(repo, "status", "--porcelain")+ r.main(["--root", str(repo), "apply", "--rewrite-only"])+ assert git(repo, "status", "--porcelain") == before