summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2019-01-17 14:53:05 -0500
committerTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2019-01-17 14:54:46 -0500
commit6360b10d45bdb7510528e6cdb75d53ca585b7cf5 (patch)
treebcd868ac873c5748273cb7397a12a7da4b5d230d
parentcee4c60f43eeb0fd944844954d6ea4af520a2a3e (diff)
downloadbuildstream-6360b10d45bdb7510528e6cdb75d53ca585b7cf5.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.py92
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")