|
1 | 1 | import hashlib |
2 | 2 | import logging |
| 3 | +import os |
3 | 4 | import pkginfo |
4 | 5 | import re |
5 | 6 | import shutil |
@@ -270,25 +271,42 @@ def artifact_to_metadata_artifact( |
270 | 271 | if not filename.endswith(".whl"): |
271 | 272 | return None |
272 | 273 |
|
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 | + |
277 | 285 | metadata_content = extract_wheel_metadata(temp_file.name) |
278 | 286 | if not metadata_content: |
279 | 287 | return None |
280 | 288 |
|
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 |
282 | 293 | temp_md.write(metadata_content) |
283 | 294 | 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) |
292 | 310 |
|
293 | 311 |
|
294 | 312 | def fetch_json_release_metadata(name: str, version: str, remotes: set[Remote]) -> dict: |
|
0 commit comments