-
-
Save kergoth/9796665eca00096fdf54096e11b989c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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