Skip to content

Commit

Permalink
utils: custom which() that ignores binaries in directories like /home
Browse files Browse the repository at this point in the history
  • Loading branch information
seirl committed Mar 4, 2017
1 parent 7028b0c commit 7bbe6c6
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions camisole/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
import shutil
import os
import textwrap


Expand Down Expand Up @@ -48,4 +48,19 @@ def tabulate(rows, headers=None, fmt="", margin=1):


def which(binary):
return shutil.which(binary) or binary
search_prefixes = ['/usr', '/lib', '/bin']
path = [*os.environ.get('PATH').split(os.pathsep),
'/usr/bin',
'/usr/local/bin'
'/bin',
]
if os.path.dirname(binary) and os.access(binary, os.X_OK):
return binary
for part in path:
# Ignore matches that are not inside standard directories
if not any(part.startswith(prefix) for prefix in search_prefixes):
continue
p = os.path.join(part, binary)
if os.access(p, os.X_OK):
return p
return binary

0 comments on commit 7bbe6c6

Please sign in to comment.