Ensure repl tests stack cleanup even on errors. (#24665)

* Make the zapxml handlers have a specific logger, so we can potentially control their verbosity

* Ensure stack shutdown is called even on script errors

* Restyle
diff --git a/scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py b/scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py
index bc94ff0..6eadeba 100644
--- a/scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py
+++ b/scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py
@@ -21,6 +21,8 @@
 from .context import Context, IdlPostProcessor
 from .parsing import AttrsToAccessPrivilege, AttrsToAttribute, ParseInt
 
+LOGGER = logging.getLogger('matter-xml-parser')
+
 
 class ClusterNameHandler(BaseHandler):
     """Handles /configurator/cluster/name elements."""
@@ -137,7 +139,7 @@
                 elif attrs['op'] == 'write':
                     self._attribute.writeacl = role
                 else:
-                    logging.error("Unknown access: %r" % attrs['op'])
+                    LOGGER.error("Unknown access: %r" % attrs['op'])
 
             return BaseHandler(self.context, handled=HandledDepth.SINGLE_TAG)
         elif name.lower() == 'description':
@@ -230,8 +232,8 @@
                         found = True
 
                 if not found:
-                    logging.error('Enum %s could not find cluster (code %d/0x%X)' %
-                                  (self._struct.name, code, code))
+                    LOGGER.error('Enum %s could not find cluster (code %d/0x%X)' %
+                                 (self._struct.name, code, code))
         else:
             idl.structs.append(self._struct)
 
@@ -279,8 +281,8 @@
                     found = True
 
             if not found:
-                logging.error('Enum %s could not find its cluster (code %d/0x%X)' %
-                              (self._enum.name, self._cluster_code, self._cluster_code))
+                LOGGER.error('Enum %s could not find its cluster (code %d/0x%X)' %
+                             (self._enum.name, self._cluster_code, self._cluster_code))
 
     def EndProcessing(self):
         self.context.AddIdlPostProcessor(self)
@@ -319,7 +321,7 @@
             # Log only instead of critical, as not our XML is well formed.
             # For example at the time of writing this, SwitchFeature in switch-cluster.xml
             # did not have a code associated with it.
-            logging.error("Bitmap %r has no cluster codes" % self._bitmap)
+            LOGGER.error("Bitmap %r has no cluster codes" % self._bitmap)
             return
 
         for code in self._cluster_codes:
@@ -329,8 +331,8 @@
                     c.bitmaps.append(self._bitmap)
                     found = True
             if not found:
-                logging.error('Bitmap %s could not find its cluster (code %d/0x%X)' %
-                              (self._bitmap.name, code, code))
+                LOGGER.error('Bitmap %s could not find its cluster (code %d/0x%X)' %
+                             (self._bitmap.name, code, code))
 
     def EndProcessing(self):
         self.context.AddIdlPostProcessor(self)
@@ -413,7 +415,7 @@
             if self._command:
                 self._command.invokeacl = AttrsToAccessPrivilege(attrs)
             else:
-                logging.warning(
+                LOGGER.warning(
                     "Ignored access role for reply %r" % self._struct)
             return BaseHandler(self.context, handled=HandledDepth.SINGLE_TAG)
         elif name.lower() == 'arg':
@@ -448,7 +450,7 @@
         if name.lower() == 'featurebit':
             # It is uncler what featurebits mean. likely a bitmap should be created
             # here, however only one such example exists currently: door-lock-cluster.xml
-            logging.info('Ignoring featurebit tag for global attribute 0x%X (%d)' % (
+            LOGGER.info('Ignoring featurebit tag for global attribute 0x%X (%d)' % (
                 self._code, self._code))
             return BaseHandler(self.context, handled=HandledDepth.SINGLE_TAG)
         else:
@@ -533,8 +535,8 @@
                 c.commands.extend(self._cluster.commands)
 
         if not found:
-            logging.error('Could not extend cluster 0x%X (%d): cluster not found' %
-                          (self._cluster_code, self._cluster_code))
+            LOGGER.error('Could not extend cluster 0x%X (%d): cluster not found' %
+                         (self._cluster_code, self._cluster_code))
 
 
 class GlobalAttributeHandler(BaseHandler):
@@ -572,7 +574,7 @@
             if attrs['side'].lower() == 'client':
                 # We expect to also have 'server' equivalent, so ignore client
                 # side attributes
-                logging.debug(
+                LOGGER.debug(
                     'Ignoring global client-side attribute %s' % (attrs['code']))
                 return BaseHandler(self.context, handled=HandledDepth.SINGLE_TAG)
 
diff --git a/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py b/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py
index cac0db0..437920f 100644
--- a/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py
+++ b/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py
@@ -14,6 +14,7 @@
 #    See the License for the specific language governing permissions and
 #    limitations under the License.
 
+import atexit
 import os
 import tempfile
 import traceback
@@ -39,6 +40,11 @@
     os.path.join(_DEFAULT_CHIP_ROOT, "src/app/zap-templates/zcl/data-model/"))
 
 
+def StackShutdown():
+    certificateAuthorityManager.Shutdown()
+    builtins.chipStack.Shutdown()
+
+
 @click.command()
 @click.option(
     '--setup-code',
@@ -78,9 +84,18 @@
         dev_ctrl = ca_list[0].adminList[0].NewController()
         dev_ctrl.CommissionWithCode(setup_code, node_id)
 
+        def _StackShutDown():
+            # Tearing down chip stack. If not done in the correct order test will fail.
+            certificate_authority_manager.Shutdown()
+            chip_stack.Shutdown()
+
+        atexit.register(_StackShutDown)
+
         try:
             # Creating Cluster definition.
-            clusters_definitions = SpecDefinitionsFromPath(_CLUSTER_XML_DIRECTORY_PATH + '/*/*.xml')
+            clusters_definitions = SpecDefinitionsFromPath(
+                _CLUSTER_XML_DIRECTORY_PATH + '/*/*.xml',
+            )
 
             # Parsing YAML test and setting up chip-repl yamltests runner.
             yaml = TestParser(yaml_path, pics_file, clusters_definitions)
@@ -105,9 +120,6 @@
             exit(-2)
 
         runner.shutdown()
-        # Tearing down chip stack. If not done in the correct order test will fail.
-        certificate_authority_manager.Shutdown()
-        chip_stack.Shutdown()
 
 
 if __name__ == '__main__':