diff options
-rw-r--r-- | cmd2/cmd2.py | 8 | ||||
-rw-r--r-- | cmd2/utils.py | 13 |
2 files changed, 17 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 3b47ee9e..520ac33d 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1163,7 +1163,7 @@ class Cmd(cmd.Cmd): # Find every executable file in the user's path that matches the pattern for path in paths: full_path = os.path.join(path, starts_with) - matches = [f for f in glob.glob(full_path + '*') if os.path.isfile(f) and os.access(f, os.X_OK)] + matches = utils.files_from_glob_pattern(full_path + '*', access=os.X_OK) for match in matches: exes_set.add(os.path.basename(match)) @@ -3707,9 +3707,9 @@ class Cmd(cmd.Cmd): :return: list of transcript file paths with glob patterns expanded """ expanded_transcripts = [] - for fileset in transcript_paths: - for fname in glob.glob(fileset): - expanded_transcripts.append(fname) + for pattern in transcript_paths: + files = utils.files_from_glob_pattern(pattern, access=os.R_OK) + expanded_transcripts.extend(files) return expanded_transcripts def run_transcript_tests(self, transcript_paths: List[str]) -> None: diff --git a/cmd2/utils.py b/cmd2/utils.py index 54ad763d..0db61ca5 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -2,6 +2,7 @@ """Shared utility functions""" import collections +import glob import os import re import subprocess @@ -319,6 +320,18 @@ def find_editor() -> str: return editor +def files_from_glob_pattern(pattern: str, access=os.F_OK) -> List[str]: + """Return a list of file paths based on a glob pattern. + + Only files are returned, not directories, and optionally only files for which the user has a specified access to. + + :param pattern: file name or glob pattern + :param access: file access type to verify (os.* where * is F_OK, R_OK, W_OK, or X_OK) + :return: list of files matching the name or glob pattern + """ + return [f for f in glob.glob(pattern) if os.path.isfile(f) and os.access(f, access)] + + class StdSim(object): """ Class to simulate behavior of sys.stdout or sys.stderr. |