diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2019-01-17 14:53:05 -0500 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2019-01-18 10:59:28 -0500 |
commit | 42a2fe3c2e22885ddc6a7f43f4a735100e886c99 (patch) | |
tree | 8445614b9f1ceb1c85927dee708010f0339ba800 | |
parent | f874295f0bba8004a565afff4fa3024f2db0a5f5 (diff) | |
download | buildstream-42a2fe3c2e22885ddc6a7f43f4a735100e886c99.tar.gz |
bzr source plugins: Remove all of the atomic backup directory business logic
Follow up of last commit which uses exclusive locking to
protect bzr operations instead.
-rw-r--r-- | buildstream/plugins/sources/bzr.py | 92 |
1 files changed, 18 insertions, 74 deletions
diff --git a/buildstream/plugins/sources/bzr.py b/buildstream/plugins/sources/bzr.py index 39cee2bf9..5b993c043 100644 --- a/buildstream/plugins/sources/bzr.py +++ b/buildstream/plugins/sources/bzr.py @@ -180,83 +180,27 @@ class BzrSource(Source): return os.path.join(self.get_mirror_directory(), utils.url_directory_name(self.original_url)) - def _atomic_replace_mirrordir(self, srcdir): - """Helper function to safely replace the mirror dir""" + def _ensure_mirror(self, skip_ref_check=False): + mirror_dir = self._get_mirror_dir() + bzr_metadata_dir = os.path.join(mirror_dir, ".bzr") + if not os.path.exists(bzr_metadata_dir): + self.call([self.host_bzr, "init-repo", "--no-trees", mirror_dir], + fail="Failed to initialize bzr repository") + + branch_dir = os.path.join(mirror_dir, self.tracking) + branch_url = self.url + "/" + self.tracking + if not os.path.exists(branch_dir): + # `bzr branch` the branch if it doesn't exist + # to get the upstream code + self.call([self.host_bzr, "branch", branch_url, branch_dir], + fail="Failed to branch from {} to {}".format(branch_url, branch_dir)) - if not os.path.exists(self._get_mirror_dir()): - # Just move the srcdir to the mirror dir - try: - os.rename(srcdir, self._get_mirror_dir()) - except OSError as e: - raise SourceError("{}: Failed to move srcdir '{}' to mirror dir '{}'" - .format(str(self), srcdir, self._get_mirror_dir())) from e else: - # Atomically swap the backup dir. - backupdir = self._get_mirror_dir() + ".bak" - try: - os.rename(self._get_mirror_dir(), backupdir) - except OSError as e: - raise SourceError("{}: Failed to move mirrordir '{}' to backup dir '{}'" - .format(str(self), self._get_mirror_dir(), backupdir)) from e - - try: - os.rename(srcdir, self._get_mirror_dir()) - except OSError as e: - # Attempt to put the backup back! - os.rename(backupdir, self._get_mirror_dir()) - raise SourceError("{}: Failed to replace bzr repo '{}' with '{}" - .format(str(self), srcdir, self._get_mirror_dir())) from e - finally: - if os.path.exists(backupdir): - shutil.rmtree(backupdir) + # `bzr pull` the branch if it does exist + # to get any changes to the upstream code + self.call([self.host_bzr, "pull", "--directory={}".format(branch_dir), branch_url], + fail="Failed to pull new changes for {}".format(branch_dir)) - @contextmanager - def _atomic_repodir(self): - """Context manager for working in a copy of the bzr repository - - Yields: - (str): A path to the copy of the bzr repo - - This should be used because bzr does not give any guarantees of - atomicity, and aborting an operation at the wrong time (or - accidentally running multiple concurrent operations) can leave the - repo in an inconsistent state. - """ - with self.tempdir() as repodir: - mirror_dir = self._get_mirror_dir() - if os.path.exists(mirror_dir): - try: - # shutil.copytree doesn't like it if destination exists - shutil.rmtree(repodir) - shutil.copytree(mirror_dir, repodir) - except (shutil.Error, OSError) as e: - raise SourceError("{}: Failed to copy bzr repo from '{}' to '{}'" - .format(str(self), mirror_dir, repodir)) from e - - yield repodir - self._atomic_replace_mirrordir(repodir) - - def _ensure_mirror(self, skip_ref_check=False): - with self._atomic_repodir() as repodir: - # Initialize repo if no metadata - bzr_metadata_dir = os.path.join(repodir, ".bzr") - if not os.path.exists(bzr_metadata_dir): - self.call([self.host_bzr, "init-repo", "--no-trees", repodir], - fail="Failed to initialize bzr repository") - - branch_dir = os.path.join(repodir, self.tracking) - branch_url = self.url + "/" + self.tracking - if not os.path.exists(branch_dir): - # `bzr branch` the branch if it doesn't exist - # to get the upstream code - self.call([self.host_bzr, "branch", branch_url, branch_dir], - fail="Failed to branch from {} to {}".format(branch_url, branch_dir)) - - else: - # `bzr pull` the branch if it does exist - # to get any changes to the upstream code - self.call([self.host_bzr, "pull", "--directory={}".format(branch_dir), branch_url], - fail="Failed to pull new changes for {}".format(branch_dir)) if not skip_ref_check and not self._check_ref(): raise SourceError("Failed to ensure ref '{}' was mirrored".format(self.ref), reason="ref-not-mirrored") |