Skip to content

Commit 5e4f2cb

Browse files
committed
tmp 2
1 parent 472edeb commit 5e4f2cb

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

pulp_python/app/utils.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import hashlib
22
import logging
3+
import os
34
import pkginfo
45
import re
56
import shutil
@@ -270,25 +271,42 @@ def artifact_to_metadata_artifact(
270271
if not filename.endswith(".whl"):
271272
return None
272273

273-
with tempfile.NamedTemporaryFile("wb", dir=tmp_dir, suffix=filename) as temp_file:
274-
artifact.file.seek(0)
275-
shutil.copyfileobj(artifact.file, temp_file)
276-
temp_file.flush()
274+
temp_wheel_path = None
275+
temp_metadata_path = None
276+
try:
277+
with tempfile.NamedTemporaryFile(
278+
"wb", dir=tmp_dir, suffix=filename, delete=False
279+
) as temp_file:
280+
temp_wheel_path = temp_file.name
281+
artifact.file.seek(0)
282+
shutil.copyfileobj(artifact.file, temp_file)
283+
temp_file.flush()
284+
277285
metadata_content = extract_wheel_metadata(temp_file.name)
278286
if not metadata_content:
279287
return None
280288

281-
with tempfile.NamedTemporaryFile("wb", dir=tmp_dir, suffix=".metadata") as temp_md:
289+
with tempfile.NamedTemporaryFile(
290+
"wb", dir=tmp_dir, suffix=".metadata", delete=False
291+
) as temp_md:
292+
temp_metadata_path = temp_md.name
282293
temp_md.write(metadata_content)
283294
temp_md.flush()
284-
metadata_artifact = Artifact.init_and_validate(temp_md.name)
285-
try:
286-
metadata_artifact.save()
287-
except IntegrityError:
288-
metadata_artifact = Artifact.objects.get(
289-
sha256=metadata_artifact.sha256, pulp_domain=get_domain()
290-
)
291-
return metadata_artifact
295+
296+
metadata_artifact = Artifact.init_and_validate(temp_metadata_path)
297+
try:
298+
metadata_artifact.save()
299+
except IntegrityError:
300+
metadata_artifact = Artifact.objects.get(
301+
sha256=metadata_artifact.sha256, pulp_domain=get_domain()
302+
)
303+
return metadata_artifact
304+
305+
finally:
306+
if temp_wheel_path and os.path.exists(temp_wheel_path):
307+
os.unlink(temp_wheel_path)
308+
if temp_metadata_path and os.path.exists(temp_metadata_path):
309+
os.unlink(temp_metadata_path)
292310

293311

294312
def fetch_json_release_metadata(name: str, version: str, remotes: set[Remote]) -> dict:

0 commit comments

Comments
 (0)