Skip to content

Commit

Permalink
Removing pyc before commit to avoid them in PRs.
Browse files Browse the repository at this point in the history
  • Loading branch information
VatsalJagani committed Nov 15, 2024
1 parent ad48705 commit 6811bc7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,15 @@ 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."
#### remove_pyc_before_commit
* description: "Remove `.pyc` files and `__pycache__` directory from the repository before generating Pull Request. Do not turn this off unless you are facing any issues explicitly."
* required: false
* default: true


### Removed Parameters in v5
* is_remove_pyc_from_splunklib_dir
* Use remove_pyc_before_commit instead.



Expand Down
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ 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."
remove_pyc_before_commit:
description: "Remove `.pyc` files and `__pycache__` directory from the repository before generating Pull Request. Do not turn this off unless you are facing any issues explicitly."
required: false
default: true

Expand Down Expand Up @@ -104,7 +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}}"
echo "remove_pyc_before_commit -> ${{inputs.remove_pyc_before_commit}}"
- name: "Install Python"
uses: actions/setup-python@v5
Expand Down Expand Up @@ -132,7 +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}}
SPLUNK_remove_pyc_before_commit: ${{inputs.remove_pyc_before_commit}}
run: |
python -u ${{ github.action_path }}/src/main.py
Expand Down
9 changes: 9 additions & 0 deletions src/utilities/base_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from helpers.git_manager import GitHubPR, get_file_hash, get_multi_files_hash, get_folder_hash
import helpers.github_action_utils as utils
from helpers.global_variables import GlobalVariables


class BaseUtility:
Expand Down Expand Up @@ -30,6 +31,14 @@ def add(self):
else:
utils.error("File to generate hash is invalid.")

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

# Removing .pyc and __pycache__
if self.remove_pyc_before_commit:
self.remove_pycache(GlobalVariables.ORIGINAL_APP_DIR_PATH)

if hash:
utils.debug("Committing and creating PR for the code change.")
github.commit_and_pr(hash=hash)
Expand Down
8 changes: 0 additions & 8 deletions src/utilities/splunk_sdk_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ 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 @@ -75,10 +71,6 @@ 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: 2 additions & 2 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setup_action_yml(test_app_repo,
logger_log_files_prefix="NONE",
logger_sourcetype="NONE",
splunk_python_sdk_install_path="bin",
is_remove_pyc_from_splunklib_dir="true"):
remove_pyc_before_commit="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 @@ -40,7 +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
os.environ["SPLUNK_remove_pyc_before_commit"] = remove_pyc_before_commit

try:
yield
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utility_python_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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_is_remove_pyc_from_splunklib_dir": "true"
"SPLUNK_remove_pyc_before_commit": "true"
}):
sdk_utility = SplunkPythonSDKUtility("nothing", temp_dir)
result = sdk_utility.implement_utility()
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_splunk_sdk_utility_skipped_due_to_existing_and_same_version_2():
def test_splunk_sdk_utility_error_upgrading_existing():
with get_temp_directory() as temp_dir:
with setup_temporary_env_vars({
"SPLUNK_is_remove_pyc_from_splunklib_dir": "true",
"SPLUNK_remove_pyc_before_commit": "true",
}):
# Prepare the temporary directory
folder_path = os.path.join(temp_dir, 'bin')
Expand Down

0 comments on commit 6811bc7

Please sign in to comment.