Skip to content

Commit

Permalink
feat: do not create __pycache__ in lib dir (#1469)
Browse files Browse the repository at this point in the history
**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)
  • Loading branch information
kkedziak-splunk authored Nov 24, 2024
1 parent 2ee1792 commit ad58e50
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 4 additions & 0 deletions splunk_add_on_ucc_framework/install_python_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/smoke/test_ucc_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 22 additions & 4 deletions tests/unit/test_install_python_libraries.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import os
import stat
from collections import namedtuple
from typing import List
from unittest import mock

import pytest
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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ad58e50

Please sign in to comment.