mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-06 09:20:57 +01:00
Rewrite of the executables_in
to also work on Windows where executables are defined by their extension.
This commit is contained in:
parent
82a03b0527
commit
15cfb752f1
1 changed files with 33 additions and 9 deletions
|
@ -317,18 +317,42 @@ class redirect_stderr(_RedirectStream):
|
||||||
_stream = "stderr"
|
_stream = "stderr"
|
||||||
|
|
||||||
|
|
||||||
def executables_in(path):
|
def _executables_in_posix(path):
|
||||||
"""Returns a generator of files in `path` that the user could execute. """
|
|
||||||
try:
|
|
||||||
if PYTHON_VERSION_INFO < (3, 5, 0):
|
if PYTHON_VERSION_INFO < (3, 5, 0):
|
||||||
for i in os.listdir(path):
|
for fname in os.listdir(path):
|
||||||
name = os.path.join(path, i)
|
fpath = os.path.join(path, fname)
|
||||||
if (os.path.exists(name) and os.access(name, os.X_OK) and \
|
if (os.path.exists(fpath) and os.access(fpath, os.X_OK) and \
|
||||||
(not os.path.isdir(name))):
|
(not os.path.isdir(fpath))):
|
||||||
yield i
|
yield fname
|
||||||
else:
|
else:
|
||||||
yield from (x.name for x in scandir(path)
|
yield from (x.name for x in scandir(path)
|
||||||
if x.is_file() and os.access(x.path, os.X_OK))
|
if x.is_file() and os.access(x.path, os.X_OK))
|
||||||
|
|
||||||
|
|
||||||
|
def _executables_in_windows(path):
|
||||||
|
extensions = builtins.__xonsh_env__.get('PATHEXT',['.COM', '.EXE', '.BAT'])
|
||||||
|
if PYTHON_VERSION_INFO < (3, 5, 0):
|
||||||
|
for fname in os.listdir(path):
|
||||||
|
fpath = os.path.join(path, fname)
|
||||||
|
if (os.path.exists(fpath) and not os.path.isdir(fpath)):
|
||||||
|
base_name, ext = os.path.splitext(fname)
|
||||||
|
if ext.upper() in extensions:
|
||||||
|
yield fname
|
||||||
|
else:
|
||||||
|
for fname in (x.name for x in scandir(path) if x.is_file()):
|
||||||
|
base_name, ext = os.path.splitext(fname)
|
||||||
|
if ext.upper() in extensions:
|
||||||
|
yield fname
|
||||||
|
|
||||||
|
|
||||||
|
def executables_in(path):
|
||||||
|
"""Returns a generator of files in `path` that the user could execute. """
|
||||||
|
if ON_WINDOWS:
|
||||||
|
func = _executables_in_windows
|
||||||
|
else:
|
||||||
|
func = _executables_in_posix
|
||||||
|
try:
|
||||||
|
yield from func(path)
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue