From 68f5dbac61c8ccb0c48d560e2208eac6305ab321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Billeter?= Date: Mon, 25 Feb 2019 11:01:49 +0100 Subject: cascache.py: Make _checkout() public This allows CasBasedDirectory.export_files() to use CASCache.checkout(), eliminating code duplication. --- buildstream/_cas/cascache.py | 66 +++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/buildstream/_cas/cascache.py b/buildstream/_cas/cascache.py index fe25efce6..3eef94803 100644 --- a/buildstream/_cas/cascache.py +++ b/buildstream/_cas/cascache.py @@ -170,7 +170,7 @@ class CASCache(): with utils._tempdir(prefix='tmp', dir=self.tmpdir) as tmpdir: checkoutdir = os.path.join(tmpdir, ref) - self._checkout(checkoutdir, tree) + self.checkout(checkoutdir, tree) try: utils.move_atomic(checkoutdir, dest) @@ -182,6 +182,42 @@ class CASCache(): return originaldest + # checkout(): + # + # Checkout the specified directory digest. + # + # Args: + # dest (str): The destination path + # tree (Digest): The directory digest to extract + # + def checkout(self, dest, tree): + os.makedirs(dest, exist_ok=True) + + directory = remote_execution_pb2.Directory() + + with open(self.objpath(tree), 'rb') as f: + directory.ParseFromString(f.read()) + + for filenode in directory.files: + # regular file, create hardlink + fullpath = os.path.join(dest, filenode.name) + os.link(self.objpath(filenode.digest), fullpath) + + if filenode.is_executable: + os.chmod(fullpath, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | + stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH) + + for dirnode in directory.directories: + # Don't try to checkout a dangling ref + if os.path.exists(self.objpath(dirnode.digest)): + fullpath = os.path.join(dest, dirnode.name) + self.checkout(fullpath, dirnode.digest) + + for symlinknode in directory.symlinks: + # symlink + fullpath = os.path.join(dest, symlinknode.name) + os.symlink(symlinknode.target, fullpath) + # commit(): # # Commit directory to cache. @@ -631,34 +667,6 @@ class CASCache(): # Local Private Methods # ################################################ - def _checkout(self, dest, tree): - os.makedirs(dest, exist_ok=True) - - directory = remote_execution_pb2.Directory() - - with open(self.objpath(tree), 'rb') as f: - directory.ParseFromString(f.read()) - - for filenode in directory.files: - # regular file, create hardlink - fullpath = os.path.join(dest, filenode.name) - os.link(self.objpath(filenode.digest), fullpath) - - if filenode.is_executable: - os.chmod(fullpath, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | - stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH) - - for dirnode in directory.directories: - # Don't try to checkout a dangling ref - if os.path.exists(self.objpath(dirnode.digest)): - fullpath = os.path.join(dest, dirnode.name) - self._checkout(fullpath, dirnode.digest) - - for symlinknode in directory.symlinks: - # symlink - fullpath = os.path.join(dest, symlinknode.name) - os.symlink(symlinknode.target, fullpath) - def _refpath(self, ref): return os.path.join(self.casdir, 'refs', 'heads', ref) -- cgit v1.2.1