Skip to content

Commit f50de9c

Browse files
committed
Fix running on S3 / Azure
1 parent e5dac3b commit f50de9c

6 files changed

Lines changed: 30 additions & 14 deletions

File tree

pulp_rust/app/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ def init_from_artifact_and_relative_path(artifact, relative_path):
138138
Cargo.toml inside the .crate tarball.
139139
"""
140140
crate_name, version = _parse_crate_relative_path(relative_path)
141-
cargo_toml = extract_cargo_toml(artifact.file.path, crate_name, version)
141+
with artifact.file.open("rb") as f:
142+
cargo_toml = extract_cargo_toml(f, crate_name, version)
142143

143144
content = RustContent(
144145
name=crate_name,

pulp_rust/app/tasks/publishing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ async def apublish_package(repository_pk, metadata, crate_path):
7979
# Cargo.toml within the tarball, then extract everything from the Cargo.toml itself.
8080
# See: https://github.com/rust-lang/cargo/issues/14492
8181
# https://github.com/rust-lang/crates.io/pull/7238
82-
cargo_toml = extract_cargo_toml(artifact.file.path, metadata["name"], metadata["vers"])
82+
with artifact.file.open("rb") as f:
83+
cargo_toml = extract_cargo_toml(f, metadata["name"], metadata["vers"])
8384
package = cargo_toml.get("package", {})
8485

8586
name = package["name"]

pulp_rust/app/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
import tomli as tomllib
88

99

10-
def extract_cargo_toml(crate_path, crate_name, version):
11-
"""Extract and parse Cargo.toml from a .crate tarball."""
10+
def extract_cargo_toml(fileobj, crate_name, version):
11+
"""Extract and parse Cargo.toml from a .crate tarball.
12+
13+
Args:
14+
fileobj: A file-like object containing the .crate tarball data.
15+
crate_name: The crate name (used to locate Cargo.toml inside the tarball).
16+
version: The crate version (used to locate Cargo.toml inside the tarball).
17+
"""
1218
expected_path = f"{crate_name}-{version}/Cargo.toml"
13-
with tarfile.open(crate_path, "r:gz") as tar:
19+
with tarfile.open(fileobj=fileobj, mode="r:gz") as tar:
1420
cargo_toml_file = tar.extractfile(expected_path)
1521
if cargo_toml_file is None:
16-
raise FileNotFoundError(f"No Cargo.toml found in {crate_path} at {expected_path}")
22+
raise FileNotFoundError(f"No Cargo.toml found at {expected_path}")
1723
return tomllib.load(cargo_toml_file)
1824

1925

pulp_rust/tests/functional/api/test_upload.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def test_upload_and_index_fidelity(
2929
crate_path, cksum = download_crate_from_upstream(crate_name, crate_version)
3030

3131
# 2. Parse metadata from the .crate file
32-
cargo_toml = extract_cargo_toml(crate_path, crate_name, crate_version)
32+
with open(crate_path, "rb") as f:
33+
cargo_toml = extract_cargo_toml(f, crate_name, crate_version)
3334
deps = extract_dependencies(cargo_toml)
3435
features = cargo_toml.get("features", {})
3536
links = cargo_toml.get("package", {}).get("links")

pulp_rust/tests/functional/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ def build_publish_metadata(crate_path, crate_name, crate_version):
129129
Cargo uses "version_req" (not "req") and "explicit_name_in_toml" (not "package")
130130
per the Cargo registry web API spec.
131131
"""
132-
cargo_toml = extract_cargo_toml(crate_path, crate_name, crate_version)
132+
with open(crate_path, "rb") as f:
133+
cargo_toml = extract_cargo_toml(f, crate_name, crate_version)
133134
deps = extract_dependencies(cargo_toml)
134135

135136
return {

pulp_rust/tests/unit/test_utils.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ class TestExtractCargoToml:
207207
def test_basic_extraction(self):
208208
toml_content = b'[package]\nname = "foo"\nversion = "1.0.0"\n'
209209
path = _make_crate_tarball("foo", "1.0.0", toml_content)
210-
result = extract_cargo_toml(path, "foo", "1.0.0")
210+
with open(path, "rb") as f:
211+
result = extract_cargo_toml(f, "foo", "1.0.0")
211212
assert result["package"]["name"] == "foo"
212213
assert result["package"]["version"] == "1.0.0"
213214

@@ -216,7 +217,8 @@ def test_with_dependencies(self):
216217
b'[package]\nname = "bar"\nversion = "0.1.0"\n' b'\n[dependencies]\nserde = "1.0"\n'
217218
)
218219
path = _make_crate_tarball("bar", "0.1.0", toml_content)
219-
result = extract_cargo_toml(path, "bar", "0.1.0")
220+
with open(path, "rb") as f:
221+
result = extract_cargo_toml(f, "bar", "0.1.0")
220222
assert "serde" in result["dependencies"]
221223

222224
def test_with_features(self):
@@ -225,7 +227,8 @@ def test_with_features(self):
225227
b'\n[features]\ndefault = ["std"]\nstd = []\n'
226228
)
227229
path = _make_crate_tarball("baz", "2.0.0", toml_content)
228-
result = extract_cargo_toml(path, "baz", "2.0.0")
230+
with open(path, "rb") as f:
231+
result = extract_cargo_toml(f, "baz", "2.0.0")
229232
assert result["features"] == {"default": ["std"], "std": []}
230233

231234
def test_missing_cargo_toml_raises(self):
@@ -241,18 +244,21 @@ def test_missing_cargo_toml_raises(self):
241244
tmp.flush()
242245

243246
with pytest.raises(KeyError):
244-
extract_cargo_toml(tmp.name, "foo", "1.0.0")
247+
with open(tmp.name, "rb") as f:
248+
extract_cargo_toml(f, "foo", "1.0.0")
245249

246250
def test_with_rust_version(self):
247251
toml_content = b'[package]\nname = "qux"\nversion = "1.0.0"\nrust-version = "1.56.0"\n'
248252
path = _make_crate_tarball("qux", "1.0.0", toml_content)
249-
result = extract_cargo_toml(path, "qux", "1.0.0")
253+
with open(path, "rb") as f:
254+
result = extract_cargo_toml(f, "qux", "1.0.0")
250255
assert result["package"]["rust-version"] == "1.56.0"
251256

252257
def test_with_links(self):
253258
toml_content = b'[package]\nname = "zlib-sys"\nversion = "0.1.0"\nlinks = "z"\n'
254259
path = _make_crate_tarball("zlib-sys", "0.1.0", toml_content)
255-
result = extract_cargo_toml(path, "zlib-sys", "0.1.0")
260+
with open(path, "rb") as f:
261+
result = extract_cargo_toml(f, "zlib-sys", "0.1.0")
256262
assert result["package"]["links"] == "z"
257263

258264

0 commit comments

Comments
 (0)