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

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 50 of file PoolFile.py.

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

Member Function Documentation

◆ __call__()

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

Definition at line 240 of file PoolFile.py.

240  def __call__ (self, url_or_fid):
241  return self.pfn (url_or_fid)
242 

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

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

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

166  def pfn (self, url_or_fid):
167  """find the physical file name given a url or a file-id"""
168  import os.path as osp
169  url_or_fid = osp.expanduser(osp.expandvars(url_or_fid))
170  import types
171  if isinstance (url_or_fid, types.ListType):
172  return [self._pfn(f) for f in url_or_fid]
173  else:
174  return self._pfn(url_or_fid)
175 

Member Data Documentation

◆ AllowedProtocols

python.PoolFile.PoolFileCatalog.AllowedProtocols
static

Definition at line 41 of file PoolFile.py.

◆ catalog

python.PoolFile.PoolFileCatalog.catalog

Definition at line 52 of file PoolFile.py.

◆ DefaultCatalog

python.PoolFile.PoolFileCatalog.DefaultCatalog
static

Definition at line 40 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
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Trk::split
@ split
Definition: LayerMaterialProperties.h:38