|
| 1 | +# Generated manually on 2025-12-15 14:00 for creating missing metadata artifacts |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | + |
| 6 | +def extract_wheel_metadata(filename): |
| 7 | + """ |
| 8 | + Extract the metadata file content from a wheel file. |
| 9 | + Returns the raw metadata content as bytes or None if metadata cannot be extracted. |
| 10 | + """ |
| 11 | + import zipfile |
| 12 | + |
| 13 | + if not filename.endswith(".whl"): |
| 14 | + return None |
| 15 | + try: |
| 16 | + with zipfile.ZipFile(filename, "r") as f: |
| 17 | + for file_path in f.namelist(): |
| 18 | + if file_path.endswith(".dist-info/METADATA"): |
| 19 | + return f.read(file_path) |
| 20 | + except (zipfile.BadZipFile, KeyError, OSError): |
| 21 | + pass |
| 22 | + return None |
| 23 | + |
| 24 | + |
| 25 | +def artifact_to_metadata_artifact(filename, artifact, tmp_dir, artifact_model, get_domain): |
| 26 | + """ |
| 27 | + Creates artifact for metadata from the provided wheel artifact. |
| 28 | + """ |
| 29 | + import os |
| 30 | + import shutil |
| 31 | + import tempfile |
| 32 | + from django.db import IntegrityError |
| 33 | + |
| 34 | + if not filename.endswith(".whl"): |
| 35 | + return None |
| 36 | + |
| 37 | + temp_wheel_path = None |
| 38 | + temp_metadata_path = None |
| 39 | + try: |
| 40 | + with tempfile.NamedTemporaryFile( |
| 41 | + "wb", dir=tmp_dir, suffix=filename, delete=False |
| 42 | + ) as temp_file: |
| 43 | + temp_wheel_path = temp_file.name |
| 44 | + artifact.file.seek(0) |
| 45 | + shutil.copyfileobj(artifact.file, temp_file) |
| 46 | + temp_file.flush() |
| 47 | + |
| 48 | + metadata_content = extract_wheel_metadata(temp_wheel_path) |
| 49 | + if not metadata_content: |
| 50 | + return None |
| 51 | + |
| 52 | + with tempfile.NamedTemporaryFile( |
| 53 | + "wb", dir=tmp_dir, suffix=".metadata", delete=False |
| 54 | + ) as temp_md: |
| 55 | + temp_metadata_path = temp_md.name |
| 56 | + temp_md.write(metadata_content) |
| 57 | + temp_md.flush() |
| 58 | + |
| 59 | + metadata_artifact = artifact_model.init_and_validate(temp_metadata_path) |
| 60 | + try: |
| 61 | + metadata_artifact.save() |
| 62 | + except IntegrityError: |
| 63 | + metadata_artifact = artifact_model.objects.get( |
| 64 | + sha256=metadata_artifact.sha256, pulp_domain=get_domain() |
| 65 | + ) |
| 66 | + return metadata_artifact |
| 67 | + |
| 68 | + finally: |
| 69 | + if temp_wheel_path and os.path.exists(temp_wheel_path): |
| 70 | + os.unlink(temp_wheel_path) |
| 71 | + if temp_metadata_path and os.path.exists(temp_metadata_path): |
| 72 | + os.unlink(temp_metadata_path) |
| 73 | + |
| 74 | + |
| 75 | +def create_missing_metadata_artifacts(apps, schema_editor): |
| 76 | + """ |
| 77 | + Create metadata artifacts for PythonPackageContent instances that have metadata_sha256 |
| 78 | + but are missing the corresponding metadata artifact. |
| 79 | + """ |
| 80 | + import tempfile |
| 81 | + from pulpcore.plugin.util import get_domain |
| 82 | + |
| 83 | + PythonPackageContent = apps.get_model("python", "PythonPackageContent") |
| 84 | + ContentArtifact = apps.get_model("core", "ContentArtifact") |
| 85 | + Artifact = apps.get_model("core", "Artifact") |
| 86 | + |
| 87 | + packages = ( |
| 88 | + PythonPackageContent.objects.filter(metadata_sha256__isnull=False) |
| 89 | + .exclude(metadata_sha256="") |
| 90 | + .prefetch_related("contentartifact_set") |
| 91 | + ) |
| 92 | + created_count = 0 |
| 93 | + skipped_count = 0 |
| 94 | + |
| 95 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 96 | + for package in packages: |
| 97 | + metadata_relative_path = f"{package.filename}.metadata" |
| 98 | + content_artifacts = list(package.contentartifact_set.all()) |
| 99 | + |
| 100 | + if any(ca.relative_path == metadata_relative_path for ca in content_artifacts): |
| 101 | + # Metadata artifact already exist |
| 102 | + continue |
| 103 | + |
| 104 | + main_content_artifact = next( |
| 105 | + (ca for ca in content_artifacts if ca.relative_path == package.filename), |
| 106 | + None, |
| 107 | + ) |
| 108 | + if not main_content_artifact: |
| 109 | + # Main artifact does not exist |
| 110 | + skipped_count += 1 |
| 111 | + continue |
| 112 | + |
| 113 | + metadata_artifact = artifact_to_metadata_artifact( |
| 114 | + package.filename, main_content_artifact.artifact, temp_dir, Artifact, get_domain |
| 115 | + ) |
| 116 | + if not metadata_artifact: |
| 117 | + # Failed to create metadata artifact |
| 118 | + skipped_count += 1 |
| 119 | + continue |
| 120 | + |
| 121 | + ContentArtifact.objects.create( |
| 122 | + artifact=metadata_artifact, content=package, relative_path=metadata_relative_path |
| 123 | + ) |
| 124 | + created_count += 1 |
| 125 | + |
| 126 | + print(f"Created {created_count} missing metadata artifacts. Skipped {skipped_count} packages.") |
| 127 | + |
| 128 | + |
| 129 | +class Migration(migrations.Migration): |
| 130 | + |
| 131 | + dependencies = [ |
| 132 | + ("python", "0018_packageprovenance"), |
| 133 | + ] |
| 134 | + |
| 135 | + operations = [ |
| 136 | + migrations.RunPython( |
| 137 | + create_missing_metadata_artifacts, |
| 138 | + reverse_code=migrations.RunPython.noop, |
| 139 | + ), |
| 140 | + ] |
0 commit comments