def which(program):
def is_exe(fpath):<EOL><INDENT>return os.path.exists(fpath) and os.access(fpath, os.X_OK) and os.path.isfile(fpath)<EOL><DEDENT>def ext_candidates(fpath):<EOL><INDENT>yield fpath<EOL>for ext in os.environ.get("<STR_LIT>", "<STR_LIT>").split(os.pathsep):<EOL><INDENT>if ext: yield fpath + ext<EOL><DEDENT><DEDENT>fpath, _ = os.path.split(program)<EOL>if fpath and is_exe(program):<EOL><INDENT>return program<EOL><DEDENT>else:<EOL><INDENT>for path in os.environ["<STR_LIT>"].split(os.pathsep):<EOL><INDENT>exe_file = os.path.join(path, program)<EOL>for candidate in ext_candidates(exe_file):<EOL><INDENT>if is_exe(candidate):<EOL><INDENT>return candidate<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return None<EOL>
Find given program on system PATH. Works for both *NIX and Windows