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 43 of file PoolFile.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 58 of file PoolFile.py.

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

Member Function Documentation

◆ __call__()

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

Definition at line 248 of file PoolFile.py.

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

◆ _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 184 of file PoolFile.py.

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

◆ 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 174 of file PoolFile.py.

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

Member Data Documentation

◆ AllowedProtocols

python.PoolFile.PoolFileCatalog.AllowedProtocols
static

Definition at line 49 of file PoolFile.py.

◆ catalog

python.PoolFile.PoolFileCatalog.catalog

Definition at line 60 of file PoolFile.py.

◆ DefaultCatalog

python.PoolFile.PoolFileCatalog.DefaultCatalog
static

Definition at line 48 of file PoolFile.py.


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