<s> r"""<STR_LIT>""" <EOL> _cmdlnUsage = """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> __version_info__ = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> __version__ = '<STR_LIT:.>' . join ( map ( str , __version_info__ ) ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> import os <EOL> import sys <EOL> import getopt <EOL> import stat <EOL> class WhichError ( Exception ) : <EOL> pass <EOL> def _getRegisteredExecutable ( exeName ) : <EOL> """<STR_LIT>""" <EOL> registered = None <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> if os . path . splitext ( exeName ) [ <NUM_LIT:1> ] . lower ( ) != '<STR_LIT>' : <EOL> exeName += '<STR_LIT>' <EOL> import _winreg <EOL> try : <EOL> key = "<STR_LIT>" + exeName <EOL> value = _winreg . QueryValue ( _winreg . HKEY_LOCAL_MACHINE , key ) <EOL> registered = ( value , "<STR_LIT>" + key ) <EOL> except _winreg . error : <EOL> pass <EOL> if registered and not os . path . exists ( registered [ <NUM_LIT:0> ] ) : <EOL> registered = None <EOL> return registered <EOL> def _samefile ( fname1 , fname2 ) : <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> return ( os . path . normpath ( os . path . normcase ( fname1 ) ) == os . path . normpath ( os . path . normcase ( fname2 ) ) ) <EOL> else : <EOL> return os . path . samefile ( fname1 , fname2 ) <EOL> def _cull ( potential , matches , verbose = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> for match in matches : <EOL> if _samefile ( potential [ <NUM_LIT:0> ] , match [ <NUM_LIT:0> ] ) : <EOL> if verbose : <EOL> sys . stderr . write ( "<STR_LIT>" % potential ) <EOL> return None <EOL> else : <EOL> if not stat . S_ISREG ( os . stat ( potential [ <NUM_LIT:0> ] ) . st_mode ) : <EOL> if verbose : <EOL> sys . stderr . write ( "<STR_LIT>" % potential ) <EOL> elif sys . platform != "<STR_LIT:win32>" and not os . access ( potential [ <NUM_LIT:0> ] , os . X_OK ) : <EOL> if verbose : <EOL> sys . stderr . write ( "<STR_LIT>" % potential ) <EOL> else : <EOL> matches . append ( potential ) <EOL> return potential <EOL> def whichgen ( command , path = None , verbose = <NUM_LIT:0> , exts = None ) : <EOL> """<STR_LIT>""" <EOL> matches = [ ] <EOL> if path is None : <EOL> usingGivenPath = <NUM_LIT:0> <EOL> path = os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) . split ( os . pathsep ) <EOL> if sys . platform . startswith ( "<STR_LIT>" ) : <EOL> path . insert ( <NUM_LIT:0> , os . curdir ) <EOL> else : <EOL> usingGivenPath = <NUM_LIT:1> <EOL> if sys . platform . startswith ( "<STR_LIT>" ) : <EOL> if exts is None : <EOL> exts = os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) . split ( os . pathsep ) <EOL> for ext in exts : <EOL> if ext . lower ( ) == "<STR_LIT>" : <EOL> break <EOL> else : <EOL> exts = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> elif not isinstance ( exts , list ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> else : <EOL> if exts is not None : <EOL> raise WhichError ( "<STR_LIT>" "<STR_LIT>" % sys . platform ) <EOL> exts = [ ] <EOL> if os . sep in command or os . altsep and os . altsep in command : <EOL> if os . path . exists ( command ) : <EOL> match = _cull ( ( command , "<STR_LIT>" ) , matches , verbose ) <EOL> if verbose : <EOL> yield match <EOL> else : <EOL> yield match [ <NUM_LIT:0> ] <EOL> else : <EOL> for i in range ( len ( path ) ) : <EOL> dirName = path [ i ] <EOL> if sys . platform . startswith ( "<STR_LIT>" ) and len ( dirName ) >= <NUM_LIT:2> and dirName [ <NUM_LIT:0> ] == '<STR_LIT:">' and dirName [ - <NUM_LIT:1> ] == '<STR_LIT:">' : <EOL> dirName = dirName [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> for ext in [ '<STR_LIT>' ] + exts : <EOL> absName = os . path . abspath ( <EOL> os . path . normpath ( os . path . join ( dirName , command + ext ) ) ) <EOL> if os . path . isfile ( absName ) : <EOL> if usingGivenPath : <EOL> fromWhere = "<STR_LIT>" % i <EOL> elif not sys . platform . startswith ( "<STR_LIT>" ) : <EOL> fromWhere = "<STR_LIT>" % i <EOL> elif i == <NUM_LIT:0> : <EOL> fromWhere = "<STR_LIT>" <EOL> else : <EOL> fromWhere = "<STR_LIT>" % ( i - <NUM_LIT:1> ) <EOL> match = _cull ( ( absName , fromWhere ) , matches , verbose ) <EOL> if match : <EOL> if verbose : <EOL> yield match <EOL> else : <EOL> yield match [ <NUM_LIT:0> ] <EOL> match = _getRegisteredExecutable ( command ) <EOL> if match is not None : <EOL> match = _cull ( match , matches , verbose ) <EOL> if match : <EOL> if verbose : <EOL> yield match <EOL> else : <EOL> yield match [ <NUM_LIT:0> ] <EOL> def which ( command , path = None , verbose = <NUM_LIT:0> , exts = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> match = whichgen ( command , path , verbose , exts ) . next ( ) <EOL> except StopIteration : <EOL> raise WhichError ( "<STR_LIT>" % command ) <EOL> return match <EOL> def whichall ( command , path = None , verbose = <NUM_LIT:0> , exts = None ) : <EOL> """<STR_LIT>""" <EOL> return list ( whichgen ( command , path , verbose , exts ) ) <EOL> def main ( argv ) : <EOL> all = <NUM_LIT:0> <EOL> verbose = <NUM_LIT:0> <EOL> altpath = None <EOL> exts = None <EOL> try : <EOL> optlist , args = getopt . getopt ( argv [ <NUM_LIT:1> : ] , '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT:all>' , '<STR_LIT:version>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> except getopt . GetoptError , msg : <EOL> sys . stderr . write ( "<STR_LIT>" % ( msg , argv ) ) <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> return <NUM_LIT:1> <EOL> for opt , optarg in optlist : <EOL> if opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> print _cmdlnUsage <EOL> return <NUM_LIT:0> <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> print "<STR_LIT>" % __version__ <EOL> return <NUM_LIT:0> <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> all = <NUM_LIT:1> <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> verbose = <NUM_LIT:1> <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> verbose = <NUM_LIT:0> <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if optarg : <EOL> altpath = optarg . split ( os . pathsep ) <EOL> else : <EOL> altpath = [ ] <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if optarg : <EOL> exts = optarg . split ( os . pathsep ) <EOL> else : <EOL> exts = [ ] <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> return - <NUM_LIT:1> <EOL> failures = <NUM_LIT:0> <EOL> for arg in args : <EOL> nmatches = <NUM_LIT:0> <EOL> for match in whichgen ( arg , path = altpath , verbose = verbose , exts = exts ) : <EOL> if verbose : <EOL> print "<STR_LIT>" % match <EOL> else : <EOL> print match <EOL> nmatches += <NUM_LIT:1> <EOL> if not all : <EOL> break <EOL> if not nmatches : <EOL> failures += <NUM_LIT:1> <EOL> return failures <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
