Created
March 4, 2026 08:55
-
-
Save andrew222651/dafa0c36aa435817c976b74f00e26214 to your computer and use it in GitHub Desktop.
Check consistency across Python packages found in git submodules
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
| import subprocess | |
| import tomllib | |
| from pathlib import Path | |
| def get_submodule_commit_hash(path: Path) -> str: | |
| result = subprocess.run( | |
| ["git", "rev-parse", "HEAD"], | |
| cwd=str(path), | |
| stdout=subprocess.PIPE, | |
| text=True, | |
| check=True, | |
| ) | |
| return result.stdout.strip() | |
| def extract_project_name(pyproject_path: Path) -> str: | |
| pyproject_data = tomllib.loads(pyproject_path.read_text()) | |
| return pyproject_data["project"]["name"] | |
| def get_submodules(path: Path) -> list[Path]: | |
| result = subprocess.run( | |
| ["git", "submodule", "foreach", "--quiet", "pwd"], | |
| cwd=str(path), | |
| stdout=subprocess.PIPE, | |
| text=True, | |
| check=True, | |
| ) | |
| submodules = result.stdout.strip().splitlines() | |
| return [Path(submodule) for submodule in submodules] | |
| def collect_project_names_and_hashes( | |
| path: Path, project_data: dict[str, set[str]] | |
| ) -> None: | |
| pyproject_path = path / "pyproject.toml" | |
| if pyproject_path.exists(): | |
| project_name = extract_project_name(pyproject_path) | |
| commit_hash = get_submodule_commit_hash(path) | |
| if project_name not in project_data: | |
| project_data[project_name] = set() | |
| project_data[project_name].add(commit_hash) | |
| submodules = get_submodules(path) | |
| for submodule in submodules: | |
| collect_project_names_and_hashes(submodule, project_data) | |
| def check_for_duplicate_hashes(project_data: dict[str, set[str]]) -> None: | |
| for project_name, hashes in project_data.items(): | |
| if len(hashes) > 1: | |
| print( | |
| f"Project '{project_name}' has multiple commit hashes: {', '.join(hashes)}" | |
| ) | |
| print( | |
| "If multiple submodules for the same Python package are present," | |
| " they must have the same commit hash." | |
| ) | |
| exit(1) | |
| else: | |
| print( | |
| f"Project '{project_name}' has a single commit hash: {', '.join(hashes)}" | |
| ) | |
| def main() -> None: | |
| project_data: dict[str, set[str]] = dict() | |
| submodules = get_submodules(Path.cwd()) | |
| for submodule in submodules: | |
| collect_project_names_and_hashes(submodule, project_data) | |
| check_for_duplicate_hashes(project_data) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment