Skip to content

Commit

Permalink
Avoid calling os.stat for performance. #3 cpburnz/python-pathspec#38
Browse files Browse the repository at this point in the history
  • Loading branch information
excitoon committed Aug 28, 2022
1 parent 14ed1b0 commit 9daa882
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions 3
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,27 @@ def filter_path(path, prefix):
if is_link:
link = os.readlink(sub_path)
link_mode = get_mode(os.path.join(path, link))
is_dir = link_mode and stat.S_IFMT(link_mode) == stat.S_IFDIR
link_suffix = f" -> {apply_color(link, link_mode, False)}"
if link_mode is None:
mode = None

else:
link_suffix = ""
is_dir = mode and stat.S_IFMT(mode) == stat.S_IFDIR

if name != ".git" and not gitignores(sub_path):
yield sub_path, is_link, name, link_suffix, mode
if name != ".git" and not gitignores(sub_path, is_dir=is_dir):
yield sub_path, is_link, is_dir, name, link_suffix, mode


def process_path(path, prefix=""):
for (sub_path, is_link, name, link_suffix, mode), next in pairwise(
for (sub_path, is_link, is_dir, name, link_suffix, mode), next in pairwise(
itertools.chain(filter_path(path, prefix), [None])
):
sticks = "├──" if next else "└──"
print(f"{prefix}{sticks}", f"{apply_color(name, mode, is_link)}{link_suffix}")

if not is_link and os.path.isdir(sub_path):
if not is_link and is_dir:
sticks = "│ " if next else " "
process_path(sub_path, prefix=f"{prefix}{sticks}")
statistics["directories"] += 1
Expand Down

0 comments on commit 9daa882

Please sign in to comment.