metadata: use os.walk to speedup metadata file discovery

os.walk is more efficient than the current handwritten traversal.

Measured the time to scan reduced from 30s+ to 8s on p920 on
chromium/src.

`followlinks=True` is set to preserve behavior that os.path.isdir
returns True for symlink to directories, and the current traversal code
will descend into those.

Change-Id: I941eec9105a46d6538ca484fbb5249a75888e38a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5379945
Reviewed-by: Anne Redulla <aredulla@google.com>
Commit-Queue: Jiewei Qian <qjw@chromium.org>
changes/45/5379945/2
Jiewei Qian 12 months ago committed by LUCI CQ
parent 3155d7d074
commit 13d20527ff

@ -28,11 +28,11 @@ def find_metadata_files(root: str) -> List[str]:
the root directory.
"""
metadata_files = []
for item in os.listdir(root):
full_path = os.path.join(root, item)
if is_metadata_file(item):
metadata_files.append(full_path)
elif os.path.isdir(full_path):
metadata_files.extend(find_metadata_files(full_path))
for (dirpath, _, filenames) in os.walk(root, followlinks=True):
for filename in filenames:
if is_metadata_file(filename):
full_path = os.path.join(root, dirpath, filename)
metadata_files.append(full_path)
return metadata_files

Loading…
Cancel
Save