From ad58e50ca2b5588f6824a4da95a15e8c0857f032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20K=C4=99dziak?= Date: Sun, 24 Nov 2024 15:54:41 +0000 Subject: [PATCH] feat: do not create __pycache__ in lib dir (#1469) **Issue number:** [ADDON-76503](https://splunk.atlassian.net/browse/ADDON-76503) ### PR Type **What kind of change does this PR introduce?** * [x] Feature * [ ] Bug Fix * [ ] Refactoring (no functional or API changes) * [ ] Documentation Update * [ ] Maintenance (dependency updates, CI, etc.) ## Summary ### Changes Compiled Python files are created when UCC checks using pip whether a library is installed. This prevents it from doing that. ### User experience Output directory with lib dir that does not contain `__pycache__`. ## Checklist If an item doesn't apply to your changes, leave it unchecked. * [x] I have performed a self-review of this change according to the [development guidelines](https://splunk.github.io/addonfactory-ucc-generator/contributing/#development-guidelines) * [x] Tests have been added/modified to cover the changes [(testing doc)](https://splunk.github.io/addonfactory-ucc-generator/contributing/#build-and-test) * [ ] Changes are documented * [x] PR title and description follows the [contributing principles](https://splunk.github.io/addonfactory-ucc-generator/contributing/#pull-requests) --- .../install_python_libraries.py | 4 +++ tests/smoke/test_ucc_build.py | 1 + tests/unit/test_install_python_libraries.py | 26 ++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/splunk_add_on_ucc_framework/install_python_libraries.py b/splunk_add_on_ucc_framework/install_python_libraries.py index fa5e00783..d63130762 100644 --- a/splunk_add_on_ucc_framework/install_python_libraries.py +++ b/splunk_add_on_ucc_framework/install_python_libraries.py @@ -105,6 +105,10 @@ def _pip_is_lib_installed( try: my_env = os.environ.copy() my_env["PYTHONPATH"] = target + + # Disable writing of .pyc files (__pycache__) + my_env["PYTHONDONTWRITEBYTECODE"] = "1" + if allow_higher_version: result = _subprocess_run(command=cmd, env=my_env) if result.returncode != 0: diff --git a/tests/smoke/test_ucc_build.py b/tests/smoke/test_ucc_build.py index 2714cbb6d..0620e5b33 100644 --- a/tests/smoke/test_ucc_build.py +++ b/tests/smoke/test_ucc_build.py @@ -190,6 +190,7 @@ def test_ucc_generate_with_everything(caplog): ("appserver", "static", "alerticon.png"), ("bin", "splunk_ta_uccexample", "modalert_test_alert_helper.py"), ("appserver", "static", "js", "build", "entry_page.js.map"), + ("lib", "__pycache__"), ] for af in files_should_be_absent: actual_file_path = path.join(actual_folder, *af) diff --git a/tests/unit/test_install_python_libraries.py b/tests/unit/test_install_python_libraries.py index 8e2d4972a..12192831b 100644 --- a/tests/unit/test_install_python_libraries.py +++ b/tests/unit/test_install_python_libraries.py @@ -1,5 +1,6 @@ import os import stat +from collections import namedtuple from typing import List from unittest import mock @@ -7,6 +8,9 @@ import tests.unit.helpers as helpers from splunk_add_on_ucc_framework.global_config import OSDependentLibraryConfig +from splunk_add_on_ucc_framework import ( + install_python_libraries as install_python_libraries_module, +) from splunk_add_on_ucc_framework.install_python_libraries import ( CouldNotInstallRequirements, SplunktaucclibNotFound, @@ -369,11 +373,12 @@ def test_install_libraries_version_mismatch( 'python3 -m pip show --version cryptography | grep "Version: 41.0.5"' ) mock_subprocess_run.side_effect = ( - lambda command, shell=True, env=None, capture_output=True: MockSubprocessResult( - 1 + lambda command, shell=True, env=None, capture_output=True: ( + MockSubprocessResult(1) + if command == version_mismatch_shell_cmd + and ucc_lib_target == env["PYTHONPATH"] + else MockSubprocessResult(0) ) - if command == version_mismatch_shell_cmd and ucc_lib_target == env["PYTHONPATH"] - else MockSubprocessResult(0) ) with pytest.raises(CouldNotInstallRequirements): @@ -509,6 +514,19 @@ def test_is_pip_lib_installed_wrong_arguments(): _pip_is_lib_installed("i", "t", "l", allow_higher_version=True) +def test_is_pip_lib_installed_do_not_write_bytecode(monkeypatch): + Result = namedtuple("Result", ["returncode", "stdout", "stderr"]) + + def run(command, env): + assert command == "python3 -m pip show --version libname" + assert env["PYTHONPATH"] == "target" + assert env["PYTHONDONTWRITEBYTECODE"] == "1" + return Result(0, b"", b"") + + monkeypatch.setattr(install_python_libraries_module, "_subprocess_run", run) + assert _pip_is_lib_installed("python3", "target", "libname") + + @mock.patch("subprocess.run", autospec=True) def test_install_libraries_pip_custom_flag(mock_subprocess_run): mock_subprocess_run.return_value = MockSubprocessResult(0)