Return names of executables in a user's path. :param starts_with: what the exes should start with. leave blank for all exes in path. :return: a list of matching exe names
(starts_with: str)
| 361 | |
| 362 | |
| 363 | def get_exes_in_path(starts_with: str) -> list[str]: |
| 364 | """Return names of executables in a user's path. |
| 365 | |
| 366 | :param starts_with: what the exes should start with. leave blank for all exes in path. |
| 367 | :return: a list of matching exe names |
| 368 | """ |
| 369 | # Purposely don't match any executable containing wildcards |
| 370 | wildcards = ["*", "?"] |
| 371 | for wildcard in wildcards: |
| 372 | if wildcard in starts_with: |
| 373 | return [] |
| 374 | |
| 375 | # Get a list of every directory in the PATH environment variable and ignore symbolic links |
| 376 | env_path = os.getenv("PATH") |
| 377 | paths = [] if env_path is None else [p for p in env_path.split(os.path.pathsep) if not os.path.islink(p)] |
| 378 | |
| 379 | # Use a set to store exe names since there can be duplicates |
| 380 | exes_set = set() |
| 381 | |
| 382 | # Find every executable file in the user's path that matches the pattern |
| 383 | for path in paths: |
| 384 | full_path = os.path.join(path, starts_with) |
| 385 | matches = files_from_glob_pattern(full_path + "*", access=os.X_OK) |
| 386 | |
| 387 | for match in matches: |
| 388 | exes_set.add(os.path.basename(match)) |
| 389 | |
| 390 | return list(exes_set) |
| 391 | |
| 392 | |
| 393 | class StdSim: |
nothing calls this directly
no test coverage detected
searching dependent graphs…