From fae132b05de5d5d9a8d3921161ce9bbb5b3f253c Mon Sep 17 00:00:00 2001
From: Michele Renda <michele.renda@cern.ch>
Date: Thu, 22 Jul 2021 08:21:56 +0000
Subject: [PATCH] First version of fix-ATEAM-767

---
 Tools/PyUtils/python/MetaReader.py           | 18 +++++++++++++++++-
 Tools/PyUtils/python/MetaReaderPeeker.py     |  2 +-
 Tools/PyUtils/python/MetaReaderPeekerFull.py |  2 +-
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/Tools/PyUtils/python/MetaReader.py b/Tools/PyUtils/python/MetaReader.py
index bf648b097601..3b21d5b33ebd 100644
--- a/Tools/PyUtils/python/MetaReader.py
+++ b/Tools/PyUtils/python/MetaReader.py
@@ -5,6 +5,7 @@ import os
 import re
 from fnmatch import fnmatchcase
 from AthenaCommon.Logging import logging
+from ROOT import gSystem
 
 msg = logging.getLogger('MetaReader')
 
@@ -24,7 +25,7 @@ regex_BS_files = re.compile(r'^(\w+):.*((\.D?RAW\..*)|(\.data$))')
 
 
 def read_metadata(filenames, file_type = None, mode = 'lite', promote = None, meta_key_filter = [],
-                  unique_tag_info_values = True):
+                  unique_tag_info_values = True, ignoreNonExistingFiles=False):
     """
     This tool is independent of Athena framework and returns the metadata from a given file.
     :param filenames: the input file from which metadata needs to be extracted.
@@ -68,6 +69,11 @@ def read_metadata(filenames, file_type = None, mode = 'lite', promote = None, me
         # Determine the file_type of the input and store this information into meta_dict
         if not file_type:
             if os.path.isfile(filename):
+                
+                if ignoreNonExistingFiles and gSystem.AccessPathName(filename): # Attention, bizarre convention of return value!! 
+                    msg.warn('Ignoring not accessible file: {}'.format(filename))
+                    continue
+                    
                 with open(filename, 'rb') as binary_file:
                     magic_file = binary_file.read(4)
 
@@ -99,6 +105,11 @@ def read_metadata(filenames, file_type = None, mode = 'lite', promote = None, me
 
         # ----- retrieves metadata from POOL files ------------------------------------------------------------------#
         if current_file_type == 'POOL':
+            
+            if ignoreNonExistingFiles and gSystem.AccessPathName(filename): # Attention, bizarre convention of return value!! 
+                msg.warn('Ignoring not accessible file: {}'.format(filename))
+                continue
+                    
             import ROOT
             # open the file using ROOT.TFile
             current_file = ROOT.TFile.Open( _get_pfn(filename) )
@@ -297,6 +308,11 @@ def read_metadata(filenames, file_type = None, mode = 'lite', promote = None, me
 
         # ----- retrieves metadata from bytestream (BS) files (RAW, DRAW) ------------------------------------------#
         elif current_file_type == 'BS':
+            
+            if ignoreNonExistingFiles and not os.path.isfile(filename): 
+                msg.warn('Ignoring not accessible file: {}'.format(filename))
+                continue
+            
             import eformat
 
             # store the number of entries
diff --git a/Tools/PyUtils/python/MetaReaderPeeker.py b/Tools/PyUtils/python/MetaReaderPeeker.py
index 60d02b590b03..b9c80346ef5f 100644
--- a/Tools/PyUtils/python/MetaReaderPeeker.py
+++ b/Tools/PyUtils/python/MetaReaderPeeker.py
@@ -33,7 +33,7 @@ def _setup():
             msg.warning("No input files specified yet! Cannot do anything.")
             return
 
-        metadata_all_files = read_metadata(inFiles, mode='peeker', promote=True)
+        metadata_all_files = read_metadata(inFiles, mode='peeker', promote=True, ignoreNonExistingFiles=True)
 
         # use first non-empty file
         first_filename = None
diff --git a/Tools/PyUtils/python/MetaReaderPeekerFull.py b/Tools/PyUtils/python/MetaReaderPeekerFull.py
index e629e2e346a7..ac8bce15e4ae 100644
--- a/Tools/PyUtils/python/MetaReaderPeekerFull.py
+++ b/Tools/PyUtils/python/MetaReaderPeekerFull.py
@@ -34,7 +34,7 @@ def _setup():
             msg.warning("No input files specified yet! Cannot do anything.")
             return
 
-        metadata_all_files = read_metadata(inFiles, mode='full')
+        metadata_all_files = read_metadata(inFiles, mode='full', ignoreNonExistingFiles=True)
 
         first_filename = inFiles[-1]  # take only the last input file from the infiles
 
-- 
GitLab