| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

unixtools.py

Go to the documentation of this file.
00001 # File: AthenaCommon/python/Utils/unixtools.py
00002 # Author: Wim Lavrijsen  (LBNL, WLavrijsen@lbl.gov)
00003 
00004 """Unix-like tools and helpers."""
00005 
00006 import os, string, sys
00007 
00008 
00009 ### data ________________________________________________________________________
00010 __version__ = '1.0.0'
00011 __author__  = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
00012 
00013 __all__ = [ 'FindFile', 'which', 'where' ]
00014 
00015 
00016 ### helper ----------------------------------------------------------------------
00017 def FindFile( filename, pathlist, access ):
00018    """Find <filename> with rights <access> through <pathlist>."""
00019 
00020  # special case for those filenames that already contain a path
00021    if os.path.dirname( filename ):
00022       if os.access( filename, access ):
00023          return filename
00024 
00025  # test the file name in all possible paths until first found
00026    for path in pathlist:
00027       f = os.path.join( path, filename )
00028       if os.access( f, access ):
00029          return f
00030 
00031  # no such accessible file avalailable
00032    return None
00033 
00034 
00035 ### UNIX-style which ------------------------------------------------------------
00036 def which( filename, env = os.environ ):
00037    """Search for <filename> through the PATH in environment <env>. Only executable
00038 files will be returned."""
00039 
00040  # retrieve the value of the PATH environment variable
00041    try:
00042       p = env[ 'PATH' ]
00043    except KeyError:
00044       p = os.defpath
00045 
00046    return FindFile( filename, string.split( p, os.pathsep ), os.X_OK )
00047 
00048 
00049 ### "which" for python files ----------------------------------------------------
00050 def where( filename, prepath = [] ):
00051    """Search for <filename> in the python path and the given <prepath>."""
00052 
00053  # look in the given path and in the python path
00054    pathlist = prepath + sys.path
00055 
00056    for ext in [ '', '.py', '.pyc', '.so' ]:
00057       result = FindFile( filename + ext, pathlist, os.R_OK )
00058       if result:
00059          break
00060 
00061    return result
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:13:00 2011 for DybPython by doxygen 1.4.7