00001
00002
00003
00004 """Unix-like tools and helpers."""
00005
00006 import os, string, sys
00007
00008
00009
00010 __version__ = '1.0.0'
00011 __author__ = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
00012
00013 __all__ = [ 'FindFile', 'which', 'where' ]
00014
00015
00016
00017 def FindFile( filename, pathlist, access ):
00018 """Find <filename> with rights <access> through <pathlist>."""
00019
00020
00021 if os.path.dirname( filename ):
00022 if os.access( filename, access ):
00023 return filename
00024
00025
00026 for path in pathlist:
00027 f = os.path.join( path, filename )
00028 if os.access( f, access ):
00029 return f
00030
00031
00032 return None
00033
00034
00035
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
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
00050 def where( filename, prepath = [] ):
00051 """Search for <filename> in the python path and the given <prepath>."""
00052
00053
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