ATLAS Offline Software
unixtools.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 # File: AthenaCommon/python/Utils/unixtools.py
4 # Author: Wim Lavrijsen (LBNL, WLavrijsen@lbl.gov)
5 
6 """Unix-like tools and helpers."""
7 
8 import os, sys
9 
10 
11 
12 __version__ = '1.0.0'
13 __author__ = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
14 
15 __all__ = [ 'FindFile', 'which', 'where',
16  'find_datafile',]
17 
18 
19 
20 def FindFile( filename, pathlist, access ):
21  """Find <filename> with rights <access> through <pathlist>."""
22 
23  # special case for those filenames that already contain a path
24  if os.path.dirname( filename ):
25  if os.access( filename, access ):
26  return filename
27 
28  # test the file name in all possible paths until first found
29  for path in pathlist:
30  f = os.path.join( path, filename )
31  if os.access( f, access ):
32  return f
33 
34  # no such accessible file avalailable
35  return None
36 
37 
38 
39 def which( filename, env = os.environ ):
40  """Search for <filename> through the PATH in environment <env>. Only executable
41 files will be returned."""
42 
43  # retrieve the value of the PATH environment variable
44  try:
45  p = env[ 'PATH' ]
46  except KeyError:
47  p = os.defpath
48 
49  return FindFile( filename, p.split( os.pathsep ), os.X_OK )
50 
51 
52 
53 def where( filename, prepath = [] ):
54  """Search for <filename> in the python path and the given <prepath>."""
55 
56  # look in the given path and in the python path
57  pathlist = prepath + sys.path
58 
59  for ext in [ '', '.py', '.pyc', '.so' ]:
60  result = FindFile( filename + ext, pathlist, os.R_OK )
61  if result:
62  break
63 
64  return result
65 
66 
67 def find_datafile(fname, pathlist=None, access=os.R_OK):
68  """the python equivalent to the C++ PathResolver for datafiles.
69  """
70  if pathlist is None:
71  pathlist = os.getenv('DATAPATH').split(os.pathsep)
72 
73  if not isinstance(pathlist, list):
74  raise TypeError("pathlist must be a list or an iterable (got %s)" %
75  type(pathlist))
76 
77  return FindFile(fname, pathlist, access)
python.Utils.unixtools.FindFile
def FindFile(filename, pathlist, access)
helper -------------------------------------------------------------------—
Definition: unixtools.py:20
python.Utils.unixtools.where
def where(filename, prepath=[])
"which" for python files -------------------------------------------------—
Definition: unixtools.py:53
python.Utils.unixtools.which
def which(filename, env=os.environ)
UNIX-style which ---------------------------------------------------------—.
Definition: unixtools.py:39
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
python.Utils.unixtools.find_datafile
def find_datafile(fname, pathlist=None, access=os.R_OK)
pathresolver-like helper function --------------------------------------—
Definition: unixtools.py:67
Trk::split
@ split
Definition: LayerMaterialProperties.h:38