ATLAS Offline Software
Loading...
Searching...
No Matches
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
8import 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
20def 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
39def which( filename, env = os.environ ):
40 """Search for <filename> through the PATH in environment <env>. Only executable
41files 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
53def 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
67def 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)
78
79
80def find_calibfile(fname, pathlist=None, access=os.R_OK):
81 """the python equivalent to the C++ PathResolver for calibfiles.
82 """
83 if pathlist is None:
84 pathlist = os.getenv('CALIBPATH').split(os.pathsep)
85
86 return find_datafile(fname, pathlist, access)
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
FindFile(filename, pathlist, access)
helper -------------------------------------------------------------------—
Definition unixtools.py:20
which(filename, env=os.environ)
UNIX-style which ---------------------------------------------------------—.
Definition unixtools.py:39
find_calibfile(fname, pathlist=None, access=os.R_OK)
pathresolver-like helper function --------------------------------------—
Definition unixtools.py:80
where(filename, prepath=[])
"which" for python files -------------------------------------------------—
Definition unixtools.py:53
find_datafile(fname, pathlist=None, access=os.R_OK)
pathresolver-like helper function --------------------------------------—
Definition unixtools.py:67