Skip to content

Commit 574d344

Browse files
committed
♻️ Clean up archive after uploading
1 parent 0296882 commit 574d344

File tree

2 files changed

+68
-56
lines changed

2 files changed

+68
-56
lines changed

src/fastapi_cloud_cli/commands/deploy.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import tarfile
66
import tempfile
77
import time
8-
import uuid
98
from enum import Enum
109
from itertools import cycle
1110
from pathlib import Path
@@ -46,19 +45,14 @@ def _should_exclude_entry(path: Path) -> bool:
4645
return False
4746

4847

49-
def archive(path: Path) -> Path:
48+
def archive(path: Path, tar_path: Path) -> Path:
5049
logger.debug("Starting archive creation for path: %s", path)
5150
files = rignore.walk(
5251
path,
5352
should_exclude_entry=_should_exclude_entry,
5453
additional_ignore_paths=[".fastapicloudignore"],
5554
)
5655

57-
temp_dir = tempfile.mkdtemp()
58-
logger.debug("Created temp directory: %s", temp_dir)
59-
60-
name = f"fastapi-cloud-deploy-{uuid.uuid4()}"
61-
tar_path = Path(temp_dir) / f"{name}.tar"
6256
logger.debug("Archive will be created at: %s", tar_path)
6357

6458
file_count = 0
@@ -586,11 +580,14 @@ def deploy(
586580
)
587581
raise typer.Exit(1)
588582

589-
logger.debug("Creating archive for deployment")
590-
archive_path = archive(path or Path.cwd()) # noqa: F841
583+
with tempfile.TemporaryDirectory() as temp_dir:
584+
logger.debug("Creating archive for deployment")
585+
archive_path = Path(temp_dir) / "archive.tar"
586+
archive(path or Path.cwd(), archive_path)
591587

592-
with toolkit.progress(title="Creating deployment") as progress:
593-
with handle_http_errors(progress):
588+
with toolkit.progress(
589+
title="Creating deployment"
590+
) as progress, handle_http_errors(progress):
594591
logger.debug("Creating deployment for app: %s", app.id)
595592
deployment = _create_deployment(app.id)
596593

@@ -602,7 +599,7 @@ def deploy(
602599

603600
_upload_deployment(deployment.id, archive_path)
604601

605-
progress.log("Deployment uploaded successfully!")
602+
progress.log("Deployment uploaded successfully!")
606603

607604
toolkit.print_line()
608605

tests/test_archive.py

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,89 @@
11
import tarfile
22
from pathlib import Path
33

4+
import pytest
5+
46
from fastapi_cloud_cli.commands.deploy import archive
57

68

7-
def test_archive_creates_tar_file(tmp_path: Path) -> None:
8-
(tmp_path / "main.py").write_text("print('hello')")
9-
(tmp_path / "config.json").write_text('{"key": "value"}')
10-
(tmp_path / "subdir").mkdir()
11-
(tmp_path / "subdir" / "utils.py").write_text("def helper(): pass")
9+
@pytest.fixture
10+
def src_path(tmp_path: Path) -> Path:
11+
path = tmp_path / "source"
12+
path.mkdir()
13+
return path
14+
15+
16+
@pytest.fixture
17+
def tar_path(tmp_path: Path) -> Path:
18+
return tmp_path / "archive.tar"
19+
1220

13-
tar_path = archive(tmp_path)
21+
def test_archive_creates_tar_file(src_path: Path, tar_path: Path) -> None:
22+
(src_path / "main.py").write_text("print('hello')")
23+
(src_path / "config.json").write_text('{"key": "value"}')
24+
(src_path / "subdir").mkdir()
25+
(src_path / "subdir" / "utils.py").write_text("def helper(): pass")
1426

27+
archive(src_path, tar_path)
1528
assert tar_path.exists()
16-
assert tar_path.suffix == ".tar"
17-
assert tar_path.name.startswith("fastapi-cloud-deploy-")
1829

1930

20-
def test_archive_excludes_venv_and_similar_folders(tmp_path: Path) -> None:
31+
def test_archive_excludes_venv_and_similar_folders(
32+
src_path: Path, tar_path: Path
33+
) -> None:
2134
"""Should exclude .venv directory from archive."""
2235
# the only files we want to include
23-
(tmp_path / "main.py").write_text("print('hello')")
24-
(tmp_path / "static").mkdir()
25-
(tmp_path / "static" / "index.html").write_text("<html></html>")
36+
(src_path / "main.py").write_text("print('hello')")
37+
(src_path / "static").mkdir()
38+
(src_path / "static" / "index.html").write_text("<html></html>")
2639
# virtualenv
27-
(tmp_path / ".venv").mkdir()
28-
(tmp_path / ".venv" / "lib").mkdir()
29-
(tmp_path / ".venv" / "lib" / "package.py").write_text("# package")
40+
(src_path / ".venv").mkdir()
41+
(src_path / ".venv" / "lib").mkdir()
42+
(src_path / ".venv" / "lib" / "package.py").write_text("# package")
3043
# pycache
31-
(tmp_path / "__pycache__").mkdir()
32-
(tmp_path / "__pycache__" / "main.cpython-311.pyc").write_text("bytecode")
44+
(src_path / "__pycache__").mkdir()
45+
(src_path / "__pycache__" / "main.cpython-311.pyc").write_text("bytecode")
3346
# pyc files
34-
(tmp_path / "main.pyc").write_text("bytecode")
47+
(src_path / "main.pyc").write_text("bytecode")
3548
# mypy/pytest
36-
(tmp_path / ".mypy_cache").mkdir()
37-
(tmp_path / ".mypy_cache" / "file.json").write_text("{}")
38-
(tmp_path / ".pytest_cache").mkdir()
39-
(tmp_path / ".pytest_cache" / "cache.db").write_text("data")
49+
(src_path / ".mypy_cache").mkdir()
50+
(src_path / ".mypy_cache" / "file.json").write_text("{}")
51+
(src_path / ".pytest_cache").mkdir()
52+
(src_path / ".pytest_cache" / "cache.db").write_text("data")
4053

41-
tar_path = archive(tmp_path)
54+
archive(src_path, tar_path)
4255

4356
with tarfile.open(tar_path, "r") as tar:
4457
names = tar.getnames()
4558
assert set(names) == {"main.py", "static/index.html"}
4659

4760

48-
def test_archive_preserves_relative_paths(tmp_path: Path) -> None:
49-
(tmp_path / "src").mkdir()
50-
(tmp_path / "src" / "app").mkdir()
51-
(tmp_path / "src" / "app" / "main.py").write_text("print('hello')")
61+
def test_archive_preserves_relative_paths(src_path: Path, tar_path: Path) -> None:
62+
(src_path / "src").mkdir()
63+
(src_path / "src" / "app").mkdir()
64+
(src_path / "src" / "app" / "main.py").write_text("print('hello')")
5265

53-
tar_path = archive(tmp_path)
66+
archive(src_path, tar_path)
5467

5568
with tarfile.open(tar_path, "r") as tar:
5669
names = tar.getnames()
5770
assert names == ["src/app/main.py"]
5871

5972

60-
def test_archive_respects_fastapicloudignore(tmp_path: Path) -> None:
73+
def test_archive_respects_fastapicloudignore(src_path: Path, tar_path: Path) -> None:
6174
"""Should exclude files specified in .fastapicloudignore."""
6275
# Create test files
63-
(tmp_path / "main.py").write_text("print('hello')")
64-
(tmp_path / "config.py").write_text("CONFIG = 'value'")
65-
(tmp_path / "secrets.env").write_text("SECRET_KEY=xyz")
66-
(tmp_path / "data").mkdir()
67-
(tmp_path / "data" / "file.txt").write_text("data")
76+
(src_path / "main.py").write_text("print('hello')")
77+
(src_path / "config.py").write_text("CONFIG = 'value'")
78+
(src_path / "secrets.env").write_text("SECRET_KEY=xyz")
79+
(src_path / "data").mkdir()
80+
(src_path / "data" / "file.txt").write_text("data")
6881

6982
# Create .fastapicloudignore file
70-
(tmp_path / ".fastapicloudignore").write_text("secrets.env\ndata/\n")
83+
(src_path / ".fastapicloudignore").write_text("secrets.env\ndata/\n")
7184

7285
# Create archive
73-
tar_path = archive(tmp_path)
86+
archive(src_path, tar_path)
7487

7588
# Verify ignored files are excluded
7689
with tarfile.open(tar_path, "r") as tar:
@@ -81,21 +94,23 @@ def test_archive_respects_fastapicloudignore(tmp_path: Path) -> None:
8194
}
8295

8396

84-
def test_archive_respects_fastapicloudignore_unignore(tmp_path: Path) -> None:
97+
def test_archive_respects_fastapicloudignore_unignore(
98+
src_path: Path, tar_path: Path
99+
) -> None:
85100
"""Test we can use .fastapicloudignore to unignore files inside .gitignore"""
86101
# Create test files
87-
(tmp_path / "main.py").write_text("print('hello')")
88-
(tmp_path / "static/build").mkdir(exist_ok=True, parents=True)
89-
(tmp_path / "static/build/style.css").write_text("body { background: #bada55 }")
102+
(src_path / "main.py").write_text("print('hello')")
103+
(src_path / "static/build").mkdir(exist_ok=True, parents=True)
104+
(src_path / "static/build/style.css").write_text("body { background: #bada55 }")
90105
# Rignore needs a .git folder to make .gitignore work
91-
(tmp_path / ".git").mkdir(exist_ok=True, parents=True)
92-
(tmp_path / ".gitignore").write_text("build/")
106+
(src_path / ".git").mkdir(exist_ok=True, parents=True)
107+
(src_path / ".gitignore").write_text("build/")
93108

94109
# Create .fastapicloudignore file
95-
(tmp_path / ".fastapicloudignore").write_text("!static/build")
110+
(src_path / ".fastapicloudignore").write_text("!static/build")
96111

97112
# Create archive
98-
tar_path = archive(tmp_path)
113+
archive(src_path, tar_path)
99114

100115
# Verify ignored files are excluded
101116
with tarfile.open(tar_path, "r") as tar:

0 commit comments

Comments
 (0)