Skip to content

Commit

Permalink
Makefile: define a PYTHON_ENV_VARS var to use with the $PYTHON var.
Browse files Browse the repository at this point in the history
Also try to address #2374.
  • Loading branch information
giampaolo committed Mar 15, 2024
1 parent 058b4cc commit aab8d09
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
70 changes: 35 additions & 35 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Configurable.
PYTHON = python3
PYTHON_ENV_VARS = PYTHONWARNINGS=always PYTHONUNBUFFERED=1 PSUTIL_DEBUG=1
ARGS =
TSCRIPT = psutil/tests/runner.py

Expand Down Expand Up @@ -47,7 +48,6 @@ BUILD_OPTS = `$(PYTHON) -c \
# In not in a virtualenv, add --user options for install commands.
INSTALL_OPTS = `$(PYTHON) -c \
"import sys; print('' if hasattr(sys, 'real_prefix') or hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix else '--user')"`
TEST_PREFIX = PSUTIL_SCRIPTS_DIR=`pwd`/scripts PYTHONWARNINGS=always PSUTIL_DEBUG=1

# if make is invoked with no arg, default to `make help`
.DEFAULT_GOAL := help
Expand Down Expand Up @@ -87,17 +87,17 @@ build: ## Compile (in parallel) without installing.
@# "build_ext -i" copies compiled *.so files in ./psutil directory in order
@# to allow "import psutil" when using the interactive interpreter from
@# within this directory.
PYTHONWARNINGS=all $(PYTHON) setup.py build_ext -i $(BUILD_OPTS)
$(PYTHON) -c "import psutil" # make sure it actually worked
$(PYTHON_ENV_VARS) $(PYTHON) setup.py build_ext -i $(BUILD_OPTS)
$(PYTHON_ENV_VARS) $(PYTHON) -c "import psutil" # make sure it actually worked

install: ## Install this package as current user in "edit" mode.
${MAKE} build
PYTHONWARNINGS=all $(PYTHON) setup.py develop $(INSTALL_OPTS)
$(PYTHON) -c "import psutil" # make sure it actually worked
$(PYTHON_ENV_VARS) $(PYTHON) setup.py develop $(INSTALL_OPTS)
$(PYTHON_ENV_VARS) $(PYTHON) -c "import psutil" # make sure it actually worked

uninstall: ## Uninstall this package via pip.
cd ..; $(PYTHON) -m pip uninstall -y -v psutil || true
$(PYTHON) scripts/internal/purge_installation.py
cd ..; $(PYTHON_ENV_VARS) $(PYTHON) -m pip uninstall -y -v psutil || true
$(PYTHON_ENV_VARS) $(PYTHON) scripts/internal/purge_installation.py

install-pip: ## Install pip (no-op if already installed).
@$(PYTHON) -c \
Expand All @@ -123,74 +123,74 @@ install-pip: ## Install pip (no-op if already installed).
setup-dev-env: ## Install GIT hooks, pip, test deps (also upgrades them).
${MAKE} install-git-hooks
${MAKE} install-pip
$(PYTHON) -m pip install $(INSTALL_OPTS) --trusted-host files.pythonhosted.org --trusted-host pypi.org --upgrade pip
$(PYTHON) -m pip install $(INSTALL_OPTS) --trusted-host files.pythonhosted.org --trusted-host pypi.org --upgrade $(PY_DEPS)
$(PYTHON_ENV_VARS) $(PYTHON) -m pip install $(INSTALL_OPTS) --trusted-host files.pythonhosted.org --trusted-host pypi.org --upgrade pip
$(PYTHON_ENV_VARS) $(PYTHON) -m pip install $(INSTALL_OPTS) --trusted-host files.pythonhosted.org --trusted-host pypi.org --upgrade $(PY_DEPS)

# ===================================================================
# Tests
# ===================================================================

test: ## Run all tests. To run a specific test do "make test ARGS=psutil.tests.test_system.TestDiskAPIs"
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS)
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS)

test-parallel: ## Run all tests in parallel.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) --parallel
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) --parallel

test-process: ## Run process-related API tests.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_process.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_process.py

test-process-all: ## Run tests which iterate over all process PIDs.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_process_all.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_process_all.py

test-system: ## Run system-related API tests.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_system.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_system.py

test-misc: ## Run miscellaneous tests.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_misc.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_misc.py

test-testutils: ## Run test utils tests.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_testutils.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_testutils.py

test-unicode: ## Test APIs dealing with strings.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_unicode.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_unicode.py

test-contracts: ## APIs sanity tests.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_contracts.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_contracts.py

test-connections: ## Test net_connections() and Process.connections().
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_connections.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_connections.py

test-posix: ## POSIX specific tests.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_posix.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_posix.py

test-platform: ## Run specific platform tests only.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_`$(PYTHON) -c 'import psutil; print([x.lower() for x in ("LINUX", "BSD", "OSX", "SUNOS", "WINDOWS", "AIX") if getattr(psutil, x)][0])'`.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_`$(PYTHON) -c 'import psutil; print([x.lower() for x in ("LINUX", "BSD", "OSX", "SUNOS", "WINDOWS", "AIX") if getattr(psutil, x)][0])'`.py

test-memleaks: ## Memory leak tests.
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_memleaks.py
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_memleaks.py

test-last-failed: ## Re-run tests which failed on last run
${MAKE} build
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) --last-failed
$(PYTHON_ENV_VARS) $(PYTHON) $(TSCRIPT) $(ARGS) --last-failed

test-coverage: ## Run test coverage.
${MAKE} build
# Note: coverage options are controlled by .coveragerc file
rm -rf .coverage htmlcov
$(TEST_PREFIX) $(PYTHON) -m coverage run -m unittest -v
$(PYTHON_ENV_VARS) $(PYTHON) -m coverage run -m unittest -v
$(PYTHON) -m coverage report
@echo "writing results to htmlcov/index.html"
$(PYTHON) -m coverage html
Expand All @@ -210,7 +210,7 @@ _pylint: ## Python pylint (not mandatory, just run it from time to time)
@git ls-files '*.py' | xargs $(PYTHON) -m pylint --rcfile=pyproject.toml --jobs=${NUM_WORKERS}

lint-c: ## Run C linter.
@git ls-files '*.c' '*.h' | xargs $(PYTHON) scripts/internal/clinter.py
@git ls-files '*.c' '*.h' | xargs $(PYTHON_ENV_VARS) $(PYTHON) scripts/internal/clinter.py

lint-rst: ## Run C linter.
@git ls-files '*.rst' | xargs rstcheck --config=pyproject.toml
Expand Down Expand Up @@ -261,20 +261,20 @@ install-git-hooks: ## Install GIT pre-commit hook.

sdist: ## Create tar.gz source distribution.
${MAKE} generate-manifest
PYTHONWARNINGS=all $(PYTHON) setup.py sdist
$(PYTHON_ENV_VARS) $(PYTHON) setup.py sdist

download-wheels-github: ## Download latest wheels hosted on github.
$(PYTHON) scripts/internal/download_wheels_github.py --tokenfile=~/.github.token
$(PYTHON_ENV_VARS) $(PYTHON) scripts/internal/download_wheels_github.py --tokenfile=~/.github.token
${MAKE} print-dist

download-wheels-appveyor: ## Download latest wheels hosted on appveyor.
$(PYTHON) scripts/internal/download_wheels_appveyor.py
$(PYTHON_ENV_VARS) $(PYTHON) scripts/internal/download_wheels_appveyor.py
${MAKE} print-dist

check-sdist: ## Check sanity of source distribution.
$(PYTHON) -m virtualenv --clear --no-wheel --quiet build/venv
build/venv/bin/python -m pip install -v --isolated --quiet dist/*.tar.gz
build/venv/bin/python -c "import os; os.chdir('build/venv'); import psutil"
$(PYTHON_ENV_VARS) $(PYTHON) -m virtualenv --clear --no-wheel --quiet build/venv
$(PYTHON_ENV_VARS) build/venv/bin/python -m pip install -v --isolated --quiet dist/*.tar.gz
$(PYTHON_ENV_VARS) build/venv/bin/python -c "import os; os.chdir('build/venv'); import psutil"
$(PYTHON) -m twine check --strict dist/*.tar.gz

check-wheels: ## Check sanity of wheels.
Expand Down Expand Up @@ -335,11 +335,11 @@ print-timeline: ## Print releases' timeline.

print-access-denied: ## Print AD exceptions
${MAKE} build
@$(TEST_PREFIX) $(PYTHON) scripts/internal/print_access_denied.py
@$(PYTHON_ENV_VARS) $(PYTHON) scripts/internal/print_access_denied.py

print-api-speed: ## Benchmark all API calls
${MAKE} build
@$(TEST_PREFIX) $(PYTHON) scripts/internal/print_api_speed.py $(ARGS)
@$(PYTHON_ENV_VARS) $(PYTHON) scripts/internal/print_api_speed.py $(ARGS)

print-downloads: ## Print PYPI download statistics
$(PYTHON) scripts/internal/print_downloads.py
Expand All @@ -356,11 +356,11 @@ grep-todos: ## Look for TODOs in the source files.

bench-oneshot: ## Benchmarks for oneshot() ctx manager (see #799).
${MAKE} build
$(TEST_PREFIX) $(PYTHON) scripts/internal/bench_oneshot.py
$(PYTHON_ENV_VARS) $(PYTHON) scripts/internal/bench_oneshot.py

bench-oneshot-2: ## Same as above but using perf module (supposed to be more precise)
${MAKE} build
$(TEST_PREFIX) $(PYTHON) scripts/internal/bench_oneshot_2.py
$(PYTHON_ENV_VARS) $(PYTHON) scripts/internal/bench_oneshot_2.py

check-broken-links: ## Look for broken links in source files.
git ls-files | xargs $(PYTHON) -Wa scripts/internal/check_broken_links.py
Expand Down
2 changes: 2 additions & 0 deletions psutil/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pickle
import socket
import stat
import sys
import unittest

import psutil
Expand Down Expand Up @@ -632,6 +633,7 @@ def test_debug(self):

with redirect_stderr(StringIO()) as f:
debug("hello")
sys.stderr.flush()
msg = f.getvalue()
assert msg.startswith("psutil-debug"), msg
self.assertIn("hello", msg)
Expand Down

0 comments on commit aab8d09

Please sign in to comment.