Skip to content

Commit c663ed1

Browse files
committed
Address PR review: drop unused imports, centralize zstandard import, tidy test helper
- Remove the unused create_tar_zst helper (dead since the gzip version) and the unused create_tar_zst/compress_log imports in WriterManager; drop the now-unused tarfile import in utils. - Add require_zstandard() which lazily imports zstandard and raises a clear, actionable error when the optional dependency is missing; use it from compress_file_zstd and WriterManager.compress_dir. - Make the test's _zstd_read_text helper self-contained (local import) and close the stream reader via a context manager.
1 parent 95c7863 commit c663ed1

3 files changed

Lines changed: 25 additions & 29 deletions

File tree

src/tracer/WriterManager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
from .ObjectStorageManager import ObjectStorageManager
3232
from ..utility.utils import (
33-
logger, create_tar_zst, capture_machine_id, compress_log,
34-
compress_file_zstd, ZSTD_LEVEL,
33+
logger, capture_machine_id,
34+
compress_file_zstd, require_zstandard, ZSTD_LEVEL,
3535
)
3636
import threading
3737
from collections import deque
@@ -955,7 +955,7 @@ def compress_dir(self, input_dir: str):
955955
input_dir: Path to the directory to compress
956956
"""
957957
try:
958-
import zstandard
958+
zstandard = require_zstandard()
959959
src = input_dir
960960
dst = input_dir.rstrip("/").rstrip("\\") + ".tar.zst"
961961

src/utility/utils.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import os
2828
import time
2929
import datetime
30-
import tarfile
3130
import hashlib
3231
import socket
3332
import struct
@@ -197,6 +196,24 @@ def logger(error_scale: str, string: str, timestamp: bool = False):
197196
ZSTD_LEVEL = 3
198197

199198

199+
def require_zstandard():
200+
"""
201+
Import and return the optional ``zstandard`` module.
202+
203+
Imported lazily so environments that never compress (and the pure-Python
204+
unit tests) don't need the dependency at import time. Raises a clear,
205+
actionable error if it is missing.
206+
"""
207+
try:
208+
import zstandard
209+
except ModuleNotFoundError as e:
210+
raise ModuleNotFoundError(
211+
"The 'zstandard' library is required for Zstandard compression but "
212+
"is not installed. Install it with 'pip install zstandard'."
213+
) from e
214+
return zstandard
215+
216+
200217
def compress_file_zstd(src: str, dst: str, level: int = ZSTD_LEVEL):
201218
"""
202219
Stream-compress a file to Zstandard.
@@ -205,35 +222,13 @@ def compress_file_zstd(src: str, dst: str, level: int = ZSTD_LEVEL):
205222
src: Path to the source file
206223
dst: Path to write the compressed (.zst) output
207224
level: Zstandard compression level
208-
209-
zstandard is imported lazily so environments that never compress (and the
210-
pure-Python unit tests) don't require the dependency at import time.
211225
"""
212-
import zstandard
226+
zstandard = require_zstandard()
213227
cctx = zstandard.ZstdCompressor(level=level)
214228
with open(src, "rb") as f_in, open(dst, "wb") as f_out:
215229
cctx.copy_stream(f_in, f_out)
216230

217231

218-
def create_tar_zst(output_filename: str, files_to_archive: list[str], level: int = ZSTD_LEVEL):
219-
"""
220-
Create a Zstandard-compressed tar archive from a list of files.
221-
222-
Args:
223-
output_filename: Name of the output .tar.zst file
224-
files_to_archive: List of file paths to include
225-
level: Zstandard compression level
226-
"""
227-
import zstandard
228-
cctx = zstandard.ZstdCompressor(level=level)
229-
with open(output_filename, "wb") as f_out:
230-
with cctx.stream_writer(f_out) as compressor:
231-
with tarfile.open(mode="w|", fileobj=compressor) as tar:
232-
for file_path in files_to_archive:
233-
tar.add(file_path, arcname=os.path.basename(file_path))
234-
logger("info", f"Created tar.zst archive: {output_filename}")
235-
236-
237232
def compress_log(input_file: str):
238233
"""
239234
Compress a log file using Zstandard.

tests/test_writer_upload.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@
3939

4040
def _zstd_read_text(path):
4141
"""Decompress a .zst file to text for round-trip assertions."""
42+
import zstandard
4243
dctx = zstandard.ZstdDecompressor()
43-
with open(path, "rb") as f:
44-
return dctx.stream_reader(f).read().decode()
44+
with open(path, "rb") as f, dctx.stream_reader(f) as reader:
45+
return reader.read().decode()
4546

4647

4748
from src.tracer.WriterManager import WriteManager

0 commit comments

Comments
 (0)