summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaoul Hidalgo Charman <raoul.hidalgocharman@codethink.co.uk>2019-04-01 14:04:20 +0100
committerTom Pollard <tom.pollard@codethink.co.uk>2019-04-26 12:55:12 +0100
commit8046145bc6f21895a3e59c33348de79a69898f03 (patch)
treebc44e9c36cc3017bcd3373bc9dcd96c0cf919b57
parentc65ba16fb630b1f184b49aa1d720865133c7fef6 (diff)
downloadbuildstream-8046145bc6f21895a3e59c33348de79a69898f03.tar.gz
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
-rw-r--r--buildstream/_cas/cascache.py28
-rw-r--r--buildstream/utils.py30
2 files changed, 32 insertions, 26 deletions
diff --git a/buildstream/_cas/cascache.py b/buildstream/_cas/cascache.py
index 5f67dc0c1..d7ab869b7 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 ade593750..7d6db0b67 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)