Skip to content

Commit 196736b

Browse files
author
Christophe Guerreiro
committed
cli: backports.zstd is replaced by zstandard 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. So backports.zstd is replaced by zstandard. Make the zstandard dependency optional by importing it only when required. 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 <christophe.guerreiro@non.se.com>
1 parent ae4e3a0 commit 196736b

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.
88

99
### Changed
1010

11+
- zstandard replaced backports.zstd for python <3.14 and considered as an optional dependency
12+
1113
### Fixed
1214

1315
### Removed
@@ -40,7 +42,7 @@ boolean options, all defaulting to `True`:
4042
- The JSON output file is no longer generated by default.
4143
- The `--output <PATH>` option have been renamed to `--json-output <PATH>`
4244
- The `--json-output <PATH>` option must now be explicitly specified to enable JSON file generation.
43-
45+
4446

4547
### Fixed
4648

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ classifiers = [
2020
"Topic :: Software Development :: Build Tools",
2121
]
2222

23-
dependencies = [
24-
"backports.zstd; python_version < '3.14'"
23+
[project.optional-dependencies]
24+
zstandard = [
25+
"zstandard;python_version<'3.14'",
2526
]
2627

2728
[project.scripts]

src/spdx_diff/cli.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
from typing import Any
1818

1919
if sys.version_info >= (3, 14):
20-
from compression import zstd
20+
from compression.zstd import open as open_zstd
2121
else:
22-
from backports import zstd
22+
try:
23+
from zstandard import open as open_zstd
24+
except ImportError:
25+
open_zstd = None
2326

2427
from . import __version__
2528

@@ -61,7 +64,12 @@ def _parse(self, json_path: pathlib.Path) -> None:
6164

6265
try:
6366
if json_path.suffix == ".zst":
64-
with zstd.open(json_path, "rt") as f:
67+
if open_zstd is None:
68+
raise RuntimeError(
69+
"Zstd support is not available."
70+
"Please install the 'zstandard' package."
71+
)
72+
with open_zstd(json_path, "rb") as f:
6573
data = json.load(f)
6674
else:
6775
with json_path.open(encoding="utf-8") as f:

0 commit comments

Comments
 (0)