@@ -195,14 +195,16 @@ def publish(
195195
196196 release_helpers = []
197197 for distribution in distributions :
198+ release_arch_qs = ReleaseArchitecture .objects .filter (
199+ pk__in = repo_version .content .order_by ("-pulp_created" ),
200+ distribution = distribution ,
201+ )
198202 architectures = list (
199- ReleaseArchitecture .objects .filter (
200- pk__in = repo_version .content .order_by ("-pulp_created" ),
201- distribution = distribution ,
203+ release_arch_qs .distinct ("architecture" ).values_list (
204+ "architecture" , flat = True
202205 )
203- .distinct ("architecture" )
204- .values_list ("architecture" , flat = True )
205206 )
207+
206208 if "all" not in architectures :
207209 architectures .append ("all" )
208210
@@ -268,16 +270,27 @@ def publish(
268270 )
269271
270272 for component in components :
273+ prcs_for_component = [
274+ prc
275+ for prc in package_release_components
276+ if prc .release_component .component == component
277+ ]
271278 packages = Package .objects .filter (
272- pk__in = [
273- prc .package .pk
274- for prc in package_release_components
275- if prc .release_component .component == component
276- ]
279+ pk__in = [prc .package .pk for prc in prcs_for_component ]
277280 ).prefetch_related ("contentartifact_set" , "_artifacts" )
278281 artifact_dict , remote_artifact_dict = _batch_fetch_artifacts (packages )
282+
283+ pkg_by_pk = {p .pk : p for p in packages }
284+ package_pairs = [
285+ (
286+ pkg_by_pk [prc .package .pk ],
287+ prc .index_architecture or pkg_by_pk [prc .package .pk ].architecture ,
288+ )
289+ for prc in prcs_for_component
290+ if prc .package .pk in pkg_by_pk
291+ ]
279292 release_helper .components [component ].add_packages (
280- packages ,
293+ package_pairs ,
281294 artifact_dict ,
282295 remote_artifact_dict ,
283296 )
@@ -338,56 +351,74 @@ def __init__(self, parent, component):
338351 source_index_path ,
339352 )
340353
341- def add_packages (self , packages , artifact_dict , remote_artifact_dict ):
354+ def add_packages (self , package_pairs , artifact_dict , remote_artifact_dict ):
342355 published_artifacts = []
343- package_data = []
356+ packages = [p for ( p , _idx ) in package_pairs ]
344357
345358 content_artifacts = {
346359 package .pk : list (package .contentartifact_set .all ()) for package in packages
347360 }
348361 layout = self .parent .publication .layout
349362
350- for package in packages :
363+ for package , index_arch in package_pairs :
351364 with suppress (IntegrityError ):
352365 content_artifact = content_artifacts .get (package .pk , [None ])[0 ]
366+ if content_artifact is None :
367+ continue
368+
369+ upstream_basename = os .path .basename (content_artifact .relative_path )
370+
371+ primary_relpath = package .filename (
372+ self .component , layout = layout , basename_override = upstream_basename
373+ )
353374 published_artifact = PublishedArtifact (
354- relative_path = package . filename ( self . component , layout = layout ) ,
375+ relative_path = primary_relpath ,
355376 publication = self .parent .publication ,
356377 content_artifact = content_artifact ,
357378 )
358379 published_artifacts .append (published_artifact )
359- package_data . append (( package , package . architecture ))
380+
360381 # In the NESTED_BY_BOTH layout, we want to _also_ publish the package under the
361382 # alphabetical path but _not_ reference it in the repo metadata.
362383 if layout == LAYOUT_TYPES .NESTED_BY_BOTH :
363- alt_published_artifact = PublishedArtifact (
364- relative_path = package .filename (
365- self .component , layout = LAYOUT_TYPES .NESTED_ALPHABETICALLY
366- ),
367- publication = self .parent .publication ,
368- content_artifact = content_artifact ,
384+ alt_relpath = package .filename (
385+ self .component ,
386+ layout = LAYOUT_TYPES .NESTED_ALPHABETICALLY ,
387+ basename_override = upstream_basename ,
369388 )
370- published_artifacts .append (alt_published_artifact )
389+ published_artifacts .append (
390+ PublishedArtifact (
391+ relative_path = alt_relpath ,
392+ publication = self .parent .publication ,
393+ content_artifact = content_artifact ,
394+ )
395+ )
396+
397+ package_serializer = Package822Serializer (package , context = {"request" : None })
398+ try :
399+ package_serializer .to822 (
400+ self .component ,
401+ artifact_dict ,
402+ remote_artifact_dict ,
403+ layout = layout ,
404+ basename_override = upstream_basename ,
405+ ).dump (self .package_index_files [index_arch ][0 ])
406+ except KeyError :
407+ log .warning (
408+ "Published package '%s' with index architecture '%s' was not added to "
409+ "component '%s' in distribution '%s' because it lacks this architecture!" ,
410+ getattr (package , "relative_path" , None ) or getattr (package , "pk" , None ),
411+ index_arch ,
412+ self .component ,
413+ self .parent .distribution ,
414+ )
415+ else :
416+ self .package_index_files [index_arch ][0 ].write (b"\n " )
371417
372418 with transaction .atomic ():
373419 if published_artifacts :
374420 PublishedArtifact .objects .bulk_create (published_artifacts , ignore_conflicts = True )
375421
376- for package , architecture in package_data :
377- package_serializer = Package822Serializer (package , context = {"request" : None })
378- try :
379- package_serializer .to822 (
380- self .component , artifact_dict , remote_artifact_dict , layout = layout
381- ).dump (self .package_index_files [architecture ][0 ])
382- except KeyError :
383- log .warn (
384- f"Published package '{ package .relative_path } ' with architecture "
385- f"'{ architecture } ' was not added to component '{ self .component } ' in "
386- f"distribution '{ self .parent .distribution } ' because it lacks this architecture!"
387- )
388- else :
389- self .package_index_files [architecture ][0 ].write (b"\n " )
390-
391422 # Publish DSC file and setup to create Sources Indices file
392423 def add_source_packages (self , source_packages ):
393424 published_artifacts = []
0 commit comments