ATLAS Offline Software
Deduplication.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 #
3 # Functions used by the ComponentAccumulator to de-duplicate components.
4 #
5 
6 from AthenaConfiguration.DebuggingContext import raiseWithCurrentContext
7 from AthenaCommon.Logging import logging
8 
9 class DeduplicationFailed(RuntimeError):
10  pass
11 
12 _msg = logging.getLogger('ComponentAccumulator')
13 
14 
15 def deduplicate(newComp, compList):
16  """Merge newComp with compList. If a matching component is found it is updated
17  in compList. Otherwise a new component is added to compList.
18 
19  Returns True if a new component has been added, otherwise False. If merging fails,
20  an exception is raised.
21  """
22 
23  for idx, comp in enumerate(compList):
24  if comp.__cpp_type__==newComp.__cpp_type__ and comp.name==newComp.name:
25  exception = None
26  try:
27  newComp.merge(comp)
28  except Exception as e:
29  exception = e # remember exception and raise again outside the handler
30  if exception:
31  raiseWithCurrentContext(exception)
32 
33  # We found a service of the same type and name and could reconcile the two instances.
34  # Overwrite the component in the list with the new (merged) component.
35  _msg.verbose("Reconciled configuration of component %s", comp.name)
36  compList[idx] = newComp
37  return False
38 
39  # No component of the same type and name found, simply append
40  _msg.debug("Adding component %s/%s to the job", newComp.__cpp_type__, newComp.name)
41  compList.append(newComp)
42  return True
43 
44 
45 def deduplicateOne(newComp, oldComp):
46  """Merge newComp with oldComp and provide diagnostics on failure."""
47 
48  exception = None
49  try:
50  assert oldComp.__cpp_type__ == newComp.__cpp_type__, "Deduplicating components of different type"
51  assert oldComp.name == newComp.name, "Deduplicating components of different name"
52  oldComp.merge(newComp)
53  except Exception as e:
54  exception = e # remember exception and raise again outside the handler
55  if exception:
56  raiseWithCurrentContext(exception)
python.DebuggingContext.raiseWithCurrentContext
def raiseWithCurrentContext(exception)
Definition: DebuggingContext.py:44
python.Deduplication.deduplicateOne
def deduplicateOne(newComp, oldComp)
Definition: Deduplication.py:45
python.Deduplication.DeduplicationFailed
Definition: Deduplication.py:9
python.Deduplication.deduplicate
def deduplicate(newComp, compList)
Definition: Deduplication.py:15