Skip to content

Commit

Permalink
fix: support Windows when checking library version (#1482)
Browse files Browse the repository at this point in the history
**Issue
number:[ADDON-76665](https://splunk.atlassian.net/browse/ADDON-76665)**

### PR Type

**What kind of change does this PR introduce?**
* [ ] Feature
* [x] Bug Fix
* [ ] Refactoring (no functional or API changes)
* [ ] Documentation Update
* [ ] Maintenance (dependency updates, CI, etc.)

## Summary

We were missing a command to fetch data from stdout (`pip show --version
{lib}`) for windows. For unix we use "grep" and for windows "findstr".

### Changes

Changed logic of `_pip_is_lib_installed` to also check library version
using "findstr" if "grep" returns non 0 status.

### User experience

Fixed an issue where Windows users were unable to build an add-on
because of the error `SplunktaucclibNotFound`

## 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)

---------

Co-authored-by: Artem Rys <[email protected]>
Co-authored-by: srv-rr-github-token <[email protected]>
Co-authored-by: soleksy-splunk <[email protected]>
Co-authored-by: Viktor Tsvetkov <[email protected]>
  • Loading branch information
5 people authored Nov 27, 2024
1 parent cce75b0 commit db17b5c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
28 changes: 13 additions & 15 deletions splunk_add_on_ucc_framework/install_python_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,27 @@ def _pip_is_lib_installed(

lib_installed_cmd = f"{installer} -m pip show --version {libname}"

if version and allow_higher_version:
cmd = f'{lib_installed_cmd} | grep "Version"'
elif version and not allow_higher_version:
cmd = f'{lib_installed_cmd} | grep "Version: {version}"'
else:
cmd = lib_installed_cmd

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:
return False
result_version = result.stdout.decode("utf-8").split("Version:")[1].strip()
return Version(result_version) >= Version(version)
result = _subprocess_run(command=lib_installed_cmd, env=my_env)
if result.returncode != 0 or "Version:" not in result.stdout.decode("utf-8"):
return False

if version:
pip_show_result = result.stdout.decode("utf-8").splitlines()
result_row = next(el for el in pip_show_result if el.startswith("Version:"))
result_version = result_row.split("Version:")[1].strip()
if allow_higher_version:
return Version(result_version) >= Version(version)
return Version(result_version) == Version(version)
else:
return_code = _subprocess_run(command=cmd, env=my_env).returncode
return return_code == 0
return result.returncode == 0

except OSError as e:
raise CouldNotInstallRequirements from e

Expand Down
31 changes: 23 additions & 8 deletions tests/unit/test_install_python_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest import mock

import pytest

import tests.unit.helpers as helpers
from splunk_add_on_ucc_framework.global_config import OSDependentLibraryConfig

Expand All @@ -28,8 +29,9 @@


class MockSubprocessResult:
def __init__(self, returncode):
def __init__(self, returncode, stdout=b""):
self.returncode = returncode
self.stdout = stdout


@mock.patch("subprocess.run", autospec=True)
Expand Down Expand Up @@ -281,8 +283,23 @@ def test_install_libraries_valid_os_libraries(
"valid_config_with_os_libraries.json"
)
global_config = gc.GlobalConfig(global_config_path)

mock_subprocess_run.return_value = MockSubprocessResult(0)
mock_subprocess_run.side_effect = [
MockSubprocessResult(0), # mock subprocess.run from _pip_install
MockSubprocessResult(0), # mock subprocess.run from _pip_install
MockSubprocessResult(
0, b"Version: 41.0.5"
), # mock subprocess.run from _pip_is_lib_installed
MockSubprocessResult(0), # mock subprocess.run from _pip_install
MockSubprocessResult(
0, b"Version: 41.0.5"
), # mock subprocess.run from _pip_is_lib_installed
MockSubprocessResult(0), # mock subprocess.run from _pip_install
MockSubprocessResult(
0, b"Version: 1.5.1"
), # mock subprocess.run from _pip_is_lib_installed
MockSubprocessResult(0), # mock subprocess.run from _pip_install
MockSubprocessResult(0), # mock subprocess.run from _pip_install
]
tmp_ucc_lib_target = tmp_path / "ucc-lib-target"
tmp_lib_path = tmp_path / "lib"
tmp_lib_path.mkdir()
Expand Down Expand Up @@ -369,15 +386,13 @@ def test_install_libraries_version_mismatch(
tmp_lib_reqs_file = tmp_lib_path / "requirements.txt"
tmp_lib_reqs_file.write_text("splunktaucclib\n")

version_mismatch_shell_cmd = (
'python3 -m pip show --version cryptography | grep "Version: 41.0.5"'
)
version_mismatch_shell_cmd = "python3 -m pip show --version cryptography"
mock_subprocess_run.side_effect = (
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)
else MockSubprocessResult(0, b"Version: 40.0.0")
)
)

Expand Down Expand Up @@ -521,7 +536,7 @@ 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"")
return Result(0, b"Version: 1.0.0", b"")

monkeypatch.setattr(install_python_libraries_module, "_subprocess_run", run)
assert _pip_is_lib_installed("python3", "target", "libname")
Expand Down

0 comments on commit db17b5c

Please sign in to comment.