Skip to content

Commit f03e885

Browse files
authored
refactor: use pathlib.Path instead of os.path (#1714)
* refactor: use pathlib.Path instead of os.path * refactor(test_extractor): use pathlib.Path instead of os.path * refactor(test_requirements): use pathlib.Path instead of os.path * refactor(test_input_engine): use pathlib.Path instead of os.path * refactor(test_json): use pathlib.Path instead of os.path * refactor(test_file): use pathlib.Path instead of os.path * refactor(test_config): use pathlib.Path instead of os.path * refactor(test_merge): use pathlib.Path instead of os.path * refactor(test_strings): use pathlib.Path instead of os.path * refactor(test_language_scanner): use pathlib.Path instead of os.path * refactor(test_package_list_parser): use pathlib.Path instead of os.path * refactor(test_output_engine): use pathlib.Path instead of os.path * refactor(test_sbom): use pathlib.Path instead of os.path * refactor(test_cli): use pathlib.Path instead of os.path * refactor(test_scanner): use pathlib.Path instead of os.path * fix: unsupported operand
1 parent 521871e commit f03e885

15 files changed

+224
-235
lines changed

cve_bin_tool/extractor.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
Extraction of archives
66
"""
77
import itertools
8-
import os
98
import re
109
import shutil
1110
import sys
1211
import tarfile
1312
import tempfile
13+
from pathlib import Path
1414

1515
import zstandard
1616
from rpmfile.cli import main as rpmextract
@@ -66,7 +66,7 @@ def __init__(self, logger=None, error_mode=ErrorMode.TruncTrace):
6666
def can_extract(self, filename):
6767
"""Check if the filename is something we know how to extract"""
6868
# Do not try to extract symlinks
69-
if os.path.islink(filename):
69+
if Path(filename).is_symlink():
7070
return False
7171
for extension in itertools.chain(*self.file_extractors.values()):
7272
if filename.endswith(extension):
@@ -82,14 +82,15 @@ async def extract_file_tar(filename, extraction_path):
8282

8383
async def extract_file_rpm(self, filename, extraction_path):
8484
"""Extract rpm packages"""
85+
extraction_path_pathlib = Path(extraction_path)
8586
if sys.platform.startswith("linux"):
8687
if not await aio_inpath("rpm2cpio") or not await aio_inpath("cpio"):
8788
await rpmextract("-xC", extraction_path, filename)
8889
else:
8990
stdout, stderr, _ = await aio_run_command(["rpm2cpio", filename])
9091
if stderr or not stdout:
9192
return 1
92-
cpio_path = os.path.join(extraction_path, "data.cpio")
93+
cpio_path = str(extraction_path_pathlib / "data.cpio")
9394
async with FileIO(cpio_path, "wb") as f:
9495
await f.write(stdout)
9596
stdout, stderr, _ = await aio_run_command(
@@ -105,16 +106,16 @@ async def extract_file_rpm(self, filename, extraction_path):
105106
stdout, stderr, _ = await aio_run_command(["7z", "x", filename])
106107
if stderr or not stdout:
107108
return 1
108-
filenames = await aio_glob(os.path.join(extraction_path, "*.cpio"))
109+
filenames = await aio_glob(str(extraction_path_pathlib / "*.cpio"))
109110
if not filenames:
110111
filenames = await aio_glob(
111-
os.path.join(extraction_path, "*.cpio.zstd")
112+
str(extraction_path_pathlib / "*.cpio.zstd")
112113
)
113114
filename = filenames[0]
114115
exit_code = await self.extract_file_zst(filename, extraction_path)
115116
if exit_code:
116117
return 1
117-
filenames = await aio_glob(os.path.join(extraction_path, "*.cpio"))
118+
filenames = await aio_glob(str(extraction_path_pathlib / "*.cpio"))
118119
filename = filenames[0]
119120
stdout, stderr, _ = await aio_run_command(["7z", "x", filename])
120121
if stderr or not stdout:
@@ -128,9 +129,7 @@ async def extract_file_zst(self, filename: str, extraction_path: str) -> int:
128129
with ErrorHandler(mode=ErrorMode.Ignore) as e:
129130
if filename.endswith(".cpio.zstd"):
130131
with open(filename, "rb") as compressed:
131-
base = os.path.basename(filename)
132-
file = os.path.splitext(base)[0]
133-
output_path = os.path.join(extraction_path, file)
132+
output_path = Path(extraction_path) / Path(filename).stem
134133
with open(output_path, "wb") as destination:
135134
dctx.copy_stream(compressed, destination)
136135
else:
@@ -147,12 +146,12 @@ async def extract_file_pkg(self, filename: str, extraction_path: str) -> int:
147146
async def _extract_through_7z() -> int:
148147
"""Extract file using `7z`"""
149148

150-
temp = os.path.join(self.tempdir, f"{os.path.basename(filename)}")
149+
temp = str(Path(self.tempdir) / Path(filename).stem)
151150
stdout, stderr, _ = await aio_run_command(
152151
["7z", "x", filename, f"-o{self.tempdir}"]
153152
)
154153
stdout, stderr, _ = await aio_run_command(
155-
["7z", "x", temp[:-4], f"-o{extraction_path}"]
154+
["7z", "x", temp, f"-o{extraction_path}"]
156155
)
157156
if not stdout:
158157
return 1
@@ -185,7 +184,7 @@ async def extract_file_deb(self, filename, extraction_path):
185184
stdout, stderr, _ = await aio_run_command(["ar", "x", filename])
186185
if stderr:
187186
return 1
188-
datafile = await aio_glob(os.path.join(extraction_path, "data.tar.*"))
187+
datafile = await aio_glob(str(Path(extraction_path) / "data.tar.*"))
189188
with ErrorHandler(mode=ErrorMode.Ignore) as e:
190189
await aio_unpack_archive(datafile[0], extraction_path)
191190
return e.exit_code
@@ -285,15 +284,16 @@ def __init__(self, raise_failure=False, *args, **kwargs):
285284

286285
async def aio_extract(self, filename):
287286
"""Run the extractor"""
287+
filename_pathlib = Path(filename)
288288
# Resolve path in case of cwd change
289-
filename = os.path.abspath(filename)
289+
filename = str(filename_pathlib.resolve())
290290
for extractor in self.file_extractors:
291291
for extension in self.file_extractors[extractor]:
292292
if filename.endswith(extension):
293-
extracted_path = os.path.join(
294-
self.tempdir, f"{os.path.basename(filename)}.extracted"
293+
extracted_path = str(
294+
Path(self.tempdir) / f"{filename_pathlib.name}.extracted"
295295
)
296-
if os.path.exists(extracted_path):
296+
if Path(extracted_path).exists():
297297
await aio_rmdir(extracted_path)
298298
await aio_makedirs(extracted_path, 0o700)
299299
async with ChangeDirContext(extracted_path):

0 commit comments

Comments
 (0)