diff --git a/.github/workflows/publish_pypi.yml b/.github/workflows/publish_pypi.yml index 661f9754b6..dc86050d57 100644 --- a/.github/workflows/publish_pypi.yml +++ b/.github/workflows/publish_pypi.yml @@ -76,6 +76,7 @@ jobs: run: pipx run cibuildwheel --output-dir wheelhouse env: CIBW_ENVIRONMENT: > + PYBAMM_DISABLE_TELEMETRY="true" PYBAMM_USE_VCPKG=ON VCPKG_ROOT_DIR=C:\vcpkg VCPKG_DEFAULT_TRIPLET=x64-windows-static-md @@ -117,6 +118,8 @@ jobs: - name: Build wheels on Linux run: pipx run cibuildwheel --output-dir wheelhouse env: + CIBW_ENVIRONMENT: > + PYBAMM_DISABLE_TELEMETRY="true" CIBW_ARCHS_LINUX: x86_64 CIBW_BEFORE_ALL_LINUX: > yum -y install openblas-devel lapack-devel && @@ -243,6 +246,8 @@ jobs: python scripts/install_KLU_Sundials.py python -m cibuildwheel --output-dir wheelhouse env: + CIBW_ENVIRONMENT: > + PYBAMM_DISABLE_TELEMETRY="true" # 10.13 for Intel (macos-13), 11.0 for Apple Silicon (macos-14 and macos-latest) MACOSX_DEPLOYMENT_TARGET: ${{ matrix.os == 'macos-14' && '11.0' || '10.13' }} CIBW_ARCHS_MACOS: auto diff --git a/src/pybamm/config.py b/src/pybamm/config.py index d3f4ab70a2..f80ed93977 100644 --- a/src/pybamm/config.py +++ b/src/pybamm/config.py @@ -8,6 +8,14 @@ import time +def check_opt_out(): + opt_out = os.getenv("PYBAMM_DISABLE_TELEMETRY", "false").lower() != "false" + config = pybamm.config.read() + if config: + opt_out = opt_out or not config["enable_telemetry"] + return opt_out + + def is_running_tests(): # pragma: no cover """ Detect if the code is being run as part of a test suite or building docs with Sphinx. @@ -15,11 +23,6 @@ def is_running_tests(): # pragma: no cover Returns: bool: True if running tests or building docs, False otherwise. """ - # Check for common test runner names in command-line arguments - test_runners = ["pytest", "unittest", "nose", "trial", "nox", "tox"] - if any(runner in arg.lower() for arg in sys.argv for runner in test_runners): - return True - # Check for other common CI environment variables ci_env_vars = [ "GITHUB_ACTIONS", @@ -28,13 +31,10 @@ def is_running_tests(): # pragma: no cover "CIRCLECI", "JENKINS_URL", "GITLAB_CI", - "CIBW_TEST_COMMAND", ] if any(var in os.environ for var in ci_env_vars): return True - assert False - # Check if pytest or unittest is running if any( test_module in sys.modules for test_module in ["pytest", "unittest", "nose"] @@ -116,7 +116,7 @@ def get_input(): # pragma: no cover def generate(): - if is_running_tests(): + if is_running_tests() or check_opt_out(): return # Check if the config file already exists diff --git a/src/pybamm/telemetry.py b/src/pybamm/telemetry.py index 92a7073001..fdd7b6cad8 100644 --- a/src/pybamm/telemetry.py +++ b/src/pybamm/telemetry.py @@ -1,17 +1,8 @@ from posthog import Posthog -import os import pybamm import sys -def check_opt_out(): - opt_out = os.getenv("PYBAMM_DISABLE_TELEMETRY", "false").lower() != "false" - config = pybamm.config.read() - if config: - opt_out = opt_out or not config["enable_telemetry"] - return opt_out - - class MockTelemetry: def __init__(self): class MockLog: @@ -27,7 +18,7 @@ def capture(**kwargs): pass -if check_opt_out(): +if pybamm.config.check_opt_out(): _posthog = MockTelemetry() else: _posthog = Posthog( @@ -43,7 +34,7 @@ def disable(): _posthog.disabled = True -if check_opt_out(): # pragma: no cover +if pybamm.config.check_opt_out(): # pragma: no cover disable() @@ -52,7 +43,7 @@ def capture(event): # pragma: no cover if pybamm.config.is_running_tests() or _posthog.disabled: return - if check_opt_out(): + if pybamm.config.check_opt_out(): disable() return diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 62906b348d..9c5e79e732 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -108,10 +108,8 @@ def mock_select(*args, **kwargs): assert "Timeout reached. Defaulting to not enabling telemetry." in captured.out def test_generate_and_read(self, monkeypatch, tmp_path): - # Mock is_running_tests to return False monkeypatch.setattr(pybamm.config, "is_running_tests", lambda: False) - - # Mock ask_user_opt_in to return True + monkeypatch.setattr(pybamm.config, "check_opt_out", lambda: False) monkeypatch.setattr(pybamm.config, "ask_user_opt_in", lambda: True) # Mock telemetry capture