Skip to content

Commit

Permalink
Remove current_depth and root from process_file
Browse files Browse the repository at this point in the history
Now that the `process_file` is not called recursively, there is no need
to provide these as parameters, as their initial value should be
calculated from the `process_file` itself.

Also renamed `current_depth` to `depth`.
  • Loading branch information
kukovecz committed Jan 27, 2022
1 parent 899db61 commit 633731c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 15 deletions.
3 changes: 0 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def test_archive_success(
assert "error" not in result.output
assert "warning" not in result.output
process_file_mock.assert_called_once_with(
in_path,
in_path,
tmp_path,
max_depth=expected_depth,
Expand Down Expand Up @@ -186,15 +185,13 @@ def test_archive_multiple_files(tmp_path: Path):
assert process_file_mock.call_count == 2
assert process_file_mock.call_args_list == [
mock.call(
in_path_1,
in_path_1,
tmp_path,
max_depth=DEFAULT_DEPTH,
entropy_depth=1,
verbose=False,
),
mock.call(
in_path_2,
in_path_2,
tmp_path,
max_depth=DEFAULT_DEPTH,
Expand Down
1 change: 0 additions & 1 deletion tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def test_all_handlers(input_dir: Path, output_dir: Path, tmp_path: Path):
), f"Integration test input dir should contain at least 1 file: {input_dir}"

process_file(
root=input_dir,
path=input_dir,
extract_root=tmp_path,
max_depth=DEFAULT_DEPTH,
Expand Down
2 changes: 0 additions & 2 deletions unblob/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ def cli(
configure_logger(verbose, extract_root)
logger.info("Start processing files", count=noformat(len(files)))
for path in files:
root = path if path.is_dir() else path.parent
process_file(
root,
path,
extract_root,
max_depth=depth,
Expand Down
2 changes: 1 addition & 1 deletion unblob/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Task:
root: Path
path: Path
current_depth: int
depth: int


@attr.define
Expand Down
16 changes: 8 additions & 8 deletions unblob/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
# TODO: this function became too complex when adding entropy calculation, but
# it will be simplified in a separate branch, because the refactor is very complex
def process_file( # noqa: C901
root: Path,
path: Path,
extract_root: Path,
max_depth: int,
entropy_depth: int,
verbose: bool = False,
current_depth: int = 0,
):

root = path if path.is_dir() else path.parent
task_queue = multiprocessing.JoinableQueue()
task_queue.put(
Task(
root=root,
path=path,
current_depth=current_depth,
depth=0,
)
)

Expand Down Expand Up @@ -89,7 +89,7 @@ def _process_task(
task_queue: multiprocessing.JoinableQueue, task: Task, config: ProcessingConfig
):
log = logger.bind(path=task.path)
if task.current_depth >= config.max_depth:
if task.depth >= config.max_depth:
log.info("Reached maximum depth, stop further processing")
return

Expand All @@ -105,7 +105,7 @@ def _process_task(
Task(
root=task.root,
path=path,
current_depth=task.current_depth + 1,
depth=task.depth + 1,
)
)
return
Expand All @@ -127,14 +127,14 @@ def _process_task(
if not outer_chunks and not unknown_chunks:
# we don't consider whole files as unknown chunks, but we still want to
# calculate entropy for whole files which produced no valid chunks
if task.current_depth < config.entropy_depth:
if task.depth < config.entropy_depth:
calculate_entropy(task.path, draw_plot=config.verbose)
return

extract_dir = make_extract_dir(task.root, task.path, config.extract_root)

carved_paths = carve_unknown_chunks(extract_dir, file, unknown_chunks)
if task.current_depth < config.entropy_depth:
if task.depth < config.entropy_depth:
for carved_path in carved_paths:
calculate_entropy(carved_path, draw_plot=config.verbose)

Expand All @@ -144,7 +144,7 @@ def _process_task(
Task(
root=config.extract_root,
path=new_path,
current_depth=task.current_depth + 1,
depth=task.depth + 1,
)
)

Expand Down

0 comments on commit 633731c

Please sign in to comment.