Skip to content

Commit

Permalink
Added code to remove pycache from splunklib installation directory be…
Browse files Browse the repository at this point in the history
…fore generating PR.
  • Loading branch information
VatsalJagani committed Mar 30, 2024
1 parent ee16a5f commit 4052af4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ def stream_events(input_script: smi.Script, inputs: smi.InputDefinition, event_w
* required: false
* default: "bin"
#### is_remove_pyc_from_splunklib_dir
* description: "Remove `.pyc` files and `__pycache__` directory from splunk-python-sdk (splunklib) installation path before generating Pull Request. Do not turn this off unless you are facing any issues explicitly."
* required: false
* default: true
## Troubleshooting
Expand All @@ -328,6 +335,7 @@ def stream_events(input_script: smi.Script, inputs: smi.InputDefinition, event_w
### v4.1
* Added `splunk_python_sdk_install_path` parameter for `splunk_python_sdk` utility. Default value is still `bin` folder, but now user can change based on their needs.
* Avoid `.pyc` files and `__pycache__` files getting into `splunk_python_sdk` installation folder and getting into Pull Request by default. You can turn this off with `is_remove_pyc_from_splunklib_dir` parameter.
### v4
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ inputs:
required: false
default: "bin"

is_remove_pyc_from_splunklib_dir:
description: "Remove `.pyc` files and `__pycache__` directory from splunk-python-sdk (splunklib) installation path before generating Pull Request. Do not turn this off unless you are facing any issues explicitly."
required: false
default: true


outputs:
stdout:
description: "Program stdout"
Expand Down Expand Up @@ -98,6 +104,7 @@ runs:
echo "logger_log_files_prefix -> ${{inputs.logger_log_files_prefix}}"
echo "logger_sourcetype -> ${{inputs.logger_sourcetype}}"
echo "splunk_python_sdk_install_path -> ${{inputs.splunk_python_sdk_install_path}}"
echo "is_remove_pyc_from_splunklib_dir -> ${{inputs.is_remove_pyc_from_splunklib_dir}}"
- name: "Install Python"
uses: actions/setup-python@v5
Expand Down Expand Up @@ -125,6 +132,7 @@ runs:
SPLUNK_logger_log_files_prefix: ${{inputs.logger_log_files_prefix}}
SPLUNK_logger_sourcetype: ${{inputs.logger_sourcetype}}
SPLUNK_splunk_python_sdk_install_path: ${{inputs.splunk_python_sdk_install_path}}
SPLUNK_is_remove_pyc_from_splunklib_dir: ${{inputs.is_remove_pyc_from_splunklib_dir}}
run: |
python -u ${{ github.action_path }}/src/main.py
Expand Down
19 changes: 19 additions & 0 deletions src/utilities/splunk_sdk_python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import os
import re
import shutil

import helpers.github_action_utils as utils
from utilities.base_utility import BaseUtility
Expand All @@ -19,6 +20,16 @@ def _get_splunklib_version(self, file_path):
utils.info("Error with getting the splunklib version.")


def remove_pycache(self, directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".pyc"):
os.remove(os.path.join(root, file))
for dir in dirs:
if dir == "__pycache__":
shutil.rmtree(os.path.join(root, dir))


def implement_utility(self):
utils.info("Adding SplunkPythonSDKUtility")

Expand All @@ -27,6 +38,10 @@ def implement_utility(self):
if not splunk_python_sdk_install_path or splunk_python_sdk_install_path == "NONE":
splunk_python_sdk_install_path = "bin"

is_remove_pyc_from_splunklib_dir = utils.str_to_boolean_default_true(
utils.get_input('is_remove_pyc_from_splunklib_dir'))
utils.info(f"is_remove_pyc_from_splunklib_dir: {is_remove_pyc_from_splunklib_dir}")

folder_to_install_splunklib = os.path.join(
self.app_write_dir, splunk_python_sdk_install_path)

Expand Down Expand Up @@ -60,6 +75,10 @@ def implement_utility(self):
utils.execute_system_command(
f'pip install splunk-sdk --target "{folder_to_install_splunklib}"')

# Removing .pyc and __pycache__
if is_remove_pyc_from_splunklib_dir:
self.remove_pycache(folder_to_install_splunklib)

new_version = self._get_splunklib_version(init_file)
utils.info(f"New splunklib version = {new_version}")

Expand Down
4 changes: 3 additions & 1 deletion tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def setup_action_yml(test_app_repo,
current_branch="NONE",
logger_log_files_prefix="NONE",
logger_sourcetype="NONE",
splunk_python_sdk_install_path="bin"):
splunk_python_sdk_install_path="bin",
is_remove_pyc_from_splunklib_dir="true"):

app_dir_path = os.path.join(os.path.dirname(__file__), "test_app_repos", test_app_repo)
print(f"TestIntegration.setup_action_yml_work -> app_dir_path={app_dir_path}")
Expand All @@ -39,6 +40,7 @@ def setup_action_yml(test_app_repo,
os.environ["SPLUNK_logger_log_files_prefix"] = logger_log_files_prefix
os.environ["SPLUNK_logger_sourcetype"] = logger_sourcetype
os.environ["SPLUNK_splunk_python_sdk_install_path"] = splunk_python_sdk_install_path
os.environ["SPLUNK_is_remove_pyc_from_splunklib_dir"] = is_remove_pyc_from_splunklib_dir

try:
yield
Expand Down
61 changes: 40 additions & 21 deletions tests/test_utility_python_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
from helpers import github_action_utils as utils


def check_no_pycache(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".pyc"):
return False
for dir in dirs:
if dir == "__pycache__":
return False
return True


def test_splunk_sdk_utility_installed_new():
with get_temp_directory() as temp_dir:
sdk_utility = SplunkPythonSDKUtility("nothing", temp_dir)
Expand All @@ -16,16 +27,16 @@ def test_splunk_sdk_utility_installed_new():
splunklib_dir = os.path.join(folder_path, 'splunklib')
init_file = os.path.join(splunklib_dir, '__init__.py')

print(f"init_file={init_file}")

assert result == init_file
assert os.path.exists(init_file)
assert check_no_pycache(folder_path)


def test_splunk_sdk_utility_installed_new_2():
with get_temp_directory() as temp_dir:
with setup_temporary_env_vars({
"SPLUNK_splunk_python_sdk_install_path": "lib",
"SPLUNK_splunk_python_sdk_install_path": "lib",
"SPLUNK_is_remove_pyc_from_splunklib_dir": "true"
}):
sdk_utility = SplunkPythonSDKUtility("nothing", temp_dir)
result = sdk_utility.implement_utility()
Expand All @@ -35,10 +46,9 @@ def test_splunk_sdk_utility_installed_new_2():
splunklib_dir = os.path.join(folder_path, 'splunklib')
init_file = os.path.join(splunklib_dir, '__init__.py')

print(f"init_file={init_file}")

assert result == init_file
assert os.path.exists(init_file)
assert check_no_pycache(folder_path)


def test_splunk_sdk_utility_upgraded_existing():
Expand Down Expand Up @@ -66,6 +76,7 @@ def test_splunk_sdk_utility_upgraded_existing():
# Validate the result
assert os.path.exists(init_file)
assert result == init_file
assert check_no_pycache(folder_path)


def test_splunk_sdk_utility_upgraded_existing_2():
Expand Down Expand Up @@ -96,6 +107,7 @@ def test_splunk_sdk_utility_upgraded_existing_2():
# Validate the result
assert os.path.exists(init_file)
assert result == init_file
assert check_no_pycache(folder_path)


def test_splunk_sdk_utility_skipped_due_to_existing_and_same_version():
Expand All @@ -118,6 +130,7 @@ def test_splunk_sdk_utility_skipped_due_to_existing_and_same_version():
# Validate the result
assert not os.path.exists(os.path.join(folder_path, 'splunk-sdk'))
assert result is None
assert check_no_pycache(folder_path)


def test_splunk_sdk_utility_skipped_due_to_existing_and_same_version_2():
Expand All @@ -143,29 +156,34 @@ def test_splunk_sdk_utility_skipped_due_to_existing_and_same_version_2():
# Validate the result
assert not os.path.exists(os.path.join(folder_path, 'splunk-sdk'))
assert result is None
assert check_no_pycache(folder_path)


def test_splunk_sdk_utility_error_upgrading_existing():
with get_temp_directory() as temp_dir:
# Prepare the temporary directory
folder_path = os.path.join(temp_dir, 'bin')
os.makedirs(folder_path)
splunklib_dir = os.path.join(folder_path, 'splunklib')
os.makedirs(splunklib_dir)
init_file = os.path.join(splunklib_dir, '__init__.py')
with open(init_file, 'w') as f:
f.write('__version_info__ = "1.0.0"')
with setup_temporary_env_vars({
"SPLUNK_is_remove_pyc_from_splunklib_dir": "true",
}):
# Prepare the temporary directory
folder_path = os.path.join(temp_dir, 'bin')
os.makedirs(folder_path)
splunklib_dir = os.path.join(folder_path, 'splunklib')
os.makedirs(splunklib_dir)
init_file = os.path.join(splunklib_dir, '__init__.py')
with open(init_file, 'w') as f:
f.write('__version_info__ = "1.0.0"')

# Initialize SplunkPythonSDKUtility instance
sdk_utility = SplunkPythonSDKUtility("nothing", temp_dir)
# Initialize SplunkPythonSDKUtility instance
sdk_utility = SplunkPythonSDKUtility("nothing", temp_dir)

# Call implement_utility function with failing upgrade
with patch.object(SplunkPythonSDKUtility, '_get_splunklib_version', return_value="2.2.2"):
with patch.object(utils, 'execute_system_command', return_value=(1, '')):
result = sdk_utility.implement_utility()
# Call implement_utility function with failing upgrade
with patch.object(SplunkPythonSDKUtility, '_get_splunklib_version', return_value="2.2.2"):
with patch.object(utils, 'execute_system_command', return_value=(1, '')):
result = sdk_utility.implement_utility()

# Validate the result
assert result is None
# Validate the result
assert result is None
assert check_no_pycache(folder_path)


def test_splunk_sdk_utility_error_upgrading_existing_2():
Expand All @@ -192,3 +210,4 @@ def test_splunk_sdk_utility_error_upgrading_existing_2():

# Validate the result
assert result is None
assert check_no_pycache(folder_path)

0 comments on commit 4052af4

Please sign in to comment.