| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
# you may not use this file except in compliance with the License. |
| 4 |
# You may obtain a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
# See the License for the specific language governing permissions and |
| 12 |
# limitations under the License. |
| 13 |
# |
| 14 |
# Authors: |
| 15 |
# Jonathan Maw <[email protected]> |
| 16 |
|
| 17 |
# |
| 18 |
# This plugin was originally developped in the https://github.com/apache/buildstream/ |
| 19 |
# repository and was copied from 1a3c707a6c46573ab159de64ac9cd92e7f6027e6 |
| 20 |
# |
| 21 |
|
| 22 |
""" |
| 23 |
bzr - stage files from a bazaar repository |
| 24 |
========================================== |
| 25 |
|
| 26 |
**Host dependencies:** |
| 27 |
|
| 28 |
* bzr |
| 29 |
|
| 30 |
**Usage:** |
| 31 |
|
| 32 |
.. code:: yaml |
| 33 |
|
| 34 |
# Specify the bzr source kind |
| 35 |
kind: bzr |
| 36 |
|
| 37 |
# Specify the bzr url. Bazaar URLs come in many forms, see |
| 38 |
# `bzr help urlspec` for more information. Using an alias defined |
| 39 |
# in your project configuration is encouraged. |
| 40 |
url: https://launchpad.net/bzr |
| 41 |
|
| 42 |
# Specify the tracking branch. This is mandatory, as bzr cannot identify |
| 43 |
# an individual revision outside its branch. bzr URLs that omit the branch |
| 44 |
# name implicitly specify the trunk branch, but bst requires this to be |
| 45 |
# explicit. |
| 46 |
track: trunk |
| 47 |
|
| 48 |
# Specify the ref. This is a revision number. This is usually a decimal, |
| 49 |
# but revisions on a branch are of the form |
| 50 |
# <revision-branched-from>.<branch-number>.<revision-since-branching> |
| 51 |
# e.g. 6622.1.6. |
| 52 |
# The ref must be specified to build, and 'bst source track' will update the |
| 53 |
# revision number to the one on the tip of the branch specified in 'track'. |
| 54 |
ref: 6622 |
| 55 |
|
| 56 |
# Specify the version to be reported as the *guess_version* when reporting |
| 57 |
# SourceInfo |
| 58 |
# |
| 59 |
# Since 2.5 |
| 60 |
# |
| 61 |
version: 1.2 |
| 62 |
|
| 63 |
See `built-in functionality doumentation |
| 64 |
<https://docs.buildstream.build/master/buildstream.source.html#core-source-builtins>`_ for |
| 65 |
details on common configuration options for sources. |
| 66 |
|
| 67 |
|
| 68 |
Reporting `SourceInfo <https://docs.buildstream.build/master/buildstream.source.html#buildstream.source.SourceInfo>`_ |
| 69 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 70 |
The bzr source reports the URL of the bzr repository as the *url*. |
| 71 |
|
| 72 |
Further, the bzr source reports the `SourceInfoMedium.BZR |
| 73 |
<https://docs.buildstream.build/master/buildstream.source.html#buildstream.source.SourceInfoMedium.BZR>`_ |
| 74 |
*medium* and the` `SourceVersionType.COMMIT |
| 75 |
<https://docs.buildstream.build/master/buildstream.source.html#buildstream.source.SourceVersionType.COMMIT>`_ |
| 76 |
*version_type*, for which it reports the bzr revision number as the *version*. |
| 77 |
|
| 78 |
Since the bzr source does not have a way to know what the release version |
| 79 |
corresponds to the revision number, the bzr source exposes the ``version`` configuration |
| 80 |
attribute to allow explicit specification of the *guess_version*. |
| 81 |
""" |
| 82 |
|
| 83 |
import os |
| 84 |
import shutil |
| 85 |
import fcntl |
| 86 |
from contextlib import contextmanager |
| 87 |
|
| 88 |
from buildstream import Source, SourceError |
| 89 |
from buildstream import utils |
| 90 |
|
| 91 |
# |
| 92 |
# Soft import of buildstream symbols only available in newer versions |
| 93 |
# |
| 94 |
# The BST_MIN_VERSION will provide a better user experience. |
| 95 |
# |
| 96 |
try: |
| 97 |
from buildstream import SourceInfoMedium, SourceVersionType |
| 98 |
except ImportError: |
| 99 |
pass |
| 100 |
|
| 101 |
|
| 102 |
class BzrSource(Source): |
| 103 |
# pylint: disable=attribute-defined-outside-init |
| 104 |
|
| 105 |
BST_MIN_VERSION = "2.5" |
| 106 |
|
| 107 |
def configure(self, node): |
| 108 |
node.validate_keys(["url", "track", "ref", "version", *Source.COMMON_CONFIG_KEYS]) |
| 109 |
|
| 110 |
self.original_url = node.get_str("url") |
| 111 |
self.tracking = node.get_str("track") |
| 112 |
self.ref = node.get_str("ref", None) |
| 113 |
self.url = self.translate_url(self.original_url) |
| 114 |
self.version = node.get_str("version", None) |
| 115 |
|
| 116 |
def preflight(self): |
| 117 |
# Check if bzr is installed, get the binary at the same time. |
| 118 |
self.host_bzr = utils.get_host_tool("bzr") |
| 119 |
|
| 120 |
def get_unique_key(self): |
| 121 |
unique_key = [self.original_url, self.tracking, self.ref] |
| 122 |
|
| 123 |
# Backwards compatible method of supporting configuration |
| 124 |
# attributes which affect SourceInfo generation. |
| 125 |
if self.version is not None: |
| 126 |
unique_key.append(self.version) |
| 127 |
|
| 128 |
return unique_key |
| 129 |
|
| 130 |
def is_cached(self): |
| 131 |
with self._locked(): |
| 132 |
return self._check_ref() |
| 133 |
|
| 134 |
def load_ref(self, node): |
| 135 |
self.ref = node.get_str("ref", None) |
| 136 |
|
| 137 |
def get_ref(self): |
| 138 |
return self.ref |
| 139 |
|
| 140 |
def set_ref(self, ref, node): |
| 141 |
node["ref"] = self.ref = ref |
| 142 |
|
| 143 |
def track(self): # pylint: disable=arguments-differ |
| 144 |
with self.timed_activity("Tracking {}".format(self.url), silent_nested=True), self._locked(): |
| 145 |
self._ensure_mirror(skip_ref_check=True) |
| 146 |
ret, out = self.check_output( |
| 147 |
[ |
| 148 |
self.host_bzr, |
| 149 |
"version-info", |
| 150 |
"--custom", |
| 151 |
"--template={revno}", |
| 152 |
self._get_branch_dir(), |
| 153 |
], |
| 154 |
fail="Failed to read the revision number at '{}'".format(self._get_branch_dir()), |
| 155 |
) |
| 156 |
if ret != 0: |
| 157 |
raise SourceError("{}: Failed to get ref for tracking {}".format(self, self.tracking)) |
| 158 |
|
| 159 |
return out |
| 160 |
|
| 161 |
def fetch(self): # pylint: disable=arguments-differ |
| 162 |
with self.timed_activity("Fetching {}".format(self.url), silent_nested=True), self._locked(): |
| 163 |
self._ensure_mirror() |
| 164 |
|
| 165 |
def stage(self, directory): |
| 166 |
self.call( |
| 167 |
[ |
| 168 |
self.host_bzr, |
| 169 |
"checkout", |
| 170 |
"--lightweight", |
| 171 |
"--revision=revno:{}".format(self.ref), |
| 172 |
self._get_branch_dir(), |
| 173 |
directory, |
| 174 |
], |
| 175 |
fail="Failed to checkout revision {} from branch {} to {}".format( |
| 176 |
self.ref, self._get_branch_dir(), directory |
| 177 |
), |
| 178 |
) |
| 179 |
# Remove .bzr dir |
| 180 |
shutil.rmtree(os.path.join(directory, ".bzr")) |
| 181 |
|
| 182 |
def init_workspace(self, directory): |
| 183 |
url = os.path.join(self.url, self.tracking) |
| 184 |
with self.timed_activity('Setting up workspace "{}"'.format(directory), silent_nested=True): |
| 185 |
# Checkout from the cache |
| 186 |
self.call( |
| 187 |
[ |
| 188 |
self.host_bzr, |
| 189 |
"branch", |
| 190 |
"--use-existing-dir", |
| 191 |
"--revision=revno:{}".format(self.ref), |
| 192 |
self._get_branch_dir(), |
| 193 |
directory, |
| 194 |
], |
| 195 |
fail="Failed to branch revision {} from branch {} to {}".format( |
| 196 |
self.ref, self._get_branch_dir(), directory |
| 197 |
), |
| 198 |
) |
| 199 |
# Switch the parent branch to the source's origin |
| 200 |
self.call( |
| 201 |
[ |
| 202 |
self.host_bzr, |
| 203 |
"switch", |
| 204 |
"--directory={}".format(directory), |
| 205 |
url, |
| 206 |
], |
| 207 |
fail="Failed to switch workspace's parent branch to {}".format(url), |
| 208 |
) |
| 209 |
|
| 210 |
def collect_source_info(self): |
| 211 |
return [ |
| 212 |
self.create_source_info( |
| 213 |
self.url, SourceInfoMedium.BAZAAR, SourceVersionType.COMMIT, self.ref, version_guess=self.version |
| 214 |
) |
| 215 |
] |
| 216 |
|
| 217 |
# _locked() |
| 218 |
# |
| 219 |
# This context manager ensures exclusive access to the |
| 220 |
# bzr repository. |
| 221 |
# |
| 222 |
@contextmanager |
| 223 |
def _locked(self): |
| 224 |
lockdir = os.path.join(self.get_mirror_directory(), "locks") |
| 225 |
lockfile = os.path.join(lockdir, utils.url_directory_name(self.original_url) + ".lock") |
| 226 |
os.makedirs(lockdir, exist_ok=True) |
| 227 |
with open(lockfile, "wb") as lock: |
| 228 |
fcntl.flock(lock, fcntl.LOCK_EX) |
| 229 |
try: |
| 230 |
yield |
| 231 |
finally: |
| 232 |
fcntl.flock(lock, fcntl.LOCK_UN) |
| 233 |
|
| 234 |
def _check_ref(self): |
| 235 |
# If the mirror doesnt exist yet, then we dont have the ref |
| 236 |
if not os.path.exists(self._get_branch_dir()): |
| 237 |
return False |
| 238 |
|
| 239 |
return ( |
| 240 |
self.call( |
| 241 |
[ |
| 242 |
self.host_bzr, |
| 243 |
"revno", |
| 244 |
"--revision=revno:{}".format(self.ref), |
| 245 |
self._get_branch_dir(), |
| 246 |
] |
| 247 |
) |
| 248 |
== 0 |
| 249 |
) |
| 250 |
|
| 251 |
def _get_branch_dir(self): |
| 252 |
return os.path.join(self._get_mirror_dir(), self.tracking) |
| 253 |
|
| 254 |
def _get_mirror_dir(self): |
| 255 |
return os.path.join( |
| 256 |
self.get_mirror_directory(), |
| 257 |
utils.url_directory_name(self.original_url), |
| 258 |
) |
| 259 |
|
| 260 |
def _ensure_mirror(self, skip_ref_check=False): |
| 261 |
mirror_dir = self._get_mirror_dir() |
| 262 |
bzr_metadata_dir = os.path.join(mirror_dir, ".bzr") |
| 263 |
if not os.path.exists(bzr_metadata_dir): |
| 264 |
self.call( |
| 265 |
[self.host_bzr, "init-repo", "--no-trees", mirror_dir], |
| 266 |
fail="Failed to initialize bzr repository", |
| 267 |
) |
| 268 |
|
| 269 |
branch_dir = os.path.join(mirror_dir, self.tracking) |
| 270 |
branch_url = self.url + "/" + self.tracking |
| 271 |
if not os.path.exists(branch_dir): |
| 272 |
# `bzr branch` the branch if it doesn't exist |
| 273 |
# to get the upstream code |
| 274 |
self.call( |
| 275 |
[self.host_bzr, "branch", branch_url, branch_dir], |
| 276 |
fail="Failed to branch from {} to {}".format(branch_url, branch_dir), |
| 277 |
) |
| 278 |
|
| 279 |
else: |
| 280 |
# `bzr pull` the branch if it does exist |
| 281 |
# to get any changes to the upstream code |
| 282 |
self.call( |
| 283 |
[ |
| 284 |
self.host_bzr, |
| 285 |
"pull", |
| 286 |
"--directory={}".format(branch_dir), |
| 287 |
branch_url, |
| 288 |
], |
| 289 |
fail="Failed to pull new changes for {}".format(branch_dir), |
| 290 |
) |
| 291 |
|
| 292 |
if not skip_ref_check and not self._check_ref(): |
| 293 |
raise SourceError( |
| 294 |
"Failed to ensure ref '{}' was mirrored".format(self.ref), |
| 295 |
reason="ref-not-mirrored", |
| 296 |
) |
| 297 |
|
| 298 |
|
| 299 |
def setup(): |
| 300 |
return BzrSource |