Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve efficiency of whisper finder #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions graphite_api/finders/whisper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

import gzip
import operator
import os.path
import time

Expand Down Expand Up @@ -77,25 +78,31 @@ def _find_paths(self, current_dir, patterns):
pattern = patterns[0]
patterns = patterns[1:]
has_wildcard = is_pattern(pattern)
using_globstar = pattern == "**"

matching_subdirs = []
files = []
# This avoids os.listdir() for performance
if has_wildcard:
entries = [x.name for x in scandir(current_dir)]
subdirs = []
for x in scandir(current_dir):
if x.is_file():
files.append(x.name)
if x.is_dir():
subdirs.append(x.name)

if pattern == "**":
matching_subdirs = map(operator.itemgetter(0), walk(current_dir))

# if this is a terminal globstar, add a pattern for all files in subdirs
if not patterns:
patterns = ["*"]
else:
matching_subdirs = match_entries(subdirs, pattern)
elif os.path.isdir(os.path.join(current_dir, pattern)):
matching_subdirs.append(pattern)
else:
entries = [pattern]

if using_globstar:
matching_subdirs = map(lambda x: x[0], walk(current_dir))
else:
subdirs = [e for e in entries
if os.path.isdir(os.path.join(current_dir, e))]
matching_subdirs = match_entries(subdirs, pattern)

# For terminal globstar, add a pattern for all files in subdirs
if using_globstar and not patterns:
patterns = ['*']

if patterns: # we've still got more directories to traverse
for subdir in matching_subdirs:
absolute_path = os.path.join(current_dir, subdir)
Expand All @@ -105,9 +112,9 @@ def _find_paths(self, current_dir, patterns):
else: # we've got the last pattern
if not has_wildcard:
entries = [pattern + '.wsp', pattern + '.wsp.gz']
files = [e for e in entries
if os.path.isfile(os.path.join(current_dir, e))]
matching_files = match_entries(files, pattern + '.*')
matching_files = [entry for entry in entries if os.path.isfile(os.path.join(current_dir, entry))]
else:
matching_files = match_entries(files, pattern + '.*')

for _basename in matching_files + matching_subdirs:
yield os.path.join(current_dir, _basename)
Expand Down