Skip to content

Commit 115e24a

Browse files
committed
Adds tool.hatch.build.targets.sdist.compress-level
1 parent 4ebce0e commit 115e24a

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

backend/src/hatchling/builders/sdist.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
class SdistArchive:
31-
def __init__(self, name: str, *, reproducible: bool) -> None:
31+
def __init__(self, name: str, *, reproducible: bool, compress_level: int) -> None:
3232
"""
3333
https://peps.python.org/pep-0517/#source-distributions
3434
"""
@@ -38,7 +38,8 @@ def __init__(self, name: str, *, reproducible: bool) -> None:
3838

3939
raw_fd, self.path = tempfile.mkstemp(suffix='.tar.gz')
4040
self.fd = os.fdopen(raw_fd, 'w+b')
41-
self.gz = gzip.GzipFile(fileobj=self.fd, mode='wb', mtime=self.timestamp)
41+
42+
self.gz = gzip.GzipFile(fileobj=self.fd, mode='wb', mtime=self.timestamp, compresslevel=compress_level)
4243
self.tf = tarfile.TarFile(fileobj=self.gz, mode='w', format=tarfile.PAX_FORMAT)
4344
self.gettarinfo = lambda *args, **kwargs: self.normalize_tar_metadata(self.tf.gettarinfo(*args, **kwargs))
4445

@@ -90,10 +91,32 @@ class SdistBuilderConfig(BuilderConfig):
9091
def __init__(self, *args: Any, **kwargs: Any) -> None:
9192
super().__init__(*args, **kwargs)
9293

94+
self.__compress_level: int | None = None
9395
self.__core_metadata_constructor: Callable[..., str] | None = None
9496
self.__strict_naming: bool | None = None
9597
self.__support_legacy: bool | None = None
9698

99+
@property
100+
def compress_level(self) -> int:
101+
if self.__compress_level is None:
102+
try:
103+
compress_level = int(self.target_config.get('compress-level', 9))
104+
except ValueError as e:
105+
message = f'Field `tool.hatch.build.{self.plugin_name}.compress-level` must be an integer'
106+
raise TypeError(message) from e
107+
108+
if not (0 <= compress_level <= 9): # noqa: PLR2004
109+
message = (
110+
'Value field '
111+
f'`tool.hatch.build.targets.{self.plugin_name}.compress-level` '
112+
'must be an integer from 0 to 9'
113+
)
114+
raise ValueError(message)
115+
116+
self.__compress_level = compress_level
117+
118+
return self.__compress_level
119+
97120
@property
98121
def core_metadata_constructor(self) -> Callable[..., str]:
99122
if self.__core_metadata_constructor is None:
@@ -166,7 +189,9 @@ def clean( # noqa: PLR6301
166189
def build_standard(self, directory: str, **build_data: Any) -> str:
167190
found_packages = set()
168191

169-
with SdistArchive(self.artifact_project_id, reproducible=self.config.reproducible) as archive:
192+
with SdistArchive(
193+
self.artifact_project_id, reproducible=self.config.reproducible, compress_level=self.config.compress_level
194+
) as archive:
170195
for included_file in self.recurse_included_files():
171196
if self.config.support_legacy:
172197
possible_package, file_name = os.path.split(included_file.relative_path)

docs/config/build.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@ skip-excluded-dirs = true
180180
!!! warning
181181
This may result in not shipping desired files. For example, if you want to include the file `a/b/c.txt` but your [VCS ignores](#vcs) `a/b`, the file `c.txt` will not be seen because its parent directory will not be entered. In such cases you can use the [`force-include`](#forced-inclusion) option.
182182

183+
## Compression level
184+
185+
You can change the level used for compressing the sdist tarballs. In some circumstances, lowering it from the default of 9 can massively reduce build times without affecting the output size too much.
186+
187+
```toml config-example
188+
[tool.hatch.build.targets.sdist]
189+
compress-level = 1
190+
```
191+
192+
Accepted values are 0 (no compression) to 9 (highest compression).
193+
183194
## Reproducible builds
184195

185196
By default, [build targets](#build-targets) will build in a reproducible manner provided that they support that behavior. To disable this, set `reproducible` to `false`:

0 commit comments

Comments
 (0)