From 2b91b8a5d6a43c6b62e577a4ca8c85ec0666a0fe Mon Sep 17 00:00:00 2001 From: Josh Cannon Date: Fri, 22 Sep 2023 18:32:28 -0500 Subject: [PATCH] Move `check_inits.py` into Pants (#19919) This change moves the pre-commit-hook (which ummm is that run in CI?!?!?!) into an `experimental_test_shell_command`. --- BUILD | 28 ++++++++++++++++++++++++ build-support/bin/check_inits.py | 36 ------------------------------- build-support/githooks/pre-commit | 3 --- 3 files changed, 28 insertions(+), 39 deletions(-) delete mode 100755 build-support/bin/check_inits.py diff --git a/BUILD b/BUILD index 0ab81327029..fe6325bbd80 100644 --- a/BUILD +++ b/BUILD @@ -22,3 +22,31 @@ remote_environment( name="buildgrid_remote", python_bootstrap_search_path=[""], ) + +files( + name="all-__init__.py-files", + sources=[ + "**/__init__.py", + "!testprojects/**", + # These are explicit namespace packages + "!src/python/pants/__init__.py", + "!src/python/pants/testutil/__init__.py", + ], +) + +# NB: This should be in `lint` when we implement `lint` in https://github.com/pantsbuild/pants/issues/17729 +experimental_test_shell_command( + name="checks-empty-init-files", + command=""" + NONEMPTY_INITS=$(find . -type f -name "*.py" -size +0); + + if [ -n "$NONEMPTY_INITS" ]; then + echo "All \\`__init__.py\\` file should be empty, but the following had content:"; + echo "$NONEMPTY_INITS"; + exit 1; + fi + exit 0; + """, + tools=["echo", "find"], + execution_dependencies=[":all-__init__.py-files"], +) diff --git a/build-support/bin/check_inits.py b/build-support/bin/check_inits.py deleted file mode 100755 index 3ce72e062d1..00000000000 --- a/build-support/bin/check_inits.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python3 -# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). -# Licensed under the Apache License, Version 2.0 (see LICENSE). - -import itertools -from pathlib import Path - -from pants_release.common import die - -DIRS_TO_CHECK = ( - "src/python", - "tests/python", - "pants-plugins", - "build-support/bin", - "build-support/migration-support", -) - - -def main() -> None: - exclude_check = { - "src/python/pants/__init__.py", - "src/python/pants/testutil/__init__.py", - } - files = itertools.chain.from_iterable( - [Path().glob(f"{d}/**/__init__.py") for d in DIRS_TO_CHECK] - ) - non_empty_inits = {str(f) for f in files if bool(f.read_text())} - if non_empty_inits - exclude_check: - die( - "All `__init__.py` file should be empty, but the following had content: " - f"{', '.join(f for f in non_empty_inits)}" - ) - - -if __name__ == "__main__": - main() diff --git a/build-support/githooks/pre-commit b/build-support/githooks/pre-commit index a48ac618fa9..a1aeb9cb266 100755 --- a/build-support/githooks/pre-commit +++ b/build-support/githooks/pre-commit @@ -35,6 +35,3 @@ if git diff "${MERGE_BASE}" --name-only | grep src/python/pants_release/generate echo "* Checking GitHub workflows generation" $_PANTS_PRE_COMMIT_BINARY run src/python/pants_release/generate_github_workflows.py -- --check fi - -echo "* Checking __init__.py files" -$_PANTS_PRE_COMMIT_BINARY run build-support/bin/check_inits.py