#NEWLINE# def which( cmd , mode = ( os . F_OK | os . X_OK ) , path = None ) : #NEWLINE# #INDENT# 'Given a command, mode, and a PATH string, return the path which\n        conforms to the given mode on the PATH, or None if there is no such\n        file.\n\n        `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result\n        of os.environ.get("PATH"), or can be overridden with a custom search\n        path.\n\n        ' #NEWLINE# def _access_check( fn , mode ) : #NEWLINE# #INDENT# return ( os . path . exists ( fn ) and os . access ( fn , mode ) and ( not os . path . isdir ( fn ) ) ) #NEWLINE# #UNINDENT# if os . path . dirname ( cmd ) : #NEWLINE# #INDENT# if _access_check ( cmd , mode ) : #NEWLINE# #INDENT# return cmd #NEWLINE# #UNINDENT# return None #NEWLINE# #UNINDENT# if ( path is None ) : #NEWLINE# #INDENT# path = os . environ . get ( 'PATH' , os . defpath ) #NEWLINE# #UNINDENT# if ( not path ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# path = path . split ( os . pathsep ) #NEWLINE# if ( sys . platform == 'win32' ) : #NEWLINE# #INDENT# if ( not ( os . curdir in path ) ) : #NEWLINE# #INDENT# path . insert ( 0 , os . curdir ) #NEWLINE# #UNINDENT# pathext = os . environ . get ( 'PATHEXT' , '' ) . split ( os . pathsep ) #NEWLINE# if any ( ( cmd . lower ( ) . endswith ( ext . lower ( ) ) for ext in pathext ) ) : #NEWLINE# #INDENT# files = [ cmd ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# files = [ ( cmd + ext ) for ext in pathext ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# files = [ cmd ] #NEWLINE# #UNINDENT# seen = set ( ) #NEWLINE# for dir in path : #NEWLINE# #INDENT# normdir = os . path . normcase ( dir ) #NEWLINE# if ( not ( dir in seen ) ) : #NEWLINE# #INDENT# seen . add ( normdir ) #NEWLINE# for thefile in files : #NEWLINE# #INDENT# name = os . path . join ( dir , thefile ) #NEWLINE# if _access_check ( name , mode ) : #NEWLINE# #INDENT# return name #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return None