Skip to content

Instantly share code, notes, and snippets.

@kergoth
Last active March 16, 2018 17:15
Show Gist options
  • Save kergoth/9796665eca00096fdf54096e11b989c9 to your computer and use it in GitHub Desktop.
Save kergoth/9796665eca00096fdf54096e11b989c9 to your computer and use it in GitHub Desktop.
def which_wild(pathname, path=None, mode=os.F_OK, *, reverse=False, candidates=False):
import glob
paths = (path or os.environ.get('PATH', os.defpath)).split(':')
if reverse:
paths.reverse()
seen, files = set(), []
for index, element in enumerate(paths):
if not os.path.isabs(element):
element = os.path.abspath(element)
candidate = os.path.join(element, pathname)
globbed = glob.glob(candidate)
if globbed:
for found_path in sorted(globbed):
if not os.access(found_path, mode):
continue
rel = os.path.relpath(found_path, element)
if rel not in seen:
seen.add(rel)
if candidates:
files.append((found_path, [os.path.join(p, rel) for p in paths[:index+1]]))
else:
files.append(found_path)
return files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment