Skip to content

Commit 5dc50d1

Browse files
committed
Added get_export_filename()
1 parent 135ad59 commit 5dc50d1

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [1.1.0] - 2024-01-30
99

10+
### Added
11+
12+
- `get_export_filename(image, platform)` for an fs-safe filename
13+
1014
### Changed
1115

1216
- `fs_name` now includes image reference and is filesystem safe for all OS.

src/docker_export/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,11 @@ def headers(self) -> dict[str, str]:
373373
return {"Authorization": f"Bearer {self.token}"}
374374

375375

376+
def get_export_filename(image: Image, platform: Platform) -> str:
377+
"""Filesystem-safe filename to export an image for a platform"""
378+
return sanitize_filename(f"{image.fs_name}_{platform}.tar")
379+
380+
376381
def get_manifests(image: Image, auth: RegistryAuth):
377382
"""get list of fat-manifests for the image"""
378383
resp = requests.get(
@@ -906,7 +911,7 @@ def main():
906911
image = Image.parse(**args)
907912
dest = pathlib.Path(output).expanduser().resolve()
908913
if not dest.suffix == ".tar":
909-
dest = dest.joinpath(sanitize_filename(f"{image.fs_name}_{platform}.tar"))
914+
dest = dest.joinpath(get_export_filename(image=image, platform=platform))
910915
build_dir = (
911916
pathlib.Path(build_dir).expanduser().resolve() if build_dir else None
912917
)

tests/test_export.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import py # pyright: ignore [reportMissingTypeStubs]
88
import pytest
99

10-
from docker_export import Image, Platform, export
10+
from docker_export import Image, Platform, export, get_export_filename
1111

1212

1313
@pytest.fixture(scope="session")
@@ -22,7 +22,7 @@ def platform_name() -> str:
2222

2323
@pytest.fixture(scope="session")
2424
def expected_filepath() -> pathlib.Path:
25-
return pathlib.Path("ghcr.io_offspot_base-httpd.tar")
25+
return pathlib.Path("ghcr.io_offspot_base-httpd_1.0_linuxarm64.tar")
2626

2727

2828
@pytest.fixture(scope="session")
@@ -42,7 +42,9 @@ def test_export(
4242
export(
4343
image=Image.parse(image_name),
4444
platform=Platform.parse(platform_name),
45-
to=expected_filepath,
45+
to=pathlib.Path(
46+
get_export_filename(Image.parse(image_name), Platform.parse(platform_name))
47+
),
4648
)
4749
assert expected_filepath.exists()
4850
assert expected_filepath.stat().st_size == expected_filesize
@@ -65,7 +67,8 @@ def test_cli(
6567
platform_name,
6668
image_name,
6769
".",
68-
]
70+
],
71+
check=False,
6972
)
7073
assert ps.returncode == 0
7174
assert expected_filepath.exists()

0 commit comments

Comments
 (0)