# (c) Copyright 2013. CodeWeavers, Inc.
import os
# Portable which(1) implementation
def which(path, app):
"""Looks for an executable in the specified directory list.
path is an os.pathsep-separated list of directories and app is the
executable name. If app contains a path separator then path is ignored.
If the file is not found, then None is returned.
"""
if os.path.isabs(app):
if os.path.isfile(app) and os.access(app, os.X_OK):
return app
elif os.sep in app or (os.altsep and os.altsep in app):
app_path = os.path.join(os.getcwd(), app)
if os.path.isfile(app_path) and os.access(app_path, os.X_OK):
return app_path
else:
for directory in path.split(os.pathsep):
if directory == "":
continue
app_path = os.path.join(directory, app)
if os.path.isfile(app_path) and os.access(app_path, os.X_OK):
return app_path
return None