diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee97be0..fe68c7c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: with: python-version: "3.11" - name: Install ruff - run: pip install --no-cache-dir ruff + run: pip install --no-cache-dir -r requirements-dev.txt - name: Run ruff format check run: ruff format --check . - name: Run ruff lint check diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..2aaf487 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +# Optional git hooks for Python scenario files (same checks as CI — see scripts/lint). +# Setup: pip install -r requirements-dev.txt pre-commit && pre-commit install +# On Datadog laptops with global core.hooksPath, use ./scripts/lint instead of pre-commit install. +repos: + - repo: local + hooks: + - id: ruff + name: ruff (scripts/lint) + entry: scripts/lint + language: system + types: [python] diff --git a/README.md b/README.md index 83fb003..5c84ab0 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,28 @@ Checkout #profiling-library-pager to get notified of test failures. Install go >= 1.25.1: `brew install go`, `choco install go`, etc. Install docker. +### Python lint (scenario workloads) + +CI and local checks use [Ruff](https://docs.astral.sh/ruff/). Install once: + +```sh +pip install -r requirements-dev.txt +``` + +```sh +./scripts/lint # check (matches the ci.yml ruff job) +./scripts/format # auto-fix formatting and lint +``` + +Optional git hooks (skip on Datadog laptops that use global `core.hooksPath` — run `./scripts/lint` manually instead): + +```sh +pip install pre-commit +pre-commit install +``` + +Ruff version is pinned only in `requirements-dev.txt`. + ### Running Tests ```sh diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..47ddd64 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,2 @@ +# Python dev tools for repo-wide lint/format (ci.yml, scripts/lint, scripts/format, pre-commit). +ruff==0.15.5 diff --git a/scenarios/python_safe_point_bias_3.11/README.md b/scenarios/python_safe_point_bias_3.11/README.md index d227f9b..c51ee38 100644 --- a/scenarios/python_safe_point_bias_3.11/README.md +++ b/scenarios/python_safe_point_bias_3.11/README.md @@ -10,6 +10,7 @@ Verifies that the Python profiler correctly attributes CPU time to `slow_method` def empty_method() -> None: pass + def slow_method() -> None: while time() < end_time: x = "h" + "e" + "l" + "l" + "o" + "," diff --git a/scripts/format b/scripts/format new file mode 100755 index 0000000..1302499 --- /dev/null +++ b/scripts/format @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euo pipefail + +source "$(dirname "${BASH_SOURCE[0]}")/ruff-common.sh" +ruff_common_setup +ruff_resolve_targets "$@" + +ruff check --fix "${RUFF_TARGETS[@]}" +ruff format "${RUFF_TARGETS[@]}" diff --git a/scripts/lint b/scripts/lint new file mode 100755 index 0000000..22bd196 --- /dev/null +++ b/scripts/lint @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Matches the ruff job in .github/workflows/ci.yml (see also scripts/format). +source "$(dirname "${BASH_SOURCE[0]}")/ruff-common.sh" +ruff_common_setup +ruff_resolve_targets "$@" + +ruff format --check "${RUFF_TARGETS[@]}" +ruff check "${RUFF_TARGETS[@]}" diff --git a/scripts/ruff-common.sh b/scripts/ruff-common.sh new file mode 100755 index 0000000..5a0be4b --- /dev/null +++ b/scripts/ruff-common.sh @@ -0,0 +1,17 @@ +# Shared setup for scripts/lint and scripts/format. +ruff_common_setup() { + RUFF_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" + cd "$RUFF_ROOT" + + if ! command -v ruff >/dev/null 2>&1; then + echo "ruff not found; install with: pip install -r $RUFF_ROOT/requirements-dev.txt" >&2 + exit 1 + fi +} + +ruff_resolve_targets() { + RUFF_TARGETS=("$@") + if [ "${#RUFF_TARGETS[@]}" -eq 0 ]; then + RUFF_TARGETS=(".") + fi +}