2018-10-31 16:08:29 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2019-09-18 15:30:46 +00:00
|
|
|
from __future__ import absolute_import
|
2018-10-31 16:08:29 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def find_executable(executable, path=None):
|
|
|
|
"""Find if 'executable' can be run. Looks for it in 'path'
|
|
|
|
(string that lists directories separated by 'os.pathsep';
|
|
|
|
defaults to os.environ['PATH']). Checks for all executable
|
|
|
|
extensions. Returns full path or None if no command is found.
|
|
|
|
"""
|
|
|
|
if path is None:
|
|
|
|
path = os.environ['PATH']
|
|
|
|
paths = path.split(os.pathsep)
|
|
|
|
extlist = ['']
|
|
|
|
if os.name == 'os2':
|
|
|
|
(base, ext) = os.path.splitext(executable)
|
|
|
|
# executable files on OS/2 can have an arbitrary extension, but
|
|
|
|
# .exe is automatically appended if no dot is present in the name
|
|
|
|
if not ext:
|
|
|
|
executable = executable + ".exe"
|
|
|
|
elif sys.platform == 'win32':
|
|
|
|
pathext = os.environ['PATHEXT'].lower().split(os.pathsep)
|
|
|
|
(base, ext) = os.path.splitext(executable)
|
|
|
|
if ext.lower() not in pathext:
|
|
|
|
extlist = pathext
|
|
|
|
for ext in extlist:
|
|
|
|
execname = executable + ext
|
|
|
|
if os.path.isfile(execname):
|
|
|
|
return execname
|
|
|
|
else:
|
|
|
|
for p in paths:
|
|
|
|
f = os.path.join(p, execname)
|
|
|
|
if os.path.isfile(f):
|
|
|
|
return f
|
|
|
|
else:
|
|
|
|
return None
|