Tries to find 'executable' in the directories listed in 'path'. A string listing directories separated by 'os.pathsep'; defaults to os.environ['PATH']. Returns the complete filename or None if not found.
(executable, path=None)
| 93 | |
| 94 | |
| 95 | def find_executable(executable, path=None): |
| 96 | """Tries to find 'executable' in the directories listed in 'path'. |
| 97 | |
| 98 | A string listing directories separated by 'os.pathsep'; defaults to |
| 99 | os.environ['PATH']. Returns the complete filename or None if not found. |
| 100 | """ |
| 101 | _, ext = os.path.splitext(executable) |
| 102 | if (sys.platform == 'win32') and (ext != '.exe'): |
| 103 | executable = executable + '.exe' |
| 104 | |
| 105 | if os.path.isfile(executable): |
| 106 | return executable |
| 107 | |
| 108 | if path is None: |
| 109 | path = os.environ.get('PATH', None) |
| 110 | if path is None: |
| 111 | try: |
| 112 | path = os.confstr("CS_PATH") |
| 113 | except (AttributeError, ValueError): |
| 114 | # os.confstr() or CS_PATH is not available |
| 115 | path = os.defpath |
| 116 | # bpo-35755: Don't use os.defpath if the PATH environment variable is |
| 117 | # set to an empty string |
| 118 | |
| 119 | # PATH='' doesn't match, whereas PATH=':' looks in the current directory |
| 120 | if not path: |
| 121 | return None |
| 122 | |
| 123 | paths = path.split(os.pathsep) |
| 124 | for p in paths: |
| 125 | f = os.path.join(p, executable) |
| 126 | if os.path.isfile(f): |
| 127 | # the file exists, we have a shot at spawn working |
| 128 | return f |
| 129 | return None |