Skip to content

Commit

Permalink
Unified run scripts and BUILD files for Python packages, part 1
Browse files Browse the repository at this point in the history
* Fixed various findings on the way.
* Added simple skeleton doctests.
* Note that all run scripts using ruff are *identical* now.
* Other pylint-based run scripts will be done in separate commits.

Change-Id: Ib7c2b12fb8863b6ef32744fb481dbb1adf5f27cb
  • Loading branch information
spt29 committed Dec 12, 2024
1 parent 80bbafa commit 8239bd6
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 85 deletions.
6 changes: 4 additions & 2 deletions packages/cmk-agent-based/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ py_doc_test(

py_pytest_main(
name = "__test__",
tags = ["no-mypy"],
deps = [
requirement("pytest"),
],
)

py_test(
Expand All @@ -62,7 +64,7 @@ py_test(
srcs = glob(["tests/**/*.py"]) + [":__test__.py"],
args = ["--config-file=$(location pyproject.toml)"],
data = ["pyproject.toml"],
main = ":__test__.py",
main = "__test__.py",
deps = [
":__test__",
":cmk-agent-based",
Expand Down
12 changes: 5 additions & 7 deletions packages/cmk-agent-based/run
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -e
JOBS=6
readonly BAZEL_CMD=${BAZEL_CMD:-bazel}
readonly ALL_LINTERS=mypy,ruff,bandit
REPO_PATH="$(git rev-parse --show-toplevel)"
readonly REPO_PATH

failure() {
test ${#@} -eq 0 || echo "$(basename "$0"):" "$@" >&2
Expand Down Expand Up @@ -118,7 +116,7 @@ setup_venv() {
# Further: uv seems to fall back to clang, see https://github.com/astral-sh/uv/issues/8036
CC="gcc" ${BAZEL_CMD} run //:create_venv
# shellcheck source=/dev/null
source "${REPO_PATH}"/.venv_uv/bin/activate
source "$("$BAZEL_CMD" info workspace)"/.venv_uv/bin/activate
SETUP_VENV_RAN=yes
}

Expand All @@ -128,22 +126,22 @@ run_clean() {
}

run_unit_tests() {
"${BAZEL_CMD}" test //packages/cmk-agent-based:all
"${BAZEL_CMD}" test "/${PWD#"$("${BAZEL_CMD}" info workspace)"}:all"
}

run_check_format() {
command -v taplo >/dev/null && RUST_LOG=warn taplo format --check --diff pyproject.toml
"${BAZEL_CMD}" lint \
--aspect:interactive=false \
--aspect:config=../../.aspect/cli/config_isort.yaml \
--aspect:config="$("$BAZEL_CMD" info workspace)"/.aspect/cli/config_isort.yaml \
--diff \
--machine \
:all
"${BAZEL_CMD}" run :format.check "$(realpath cmk)" "$(realpath tests)"
}

run_mypy() {
"${BAZEL_CMD}" build --config=mypy //packages/cmk-agent-based:all
"${BAZEL_CMD}" build --config=mypy "/${PWD#"$("${BAZEL_CMD}" info workspace)"}:all"
}

run_ruff() {
Expand All @@ -162,7 +160,7 @@ run_format() {
command -v taplo >/dev/null && RUST_LOG=warn taplo format pyproject.toml
"${BAZEL_CMD}" lint \
--aspect:interactive=false \
--aspect:config="${REPO_PATH}"/.aspect/cli/config_isort.yaml \
--aspect:config="$("$BAZEL_CMD" info workspace)"/.aspect/cli/config_isort.yaml \
--fix \
:all
"${BAZEL_CMD}" run :format "$(realpath cmk)" "$(realpath tests)"
Expand Down
4 changes: 2 additions & 2 deletions packages/cmk-ccc/run
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ run_clean() {
}

run_unit_tests() {
"${BAZEL_CMD}" test //packages/cmk-ccc:all
"${BAZEL_CMD}" test "/${PWD#"$("${BAZEL_CMD}" info workspace)"}:all"
}

run_check_format() {
Expand All @@ -141,7 +141,7 @@ run_check_format() {
}

run_mypy() {
"${BAZEL_CMD}" build --config=mypy //packages/cmk-ccc:all
"${BAZEL_CMD}" build --config=mypy "/${PWD#"$("${BAZEL_CMD}" info workspace)"}:all"
}

run_ruff() {
Expand Down
12 changes: 9 additions & 3 deletions packages/cmk-crypto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
load("@aspect_rules_py//py:defs.bzl", "py_library", "py_pytest_main", "py_test")
load("@cmk_requirements//:requirements.bzl", "requirement")
load("@rules_python//python:packaging.bzl", "py_wheel")
load("//bazel/rules:doctest.bzl", "py_doc_test")
load("//bazel/rules:package_wheel.bzl", "package_wheel")

exports_files([
Expand Down Expand Up @@ -33,6 +34,13 @@ py_library(
],
)

py_doc_test(
name = "doctest",
size = "small",
srcs = ["cmk-crypto"],
tags = ["no-mypy"],
)

py_pytest_main(
name = "__test__",
deps = [
Expand Down Expand Up @@ -65,9 +73,7 @@ py_wheel(
strip_path_prefixes = ["packages/cmk-crypto"],
# TODO: Duplicated from pyproject.toml
version = "0.9.0",
deps = [
":cmk-crypto",
],
deps = [":cmk-crypto"],
)

package_wheel(
Expand Down
16 changes: 10 additions & 6 deletions packages/cmk-crypto/run
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ setup_venv() {
# Further: uv seems to fall back to clang, see https://github.com/astral-sh/uv/issues/8036
CC="gcc" ${BAZEL_CMD} run //:create_venv
# shellcheck source=/dev/null
source "$(bazel info workspace)"/.venv_uv/bin/activate
source "$("$BAZEL_CMD" info workspace)"/.venv_uv/bin/activate
SETUP_VENV_RAN=yes
}

Expand All @@ -126,18 +126,22 @@ run_clean() {
}

run_unit_tests() {
"${BAZEL_CMD}" test //packages/cmk-crypto:all
"${BAZEL_CMD}" test "/${PWD#"$("${BAZEL_CMD}" info workspace)"}:all"
}

run_check_format() {
command -v taplo >/dev/null && RUST_LOG=warn taplo format --check --diff pyproject.toml
setup_venv
ruff check --select I --diff cmk tests
ruff format --check --diff cmk tests
"${BAZEL_CMD}" lint \
--aspect:interactive=false \
--aspect:config="$("$BAZEL_CMD" info workspace)"/.aspect/cli/config_isort.yaml \
--diff \
--machine \
:all
"${BAZEL_CMD}" run :format.check "$(realpath cmk)" "$(realpath tests)"
}

run_mypy() {
"${BAZEL_CMD}" build --config=mypy //packages/cmk-crypto:all
"${BAZEL_CMD}" build --config=mypy "/${PWD#"$("${BAZEL_CMD}" info workspace)"}:all"
}

run_ruff() {
Expand Down
40 changes: 36 additions & 4 deletions packages/cmk-graphing/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
load("@aspect_rules_py//py:defs.bzl", "py_library")
load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
load("@aspect_rules_py//py:defs.bzl", "py_library", "py_pytest_main", "py_test")
load("@cmk_requirements//:requirements.bzl", "requirement")
load("@rules_python//python:packaging.bzl", "py_wheel")
load("//bazel/rules:doctest.bzl", "py_doc_test")
load("//bazel/rules:package_wheel.bzl", "package_wheel")

exports_files([
Expand All @@ -23,15 +26,44 @@ py_library(
visibility = ["//cmk:__pkg__"],
)

py_doc_test(
name = "doctest",
size = "small",
srcs = ["cmk-graphing"],
tags = ["no-mypy"],
)

py_pytest_main(
name = "__test__",
deps = [
requirement("pytest"),
],
)

py_test(
name = "unit",
size = "small",
srcs = glob(["tests/**/*.py"]) + [":__test__.py"],
main = "__test__.py",
deps = [
":__test__",
":cmk-graphing",
requirement("pytest"),
],
)

format_multirun(
name = "format",
python = "@aspect_rules_lint//format:ruff",
)

py_wheel(
name = "wheel",
distribution = "cmk-graphing",
strip_path_prefixes = ["packages/cmk-graphing"],
# TODO: Duplicated from pyproject.toml
version = "1.0.0",
deps = [
":cmk-graphing",
],
deps = [":cmk-graphing"],
)

package_wheel(
Expand Down
41 changes: 23 additions & 18 deletions packages/cmk-graphing/run
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -e

JOBS=6
readonly BAZEL_CMD=${BAZEL_CMD:-bazel}
readonly ALL_LINTERS=mypy,pylint,bandit
readonly ALL_LINTERS=mypy,ruff,bandit

failure() {
test ${#@} -eq 0 || echo "$(basename "$0"):" "$@" >&2
Expand Down Expand Up @@ -35,7 +35,7 @@ parse_options() {
RUN_UNIT_TESTS=no
RUN_CHECK_FORMAT=no
RUN_MYPY=no
RUN_PYLINT=no
RUN_RUFF=no
RUN_BANDIT=no
RUN_FORMAT=no
RUN_DOCUMENTATION=no
Expand Down Expand Up @@ -78,7 +78,7 @@ parse_options() {
RUN_UNIT_TESTS=yes
RUN_CHECK_FORMAT=yes
RUN_MYPY=yes
RUN_PYLINT=yes
RUN_RUFF=yes
RUN_BANDIT=yes
shift
;;
Expand Down Expand Up @@ -107,7 +107,7 @@ parse_options() {
esac
done

readonly RUN_CLEAN RUN_UNIT_TESTS RUN_CHECK_FORMAT RUN_MYPY RUN_PYLINT RUN_BANDIT RUN_FORMAT RUN_DOCUMENTATION JOBS
readonly RUN_CLEAN RUN_UNIT_TESTS RUN_CHECK_FORMAT RUN_MYPY RUN_RUFF RUN_BANDIT RUN_FORMAT RUN_DOCUMENTATION JOBS
}

setup_venv() {
Expand All @@ -116,7 +116,7 @@ setup_venv() {
# Further: uv seems to fall back to clang, see https://github.com/astral-sh/uv/issues/8036
CC="gcc" ${BAZEL_CMD} run //:create_venv
# shellcheck source=/dev/null
source "$(bazel info workspace)"/.venv_uv/bin/activate
source "$("$BAZEL_CMD" info workspace)"/.venv_uv/bin/activate
SETUP_VENV_RAN=yes
}

Expand All @@ -126,24 +126,26 @@ run_clean() {
}

run_unit_tests() {
setup_venv
pytest
"${BAZEL_CMD}" test "/${PWD#"$("${BAZEL_CMD}" info workspace)"}:all"
}

run_check_format() {
command -v taplo >/dev/null && RUST_LOG=warn taplo format --check --diff pyproject.toml
setup_venv
ruff check --select I --diff cmk tests
ruff format --check --diff cmk tests
"${BAZEL_CMD}" lint \
--aspect:interactive=false \
--aspect:config="$("$BAZEL_CMD" info workspace)"/.aspect/cli/config_isort.yaml \
--diff \
--machine \
:all
"${BAZEL_CMD}" run :format.check "$(realpath cmk)" "$(realpath tests)"
}

run_mypy() {
"${BAZEL_CMD}" build --config=mypy //packages/cmk-graphing:all
"${BAZEL_CMD}" build --config=mypy "/${PWD#"$("${BAZEL_CMD}" info workspace)"}:all"
}

run_pylint() {
setup_venv
PYTHONPATH=. pylint --jobs="${JOBS}" cmk tests
run_ruff() {
"${BAZEL_CMD}" lint :all
}

run_bandit() {
Expand All @@ -156,9 +158,12 @@ run_bandit() {

run_format() {
command -v taplo >/dev/null && RUST_LOG=warn taplo format pyproject.toml
setup_venv
ruff check --select I --fix cmk tests
ruff format cmk tests
"${BAZEL_CMD}" lint \
--aspect:interactive=false \
--aspect:config="$("$BAZEL_CMD" info workspace)"/.aspect/cli/config_isort.yaml \
--fix \
:all
"${BAZEL_CMD}" run :format "$(realpath cmk)" "$(realpath tests)"
}

run_documentation() {
Expand All @@ -174,7 +179,7 @@ main() {
test ${RUN_UNIT_TESTS} = yes && run_unit_tests
test ${RUN_CHECK_FORMAT} = yes && run_check_format
test ${RUN_MYPY} = yes && run_mypy
test ${RUN_PYLINT} = yes && run_pylint
test ${RUN_RUFF} = yes && run_ruff
test ${RUN_BANDIT} = yes && run_bandit
test ${RUN_DOCUMENTATION} = yes && run_documentation
test ${RUN_FORMAT} = yes && run_format
Expand Down
43 changes: 39 additions & 4 deletions packages/cmk-server-side-calls/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
load("@aspect_rules_py//py:defs.bzl", "py_library")
load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
load("@aspect_rules_py//py:defs.bzl", "py_library", "py_pytest_main", "py_test")
load("@cmk_requirements//:requirements.bzl", "requirement")
load("@rules_python//python:packaging.bzl", "py_wheel")
load("//bazel/rules:doctest.bzl", "py_doc_test")
load("//bazel/rules:package_wheel.bzl", "package_wheel")

exports_files([
Expand All @@ -20,15 +23,47 @@ py_library(
visibility = ["//cmk:__pkg__"],
)

py_doc_test(
name = "doctest",
size = "small",
srcs = ["cmk-server-side-calls"],
tags = ["no-mypy"],
# TODO: There should be *no* imports in doctests!
deps = [requirement("pydantic")],
)

py_pytest_main(
name = "__test__",
deps = [
requirement("pytest"),
],
)

py_test(
name = "unit",
size = "small",
srcs = glob(["tests/**/*.py"]) + [":__test__.py"],
main = "__test__.py",
deps = [
":__test__",
":cmk-server-side-calls",
requirement("pydantic"),
requirement("pytest"),
],
)

format_multirun(
name = "format",
python = "@aspect_rules_lint//format:ruff",
)

py_wheel(
name = "wheel",
distribution = "cmk-server-side-calls",
strip_path_prefixes = ["packages/cmk-server-side-calls"],
# TODO: Duplicated from pyproject.toml
version = "1.0.0",
deps = [
":cmk-server-side-calls",
],
deps = [":cmk-server-side-calls"],
)

package_wheel(
Expand Down
Loading

0 comments on commit 8239bd6

Please sign in to comment.