Skip to content

Commit 96c32f0

Browse files
parthaadralley
authored andcommitted
Use published relative paths for FS Exporter
FS Exporter correctly uses relative paths now from publication instead of directly fetching from content artifact. fixes #2933 (cherry picked from commit 4cc15b3)
1 parent cab70f9 commit 96c32f0

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

CHANGES/2933.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use published relative paths for FS Exporter.

pulpcore/app/tasks/export.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,38 @@
3737
log = logging.getLogger(__name__)
3838

3939

40-
def _export_to_file_system(path, content_artifacts, method=FS_EXPORT_METHODS.WRITE):
40+
def _validate_fs_export(content_artifacts):
4141
"""
42-
Export a set of ContentArtifacts to the filesystem.
43-
4442
Args:
45-
path (str): A path to export the ContentArtifacts to
4643
content_artifacts (django.db.models.QuerySet): Set of ContentArtifacts to export
4744
4845
Raises:
49-
ValidationError: When path is not in the ALLOWED_EXPORT_PATHS setting
5046
RuntimeError: If Artifacts are not downloaded or when trying to link non-fs files
5147
"""
5248
if content_artifacts.filter(artifact=None).exists():
5349
RuntimeError(_("Cannot export artifacts that haven't been downloaded."))
5450

51+
52+
def _export_to_file_system(path, relative_paths_to_artifacts, method=FS_EXPORT_METHODS.WRITE):
53+
"""
54+
Export a set of artifacts to the filesystem.
55+
56+
Args:
57+
path (str): A path to export the ContentArtifacts to
58+
relative_paths_to_artifacts: A dict with {relative_path: artifact} mapping
59+
60+
Raises:
61+
ValidationError: When path is not in the ALLOWED_EXPORT_PATHS setting
62+
"""
63+
5564
if (
5665
settings.DEFAULT_FILE_STORAGE != "pulpcore.app.models.storage.FileSystem"
5766
and method != FS_EXPORT_METHODS.WRITE
5867
):
5968
raise RuntimeError(_("Only write is supported for non-filesystem storage."))
6069

61-
for ca in content_artifacts.select_related("artifact").iterator():
62-
artifact = ca.artifact
63-
dest = os.path.join(path, ca.relative_path)
64-
70+
for relative_path, artifact in relative_paths_to_artifacts.items():
71+
dest = os.path.join(path, relative_path)
6572
os.makedirs(os.path.split(dest)[0], exist_ok=True)
6673

6774
if method == FS_EXPORT_METHODS.SYMLINK:
@@ -74,7 +81,6 @@ def _export_to_file_system(path, content_artifacts, method=FS_EXPORT_METHODS.WRI
7481
with open(dest, "wb") as f, artifact.file as af:
7582
for chunk in af.chunks(1024 * 1024):
7683
f.write(chunk)
77-
7884
else:
7985
raise RuntimeError(_("Unsupported export method '{}'.").format(method))
8086

@@ -112,7 +118,21 @@ def fs_publication_export(exporter_pk, publication_pk):
112118
content__in=publication.repository_version.content
113119
)
114120

115-
_export_to_file_system(exporter.path, content_artifacts)
121+
_validate_fs_export(content_artifacts)
122+
123+
relative_path_to_artifacts = {}
124+
if publication.pass_through:
125+
relative_path_to_artifacts = {
126+
ca.relative_path: ca.artifact
127+
for ca in content_artifacts.select_related("artifact").iterator()
128+
}
129+
130+
for pa in publication.published_artifact.select_related(
131+
"content_artifact", "content_artifact__artifact"
132+
).iterator():
133+
relative_path_to_artifacts[pa.relative_path] = pa.content_artifact.artifact
134+
135+
_export_to_file_system(exporter.path, relative_path_to_artifacts, exporter.method)
116136

117137

118138
def fs_repo_version_export(exporter_pk, repo_version_pk):
@@ -140,8 +160,14 @@ def fs_repo_version_export(exporter_pk, repo_version_pk):
140160
)
141161

142162
content_artifacts = ContentArtifact.objects.filter(content__in=repo_version.content)
163+
_validate_fs_export(content_artifacts)
164+
165+
relative_path_to_artifacts = {
166+
ca.relative_path: ca.artifact
167+
for ca in content_artifacts.select_related("artifact").iterator()
168+
}
143169

144-
_export_to_file_system(exporter.path, content_artifacts)
170+
_export_to_file_system(exporter.path, relative_path_to_artifacts, exporter.method)
145171

146172

147173
def _get_versions_to_export(the_exporter, the_export):

0 commit comments

Comments
 (0)