From 6dcdd3cf2b6ebc685bcad9d58549085ed0802d59 Mon Sep 17 00:00:00 2001 From: Christophe Guerreiro Date: Wed, 1 Jul 2026 11:30:35 +0200 Subject: [PATCH] cli: Add zstandard fallback import for Python < 3.14 The zstandard package is widely available across distributions and Python environments, while backports.zstd is not always packaged or readily accessible. Add zstandard as a fallback import. Make the zstd support optional by deferring dependency checks until the feature is actually used. The zstandard backend does not support text mode when opening compressed files through zstd.open(). Switch to binary mode and let json.load() consume the resulting binary steam, which is supported by both backends. Signed-off-by: Christophe Guerreiro --- CHANGELOG.md | 5 ++++- pyproject.toml | 8 ++++++-- src/spdx_diff/cli.py | 17 ++++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7294d0..2687069 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ All notable changes to this project will be documented in this file. ### Changed +- zstandard fallback import added for python <3.14 +- Zstd libraries are optional dependencies + ### Fixed ### Removed @@ -40,7 +43,7 @@ boolean options, all defaulting to `True`: - The JSON output file is no longer generated by default. - The `--output ` option have been renamed to `--json-output ` - The `--json-output ` option must now be explicitly specified to enable JSON file generation. - + ### Fixed diff --git a/pyproject.toml b/pyproject.toml index c9c0c63..a5627b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,8 +20,12 @@ classifiers = [ "Topic :: Software Development :: Build Tools", ] -dependencies = [ - "backports.zstd; python_version < '3.14'" +[project.optional-dependencies] +zstd = [ + "backports.zstd;python_version<'3.14'", +] +zstandard = [ + "zstandard;python_version<'3.14'", ] [project.scripts] diff --git a/src/spdx_diff/cli.py b/src/spdx_diff/cli.py index 83f1968..e930b80 100644 --- a/src/spdx_diff/cli.py +++ b/src/spdx_diff/cli.py @@ -17,9 +17,15 @@ from typing import Any if sys.version_info >= (3, 14): - from compression import zstd + from compression.zstd import open as open_zstd else: - from backports import zstd + try: + from backports.zstd import open as open_zstd # type: ignore[import-not-found] + except ImportError: + try: + from zstandard import open as open_zstd # type: ignore[import-not-found] + except ImportError: + open_zstd = None from . import __version__ @@ -61,7 +67,12 @@ def _parse(self, json_path: pathlib.Path) -> None: try: if json_path.suffix == ".zst": - with zstd.open(json_path, "rt") as f: + if open_zstd is None: + raise RuntimeError( + "Zstd support is not available." + "Please install the 'zstandard' package." + ) + with open_zstd(json_path, "rb") as f: data = json.load(f) else: with json_path.open(encoding="utf-8") as f: