diff options
author | James Ennis <james.ennis@codethink.com> | 2019-01-24 14:09:05 +0000 |
---|---|---|
committer | James Ennis <james.ennis@codethink.com> | 2019-02-13 09:35:45 +0000 |
commit | 491937d7aad3f6a35779327d06135537c7622e0e (patch) | |
tree | 0b0155430671858de9baab22d73863171d7211b3 | |
parent | f95d6ee8970e64487cdfece39e00ad402774617a (diff) | |
download | buildstream-491937d7aad3f6a35779327d06135537c7622e0e.tar.gz |
cascache.py: Allow CASCache.list_refs() to handle globs
This commit ensures that CASCache.list_refs(), and
ArtifactCache.list_artifacts(), can both handle glob expressions.
-rw-r--r-- | buildstream/_artifactcache.py | 7 | ||||
-rw-r--r-- | buildstream/_cas/cascache.py | 24 |
2 files changed, 24 insertions, 7 deletions
diff --git a/buildstream/_artifactcache.py b/buildstream/_artifactcache.py index b3ca01618..bc0032bec 100644 --- a/buildstream/_artifactcache.py +++ b/buildstream/_artifactcache.py @@ -514,11 +514,14 @@ class ArtifactCache(): # # List artifacts in this cache in LRU order. # + # Args: + # glob (str): An option glob expression to be used to list artifacts satisfying the glob + # # Returns: # ([str]) - A list of artifact names as generated in LRU order # - def list_artifacts(self): - return self.cas.list_refs() + def list_artifacts(self, *, glob=None): + return self.cas.list_refs(glob=glob) # remove(): # diff --git a/buildstream/_cas/cascache.py b/buildstream/_cas/cascache.py index 560587055..792bf3eb9 100644 --- a/buildstream/_cas/cascache.py +++ b/buildstream/_cas/cascache.py @@ -24,6 +24,7 @@ import stat import errno import uuid import contextlib +from fnmatch import fnmatch import grpc @@ -472,22 +473,35 @@ class CASCache(): # # List refs in Least Recently Modified (LRM) order. # + # Args: + # glob (str) - An optional glob expression to be used to list refs satisfying the glob + # # Returns: # (list) - A list of refs in LRM order # - def list_refs(self): + 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(ref_heads): + for root, _, files in os.walk(path): for filename in files: ref_path = os.path.join(root, filename) - refs.append(os.path.relpath(ref_path, ref_heads)) - # Obtain the mtime (the time a file was last modified) - mtimes.append(os.path.getmtime(ref_path)) + 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. |