You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by no...@apache.org on 2020/12/29 12:25:12 UTC
[buildstream] 02/13: cascache: move list refs method to utils
This is an automated email from the ASF dual-hosted git repository.
not-in-ldap pushed a commit to branch tpollard/prototemp
in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 8046145bc6f21895a3e59c33348de79a69898f03
Author: Raoul Hidalgo Charman <ra...@codethink.co.uk>
AuthorDate: Mon Apr 1 14:04:20 2019 +0100
cascache: move list refs method to utils
This is more generic such that it can be used for listing files in other
paths.
Part #974
---
buildstream/_cas/cascache.py | 28 ++--------------------------
buildstream/utils.py | 30 ++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 26 deletions(-)
diff --git a/buildstream/_cas/cascache.py b/buildstream/_cas/cascache.py
index 5f67dc0..d7ab869 100644
--- a/buildstream/_cas/cascache.py
+++ b/buildstream/_cas/cascache.py
@@ -24,7 +24,6 @@ import stat
import errno
import uuid
import contextlib
-from fnmatch import fnmatch
import grpc
@@ -514,31 +513,8 @@ class CASCache():
#
def list_refs(self, *, glob=None):
# string of: /path/to/repo/refs/heads
- ref_heads = os.path.join(self.casdir, 'refs', 'heads')
- path = ref_heads
-
- if glob is not None:
- globdir = os.path.dirname(glob)
- if not any(c in "*?[" for c in globdir):
- # path prefix contains no globbing characters so
- # append the glob to optimise the os.walk()
- path = os.path.join(ref_heads, globdir)
-
- refs = []
- mtimes = []
-
- for root, _, files in os.walk(path):
- for filename in files:
- ref_path = os.path.join(root, filename)
- relative_path = os.path.relpath(ref_path, ref_heads) # Relative to refs head
- if not glob or fnmatch(relative_path, glob):
- refs.append(relative_path)
- # Obtain the mtime (the time a file was last modified)
- mtimes.append(os.path.getmtime(ref_path))
-
- # NOTE: Sorted will sort from earliest to latest, thus the
- # first ref of this list will be the file modified earliest.
- return [ref for _, ref in sorted(zip(mtimes, refs))]
+ return [ref for _, ref in sorted(list(utils._list_directory(
+ os.path.join(self.casdir, 'refs', 'heads'), glob_expr=glob)))]
# list_objects():
#
diff --git a/buildstream/utils.py b/buildstream/utils.py
index ade5937..7d6db0b 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -23,6 +23,7 @@ Utilities
import calendar
import errno
+from fnmatch import fnmatch
import hashlib
import os
import re
@@ -1291,3 +1292,32 @@ def _deterministic_umask():
yield
finally:
os.umask(old_umask)
+
+
+# _list_directory()
+#
+# List files in a directory, given a base path
+#
+# Args:
+# base_path (str): Base path to traverse over
+# glob_expr (str|None): Optional glob expression to match against files
+#
+# Returns:
+# (iter (mtime, filename)]): iterator of tuples of mtime and filenames
+#
+def _list_directory(base_path, *, glob_expr=None):
+ path = base_path
+ if glob_expr is not None:
+ globdir = os.path.dirname(glob_expr)
+ if not any(c in "*?[" for c in globdir):
+ # path prefix contains no globbing characters so
+ # append the glob to optimise the os.walk()
+ path = os.path.join(base_path, globdir)
+
+ for root, _, files in os.walk(path):
+ for filename in files:
+ ref_path = os.path.join(root, filename)
+ relative_path = os.path.relpath(ref_path, base_path) # Relative to refs head
+ if not glob_expr or fnmatch(relative_path, glob_expr):
+ # Obtain the mtime (the time a file was last modified)
+ yield (os.path.getmtime(ref_path), relative_path)