-
Notifications
You must be signed in to change notification settings - Fork 4
Remove the temp dir and exclude excel files #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,11 +32,14 @@ | |
| PKG_NAME = "fosslight_binary" | ||
| logger = logging.getLogger(constant.LOGGER_NAME) | ||
|
|
||
| _REMOVE_FILE_EXTENSION = ['json', 'js'] | ||
| _REMOVE_FILE_EXTENSION = ['json', 'js', 'xlsx', 'xls', 'xlsm'] | ||
| _REMOVE_FILE_COMMAND_RESULT = ['timezone data', 'apple binary property list'] | ||
| INCLUDE_FILE_COMMAND_RESULT = ['current ar archive'] | ||
| _TEMP_DIR_PREFIX = '.fosslight_temp_' | ||
| _LOG_FILE_PREFIX = 'fosslight_log_bin_' | ||
|
|
||
| _error_logs = [] | ||
| _temp_output_path = "" | ||
| _root_path = "" | ||
| start_time = "" | ||
| finish_time = "" | ||
|
|
@@ -71,6 +74,45 @@ def get_checksum_and_tlsh(bin_with_path): | |
| return checksum_value, tlsh_value, error_msg | ||
|
|
||
|
|
||
| def _prepare_temp_dir(temp_path): | ||
| """Create the temp directory that holds intermediate output. | ||
|
|
||
| A directory left behind by a previous run killed with SIGKILL or a power | ||
| loss is removed first. Kept around, the copytree at the end of the run | ||
| would copy that run's result files into the output directory as well. | ||
| """ | ||
| global _temp_output_path | ||
|
|
||
| if os.path.isdir(temp_path): | ||
| shutil.rmtree(temp_path, ignore_errors=True) | ||
| os.makedirs(temp_path, exist_ok=True) | ||
| _temp_output_path = temp_path | ||
|
|
||
|
|
||
| def _cleanup_temp_dir(): | ||
| """Remove the temp directory if it is still there. | ||
|
|
||
| Called from the finally block of find_binaries so that no temp directory | ||
| survives, however the analysis ends (interrupt, exception, sys.exit). | ||
| """ | ||
| global _temp_output_path | ||
|
|
||
| temp_path, _temp_output_path = _temp_output_path, "" | ||
| if not temp_path or not os.path.isdir(temp_path): | ||
| return | ||
|
|
||
| temp_path = os.path.abspath(temp_path) | ||
| logging_logger = logging.getLogger(constant.LOGGER_NAME) | ||
| for handler in logging_logger.handlers[:]: | ||
| if (isinstance(handler, logging.FileHandler) | ||
| and os.path.dirname(os.path.abspath(handler.baseFilename)) == temp_path): | ||
| handler.flush() | ||
| handler.close() | ||
| logging_logger.removeHandler(handler) | ||
|
|
||
| shutil.rmtree(temp_path, ignore_errors=True) | ||
|
|
||
|
|
||
| def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]): | ||
| global logger, _result_log | ||
|
|
||
|
|
@@ -85,7 +127,8 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]): | |
| output_path = os.path.abspath(output_path) | ||
|
|
||
| original_output_path = output_path | ||
| output_path = os.path.join(output_path, '.fosslight_temp') | ||
| output_path = os.path.join(output_path, f"{_TEMP_DIR_PREFIX}{file_time}") | ||
| _prepare_temp_dir(output_path) | ||
|
|
||
| while len(output_files) < len(output_extensions): | ||
| output_files.append(None) | ||
|
Comment on lines
127
to
134
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '115,180p' src/fosslight_binary/binary_analysis.py
rg -n "file_time|strftime|datetime|def init|_prepare_temp_dir|output_path" src/fosslight_binary/binary_analysis.py src/fosslight_binary/cli.pyRepository: fosslight/fosslight_binary_scanner Length of output: 6601 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- binary_analysis.py definitions and entry flow ---'
sed -n '1,115p' src/fosslight_binary/binary_analysis.py
sed -n '220,275p' src/fosslight_binary/binary_analysis.py
printf '%s\n' '--- timestamp helper and start_time bindings ---'
rg -n -C 4 "def timestamp_for_filename|timestamp_for_filename|start_time\s*=|start_time" src
printf '%s\n' '--- CLI invocation bindings ---'
rg -n -C 5 "binary_analysis|find_binaries|init\(" src/fosslight_binary/cli.py srcRepository: fosslight/fosslight_binary_scanner Length of output: 21766 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- repository declarations for fosslight-util ---'
rg -n -i -C 3 "fosslight[-_]util|timestamp_for_filename" pyproject.toml setup.py setup.cfg requirements* tox.ini .github 2>/dev/null || true
printf '%s\n' '--- all repository references ---'
rg -n "timestamp_for_filename|fosslight_util\.time" . --glob '!*.pyc'
printf '%s\n' '--- installed distribution metadata/source candidates ---'
python3 - <<'PY'
import importlib.metadata as md
for dist in md.distributions():
name = dist.metadata.get("Name", "")
if "fosslight" in name.lower():
print(name, dist.version, dist.locate_file(""))
for f in dist.files or ():
if str(f).endswith(("time.py", "METADATA")) and ("fosslight" in str(f).lower() or str(f).endswith("METADATA")):
print(" ", f)
PYRepository: fosslight/fosslight_binary_scanner Length of output: 1479 🌐 Web query:
💡 Result: <search_synthesis> <source_evidence> Citations:
🏁 Script executed: #!/bin/bash
set -e
for url in \
https://raw.githubusercontent.com/fosslight/fosslight_util/main/fosslight_util/time.py \
https://raw.githubusercontent.com/fosslight/fosslight_util/master/fosslight_util/time.py \
https://raw.githubusercontent.com/fosslight/fosslight_util/main/src/fosslight_util/time.py \
https://raw.githubusercontent.com/fosslight/fosslight_util/master/src/fosslight_util/time.py
do
echo "--- $url ---"
curl -L --fail --silent --show-error "$url" || true
echo
doneRepository: fosslight/fosslight_binary_scanner Length of output: 5022 🏁 Script executed: #!/bin/bash
set -e
for tag in v2.2.2 v2.2.9; do
url="https://raw.githubusercontent.com/fosslight/fosslight_util/${tag}/src/fosslight_util/time.py"
echo "--- $url ---"
curl -L --fail --silent --show-error "$url" || true
echo
doneRepository: fosslight/fosslight_binary_scanner Length of output: 4732 Create a unique temporary directory for each CLI invocation. 🧰 Tools🪛 ast-grep (0.45.3)[warning] 115-180: Do not use an empty list as a default parameter Note: [CWE-710] Improper Adherence to Coding Standards (mutable default argument). (no-empty-list-as-parameter) 🤖 Prompt for AI Agents |
||
|
|
@@ -125,7 +168,7 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]): | |
| logger.error(f"Format error - {msg}") | ||
| sys.exit(1) | ||
|
|
||
| log_file = os.path.join(output_path, f"fosslight_log_bin_{file_time}.txt") | ||
| log_file = os.path.join(output_path, f"{_LOG_FILE_PREFIX}{file_time}.txt") | ||
| logger, _result_log = init_log(log_file, True, logging.INFO, logging.DEBUG, | ||
| PKG_NAME, path_to_find_bin, path_to_exclude) | ||
|
|
||
|
|
@@ -172,6 +215,22 @@ def get_file_list(path_to_find, excluded_files): | |
| def find_binaries(path_to_find_bin, output_dir, formats, kb_url="", kb_token="", simple_mode=False, | ||
| correct_mode=True, correct_filepath="", path_to_exclude=[], | ||
| all_exclude_mode=()): | ||
| """Analyze binaries, leaving no temp directory behind however the run ends. | ||
|
|
||
| Ctrl+C (KeyboardInterrupt), an unexpected exception and the sys.exit raised | ||
| by error_occured all pass through the finally block. | ||
| """ | ||
| try: | ||
| return _analyze_binaries(path_to_find_bin, output_dir, formats, kb_url, kb_token, | ||
| simple_mode, correct_mode, correct_filepath, path_to_exclude, | ||
| all_exclude_mode) | ||
| finally: | ||
| _cleanup_temp_dir() | ||
|
|
||
|
|
||
| def _analyze_binaries(path_to_find_bin, output_dir, formats, kb_url="", kb_token="", simple_mode=False, | ||
| correct_mode=True, correct_filepath="", path_to_exclude=[], | ||
| all_exclude_mode=()): | ||
| global start_time, finish_time, _root_path, _result_log | ||
|
|
||
| mode = "Normal Mode" | ||
|
|
@@ -291,7 +350,8 @@ def find_binaries(path_to_find_bin, output_dir, formats, kb_url="", kb_token="", | |
|
|
||
| try: | ||
| if os.path.isfile(log_file): | ||
| move_log_file(log_file, os.path.join(original_output_path, f"fosslight_log_bin_{timestamp_for_filename(start_time)}.txt")) | ||
| move_log_file(log_file, os.path.join(original_output_path, | ||
| f"{_LOG_FILE_PREFIX}{timestamp_for_filename(start_time)}.txt")) | ||
| else: | ||
| logger.debug("Moving binary analysis log file is skipped") | ||
| except Exception as ex: | ||
|
|
@@ -300,7 +360,6 @@ def find_binaries(path_to_find_bin, output_dir, formats, kb_url="", kb_token="", | |
| try: | ||
| if os.path.isdir(output_path): | ||
| shutil.copytree(output_path, original_output_path, dirs_exist_ok=True) | ||
| shutil.rmtree(output_path) | ||
| else: | ||
| logger.debug(f"Temp directory not found, skip moving: {output_path}") | ||
| except Exception as ex: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: fosslight/fosslight_binary_scanner
Length of output: 2648
🏁 Script executed:
Repository: fosslight/fosslight_binary_scanner
Length of output: 17340
🤖 get_repo_knowledge executed:
get_repo_knowledge fosslight/fosslight_binary_scanner /tmp/coderabbit-repo-knowledge/fosslight-fosslight-binary-scanner-9f74f22f/architectureLength of output: 19297
Bind temporary-directory cleanup to each invocation.
init()stores the directory in the module-global_temp_output_path, andfind_binaries()cleans that shared value infinally. When a second invocation overwrites it before the first cleanup runs, the first call can delete the second call's active directory and close its log handler. Pass the temporary path through the invocation or retain a per-callTemporaryDirectoryobject.🤖 Prompt for AI Agents
Source: Learnings