ATLAS Offline Software
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | List of all members
python.PoolFile.PoolFileCatalog Class Reference
Inheritance diagram for python.PoolFile.PoolFileCatalog:
Collaboration diagram for python.PoolFile.PoolFileCatalog:

Public Member Functions

def __init__ (self, catalog=None)
 
def pfn (self, url_or_fid)
 
def __call__ (self, url_or_fid)
 

Public Attributes

 catalog
 

Static Public Attributes

 DefaultCatalog
 
 AllowedProtocols
 

Private Member Functions

def _pfn (self, url_or_fid)
 

Detailed Description

reverse-engineering of the POOL FileCatalog.
    allows to retrieve the physical filename from a logical one, provided
    that the file-id is known to the (real) PoolFileCatalog

Definition at line 42 of file PoolFile.py.

Constructor & Destructor Documentation

◆ __init__()

def python.PoolFile.PoolFileCatalog.__init__ (   self,
  catalog = None 
)

Definition at line 57 of file PoolFile.py.

57  def __init__ (self, catalog=None):
58  super (PoolFileCatalog, self).__init__()
59  self.catalog = None
60 
61  if catalog is None:
62  # chase poolfilecatalog location
63  catalog = os.environ.get("POOL_CATALOG", self.DefaultCatalog)
64 
65  if isinstance(catalog, str):
66  catalog = [catalog]
67 
68  if not isinstance (catalog, (str, list)):
69  raise TypeError(
70  "catalog contact string should be a string or a list thereof! (got %r)"%
71  type(catalog))
72 
73  osp = os.path
74  def osp_exp(x):
75  return osp.expanduser(osp.expandvars(x))
76 
77  def _handle_apcfile_old(x):
78  """ return $ATLAS_POOLCOND_PATH/poolcond/x
79  """
80  if 'ATLAS_POOLCOND_PATH' not in os.environ:
81  return osp_exp(x)
82  pcp = os.environ["ATLAS_POOLCOND_PATH"]
83  if x.startswith("apcfile:"):
84  x = x[len("apcfile:"):]
85  return osp_exp(osp.join(pcp, 'poolcond', x))
86 
87  def _handle_apcfile(x):
88  """ return $ATLAS_POOLCOND_PATH/x
89  """
90  if 'ATLAS_POOLCOND_PATH' not in os.environ:
91  return osp_exp(x)
92  pcp = os.environ["ATLAS_POOLCOND_PATH"]
93  if x.startswith("apcfile:"):
94  x = x[len("apcfile:"):]
95  return osp_exp(osp.join(pcp, x))
96 
97  def _handle_xmlcatalog_file(x):
98  return osp_exp(x[len("xmlcatalog_file:"):])
99 
100  def _handle_prfile(x):
101  x = x[len("prfile:"):]
102  x = osp_exp(x)
103  try:
104  import AthenaCommon.Utils.unixtools as u
105  return u.FindFile(x,
106  os.environ['DATAPATH'].split(os.pathsep),
107  os.R_OK)
108  except ImportError:
109  return x
110 
111  def _handle_file(x):
112  x = x[len("file:"):]
113  x = osp_exp(x)
114  return x
115 
116  cat_dispatch = {
117  "xmlcatalog_file:": _handle_xmlcatalog_file,
118  "apcfile:": _handle_apcfile,
119  "prfile:": _handle_prfile,
120  "file:": _handle_file,
121  }
122  assert sorted(cat_dispatch.keys()) == sorted(self.AllowedProtocols), \
123  "catalog dispatch keys does not match AllowedProtocols:" \
124  "\n%s\n%s" % (sorted(cat_dispatch.keys()),
125  sorted(self.AllowedProtocols))
126 
127  from . import xmldict
128  def _build_catalog(catalog):
129  if not catalog.startswith(self.AllowedProtocols):
130  raise ValueError(
131  "sorry PoolFile:PoolFileCatalog only supports %s"
132  " as a protocol for the POOL file catalog (got: '%s')"
133  % (self.AllowedProtocols, catalog)
134  )
135  for protocol, handler in cat_dispatch.iteritems():
136  if catalog.startswith(protocol):
137  catalog = handler(catalog)
138  break
139  # make sure the catalog exists...
140  import os
141 
142  if not os.path.exists (catalog):
143  return {}
144  # raise RuntimeError(
145  # 'could not find any PoolFileCatalog in [%s]' % catalog
146  # )
147 
148 
149  root = xmldict.ElementTree.parse (catalog).getroot()
150  return dict(xmldict.xml2dict(root))
151 
152  errors = []
153  cat = {'POOLFILECATALOG':{'File':[]}}
154  for c in catalog:
155  try:
156  bc = _build_catalog(c)
157  pc = bc.get('POOLFILECATALOG',{})
158  files = []
159  if pc:
160  files = pc.get('File',[])
161  if isinstance(files, dict):
162  files = [files]
163  cat['POOLFILECATALOG']['File'].extend(files)
164  except Exception as err:
165  errors.append(err)
166 
167  if errors:
168  raise errors[0] # FIXME : should we customize this a bit ?
169 
170  self.catalog = cat
171  pass
172 

Member Function Documentation

◆ __call__()

def python.PoolFile.PoolFileCatalog.__call__ (   self,
  url_or_fid 
)

Definition at line 247 of file PoolFile.py.

247  def __call__ (self, url_or_fid):
248  return self.pfn (url_or_fid)
249 

◆ _pfn()

def python.PoolFile.PoolFileCatalog._pfn (   self,
  url_or_fid 
)
private
find the physical file name given a url or a file-id

Definition at line 183 of file PoolFile.py.

183  def _pfn (self, url_or_fid):
184  """find the physical file name given a url or a file-id"""
185  if not ('POOLFILECATALOG' in self.catalog):
186  return None
187  if not ('File' in self.catalog['POOLFILECATALOG']):
188  return None
189 
190  PFN_IDX = 0 # take this pfn when alternates exist
191 
192  files = self.catalog['POOLFILECATALOG']['File']
193  if isinstance(files, dict):
194  # in case there where only one entry in the catalog
195  files = [files]
196  import re
197  if url_or_fid.lower().startswith('fid:'):
198  url_or_fid = url_or_fid[len('fid:'):]
199  if re.compile (r'\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$').match (url_or_fid):
200  fid = url_or_fid.lower()
201  # better to check consistency of catalog over all entries
202  # than declare success on first match...
203  match = {}
204  for f in files:
205  if f.ID.lower() == fid:
206  match[fid] = []
207  pfn = f.physical.pfn
208  if isinstance(pfn, (list,tuple)):
209  match[fid].append([i.name for i in pfn])
210  else:
211  match[fid].append([pfn.name])
212  if len(match[fid])==1:
213  return match[fid][0][PFN_IDX]
214  if len(match[fid])>1:
215  raise LookupError (
216  "more than one match for FID='%s'!\n%r"%(fid,match)
217  )
218  raise KeyError ("no entry with FID='%s' in catalog" % fid)
219  else:
220  url = url_or_fid
221  if url.lower().startswith("lfn:"):
222  url = url[len("lfn:"):]
223  # better to check consistency of catalog over all entries
224  # than declare success on first match...
225  match = {}
226  for f in files:
227  if (f.logical != '' # no LFN for this entry
228  and f.logical.lfn.name == url):
229  match[url] = []
230  pfn = f.physical.pfn
231  if isinstance(pfn, (list,tuple)):
232  match[url].append([i.name for i in pfn])
233  else:
234  match[url].append([pfn.name])
235  if len(match[url])==1:
236  return match[url][0][PFN_IDX]
237  if len(match[url])>1:
238  raise LookupError (
239  "more than one match for LFN='%s'!\n%r"%(url,match)
240  )
241  raise KeyError ("no entry with LFN='%s' in catalog" % url)
242  # assume that if not LFN: then PFN:, no matter what...
243  if url.lower().startswith("pfn:"):
244  url = url[len("pfn:"):]
245  return url
246 

◆ pfn()

def python.PoolFile.PoolFileCatalog.pfn (   self,
  url_or_fid 
)
find the physical file name given a url or a file-id

Definition at line 173 of file PoolFile.py.

173  def pfn (self, url_or_fid):
174  """find the physical file name given a url or a file-id"""
175  import os.path as osp
176  url_or_fid = osp.expanduser(osp.expandvars(url_or_fid))
177  import types
178  if isinstance (url_or_fid, types.ListType):
179  return [self._pfn(f) for f in url_or_fid]
180  else:
181  return self._pfn(url_or_fid)
182 

Member Data Documentation

◆ AllowedProtocols

python.PoolFile.PoolFileCatalog.AllowedProtocols
static

Definition at line 48 of file PoolFile.py.

◆ catalog

python.PoolFile.PoolFileCatalog.catalog

Definition at line 59 of file PoolFile.py.

◆ DefaultCatalog

python.PoolFile.PoolFileCatalog.DefaultCatalog
static

Definition at line 47 of file PoolFile.py.


The documentation for this class was generated from the following file:
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename R::value_type > sorted(const R &r, PROJ proj={})
Helper function to create a sorted vector from an unsorted range.
python.processes.powheg.ZZj_MiNNLO.ZZj_MiNNLO.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZj_MiNNLO.py:18
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
handler
void handler(int sig)
signal handler
Definition: rmain.cxx:99
ActsTrk::detail::MakeDerivedVariant::extend
constexpr std::variant< Args..., T > extend(const std::variant< Args... > &, const T &)
Definition: MakeDerivedVariant.h:17
Trk::split
@ split
Definition: LayerMaterialProperties.h:38