diff options
author | Jürg Billeter <j@bitron.ch> | 2020-03-03 17:18:11 +0100 |
---|---|---|
committer | Jürg Billeter <j@bitron.ch> | 2020-03-10 15:46:04 +0000 |
commit | 7fbfccce44247122c5b9257e73328de16afa0445 (patch) | |
tree | 44e14a6c3e68e286905e1df825d5e856b58eac46 /src | |
parent | f78e6ee55e98c91e2cef055e232df5b955ac7323 (diff) | |
download | buildstream-7fbfccce44247122c5b9257e73328de16afa0445.tar.gz |
storage: Add public Directory.exists() method
Diffstat (limited to 'src')
-rw-r--r-- | src/buildstream/buildelement.py | 2 | ||||
-rw-r--r-- | src/buildstream/sandbox/_sandboxreapi.py | 2 | ||||
-rw-r--r-- | src/buildstream/sandbox/sandbox.py | 4 | ||||
-rw-r--r-- | src/buildstream/storage/_casbaseddirectory.py | 6 | ||||
-rw-r--r-- | src/buildstream/storage/_filebaseddirectory.py | 32 | ||||
-rw-r--r-- | src/buildstream/storage/directory.py | 12 |
6 files changed, 35 insertions, 23 deletions
diff --git a/src/buildstream/buildelement.py b/src/buildstream/buildelement.py index acb079a56..95b085eba 100644 --- a/src/buildstream/buildelement.py +++ b/src/buildstream/buildelement.py @@ -280,7 +280,7 @@ class BuildElement(Element): buildroot = self.get_variable("build-root") buildroot_vdir = vdir.descend(*buildroot.lstrip(os.sep).split(os.sep)) - if buildroot_vdir._exists(marker_filename): + if buildroot_vdir.exists(marker_filename): # Already prepared return diff --git a/src/buildstream/sandbox/_sandboxreapi.py b/src/buildstream/sandbox/_sandboxreapi.py index c2b6cac48..26fe5f2b2 100644 --- a/src/buildstream/sandbox/_sandboxreapi.py +++ b/src/buildstream/sandbox/_sandboxreapi.py @@ -76,7 +76,7 @@ class SandboxREAPI(Sandbox): # Ensure mount point exists in sandbox mount_point_components = mount_point.split(os.path.sep) - if not vdir._exists(*mount_point_components): + if not vdir.exists(*mount_point_components): if os.path.isdir(mount_source): # Mounting a directory, mount point must be a directory vdir.descend(*mount_point_components, create=True) diff --git a/src/buildstream/sandbox/sandbox.py b/src/buildstream/sandbox/sandbox.py index b82e2da59..6f6acc946 100644 --- a/src/buildstream/sandbox/sandbox.py +++ b/src/buildstream/sandbox/sandbox.py @@ -568,14 +568,14 @@ class Sandbox: vroot = self.get_virtual_directory() command_as_parts = command.lstrip(os.sep).split(os.sep) if os.path.isabs(command): - return vroot._exists(*command_as_parts, follow_symlinks=True) + return vroot.exists(*command_as_parts, follow_symlinks=True) if len(command_as_parts) > 1: return False for path in env.get("PATH").split(":"): path_as_parts = path.lstrip(os.sep).split(os.sep) - if vroot._exists(*path_as_parts, command, follow_symlinks=True): + if vroot.exists(*path_as_parts, command, follow_symlinks=True): return True return False diff --git a/src/buildstream/storage/_casbaseddirectory.py b/src/buildstream/storage/_casbaseddirectory.py index e4b267a63..ffcf085bb 100644 --- a/src/buildstream/storage/_casbaseddirectory.py +++ b/src/buildstream/storage/_casbaseddirectory.py @@ -795,7 +795,7 @@ class CasBasedDirectory(Directory): return self.__digest - def _exists(self, *path, follow_symlinks=False): + def exists(self, *path, follow_symlinks=False): try: subdir = self.descend(*path[:-1], follow_symlinks=follow_symlinks) target = subdir.index.get(path[-1]) @@ -804,8 +804,8 @@ class CasBasedDirectory(Directory): linklocation = target.target newpath = linklocation.split(os.path.sep) if os.path.isabs(linklocation): - return subdir._find_root()._exists(*newpath, follow_symlinks=True) - return subdir._exists(*newpath, follow_symlinks=True) + return subdir._find_root().exists(*newpath, follow_symlinks=True) + return subdir.exists(*newpath, follow_symlinks=True) else: return True return False diff --git a/src/buildstream/storage/_filebaseddirectory.py b/src/buildstream/storage/_filebaseddirectory.py index aafe33279..62fcc39c0 100644 --- a/src/buildstream/storage/_filebaseddirectory.py +++ b/src/buildstream/storage/_filebaseddirectory.py @@ -232,6 +232,22 @@ class FileBasedDirectory(Directory): def get_size(self): return utils._get_dir_size(self.external_directory) + def exists(self, *path, follow_symlinks=False): + try: + subdir = self.descend(*path[:-1], follow_symlinks=follow_symlinks) + newpath = os.path.join(subdir.external_directory, path[-1]) + st = os.lstat(newpath) + if follow_symlinks and stat.S_ISLNK(st.st_mode): + linklocation = os.readlink(newpath) + newpath = linklocation.split(os.path.sep) + if os.path.isabs(linklocation): + return subdir._find_root().exists(*newpath, follow_symlinks=True) + return subdir.exists(*newpath, follow_symlinks=True) + else: + return True + except (VirtualDirectoryError, FileNotFoundError): + return False + def __str__(self): # This returns the whole path (since we don't know where the directory started) # which exposes the sandbox directory; we will have to assume for the time being @@ -358,22 +374,6 @@ class FileBasedDirectory(Directory): os.symlink(entry.target, dest_path) result.files_written.append(relative_pathname) - def _exists(self, *path, follow_symlinks=False): - try: - subdir = self.descend(*path[:-1], follow_symlinks=follow_symlinks) - newpath = os.path.join(subdir.external_directory, path[-1]) - st = os.lstat(newpath) - if follow_symlinks and stat.S_ISLNK(st.st_mode): - linklocation = os.readlink(newpath) - newpath = linklocation.split(os.path.sep) - if os.path.isabs(linklocation): - return subdir._find_root()._exists(*newpath, follow_symlinks=True) - return subdir._exists(*newpath, follow_symlinks=True) - else: - return True - except (VirtualDirectoryError, FileNotFoundError): - return False - def _create_empty_file(self, name): with open(os.path.join(self.external_directory, name), "w"): pass diff --git a/src/buildstream/storage/directory.py b/src/buildstream/storage/directory.py index 55cc717f2..a9249baee 100644 --- a/src/buildstream/storage/directory.py +++ b/src/buildstream/storage/directory.py @@ -196,6 +196,18 @@ class Directory: and effective space used may be lower than this number due to deduplication. """ raise NotImplementedError() + def exists(self, *paths: str, follow_symlinks: bool = False) -> bool: + """ Check whether the specified path exists. + + Args: + *paths: A list of strings which are all path components. + follow_symlinks: True to follow symlinks. + + Returns: + True if the path exists, False otherwise. + """ + raise NotImplementedError() + # FileType: # |