Skip to content

Commit 9c4b792

Browse files
committed
refactor: get_extract_dir_for_input -> ExtractionConfig.get_extract_dir_for
1 parent ebf1f28 commit 9c4b792

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

tests/test_processing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
calculate_entropy,
1111
calculate_unknown_chunks,
1212
draw_entropy_plot,
13-
get_extract_dir_for_input,
1413
remove_inner_chunks,
1514
)
1615

@@ -155,10 +154,10 @@ def test_calculate_entropy_no_exception(path: Path, draw_plot: bool):
155154
("/extract", "/some/place/else/firmware", "firmware"),
156155
],
157156
)
158-
def test_get_extract_dir_for_input(
157+
def test_ExtractionConfig_get_extract_dir_for(
159158
extract_root: str, path: str, extract_dir_prefix: str
160159
):
161160
cfg = ExtractionConfig(extract_root=Path(extract_root), entropy_depth=0)
162-
assert get_extract_dir_for_input(cfg, Path(path)) == (
161+
assert cfg.get_extract_dir_for(Path(path)) == (
163162
cfg.extract_root / Path(extract_dir_prefix + cfg.extract_suffix)
164163
)

unblob/processing.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ class ExtractionConfig:
6565
extract_suffix: str = "_extract"
6666
handlers: Handlers = BUILTIN_HANDLERS
6767

68+
def get_extract_dir_for(self, path: Path) -> Path:
69+
"""Extraction dir under root with the name of path."""
70+
try:
71+
relative_path = path.relative_to(self.extract_root)
72+
except ValueError:
73+
# path is not inside root, i.e. it is an input file
74+
relative_path = Path(path.name)
75+
extract_name = path.name + self.extract_suffix
76+
extract_dir = self.extract_root / relative_path.with_name(extract_name)
77+
return extract_dir.expanduser().resolve()
78+
6879

6980
@terminate_gracefully
7081
def process_file(config: ExtractionConfig, path: Path) -> ProcessResult:
@@ -104,7 +115,7 @@ def process_result(pool, result):
104115
def check_extract_directory(task: Task, config: ExtractionConfig):
105116
errors = []
106117

107-
extract_dir = get_extract_dir_for_input(config, task.path)
118+
extract_dir = config.get_extract_dir_for(task.path)
108119
if extract_dir.exists():
109120
if config.force_extract:
110121
shutil.rmtree(extract_dir)
@@ -198,7 +209,7 @@ def __init__(
198209
self.size = size
199210
self.result = result
200211

201-
self.carve_dir = get_extract_dir_for_input(config, self.task.path)
212+
self.carve_dir = config.get_extract_dir_for(self.task.path)
202213

203214
def process(self):
204215
logger.debug("Processing file", path=self.task.path, size=self.size)
@@ -285,18 +296,6 @@ def _extract_chunk(self, file, chunk: ValidChunk):
285296
)
286297

287298

288-
def get_extract_dir_for_input(config: ExtractionConfig, path: Path) -> Path:
289-
"""Extraction dir under root with the name of path."""
290-
try:
291-
relative_path = path.relative_to(config.extract_root)
292-
except ValueError:
293-
# path is not inside root, i.e. it is an input file
294-
relative_path = Path(path.name)
295-
extract_name = path.name + config.extract_suffix
296-
extract_dir = config.extract_root / relative_path.with_name(extract_name)
297-
return extract_dir.expanduser().resolve()
298-
299-
300299
def remove_inner_chunks(chunks: List[ValidChunk]) -> List[ValidChunk]:
301300
"""Remove all chunks from the list which are within another bigger chunks."""
302301
if not chunks:

0 commit comments

Comments
 (0)