Debian Codegraph @

tests/testutils/repo/bzrrepo.py

raw
1 import os
2 import subprocess
3 import pytest
4
5 from buildstream._testing import Repo
6
7 from ..site import BZR, BZR_ENV, HAVE_BZR
8
9
10 class Bzr(Repo):
11 def __init__(self, directory, subdir="repo"):
12 if not HAVE_BZR:
13 pytest.skip("bzr is not available")
14 super().__init__(directory, subdir)
15 self.bzr = BZR
16
17 self.env = os.environ.copy()
18 self.env.update(BZR_ENV)
19
20 def create(self, directory):
21 # Work around race condition in bzr's creation of ~/.bazaar in
22 # ensure_config_dir_exists() when running tests in parallel.
23 bazaar_config_dir = os.path.expanduser("~/.bazaar")
24 os.makedirs(bazaar_config_dir, exist_ok=True)
25
26 branch_dir = os.path.join(self.repo, "trunk")
27
28 subprocess.call([self.bzr, "init-repo", self.repo], env=self.env)
29 subprocess.call([self.bzr, "init", branch_dir], env=self.env)
30 self.copy_directory(directory, branch_dir)
31 subprocess.call([self.bzr, "add", "."], env=self.env, cwd=branch_dir)
32 subprocess.call(
33 [self.bzr, "commit", '--message="Initial commit"'],
34 env=self.env,
35 cwd=branch_dir,
36 )
37
38 return self.latest_commit()
39
40 def source_config(self, ref=None):
41 config = {
42 "kind": "bzr",
43 "url": "file://" + self.repo,
44 "track": "trunk",
45 }
46 if ref is not None:
47 config["ref"] = ref
48
49 return config
50
51 def latest_commit(self):
52 return subprocess.check_output(
53 [
54 self.bzr,
55 "version-info",
56 "--custom",
57 "--template={revno}",
58 os.path.join(self.repo, "trunk"),
59 ],
60 env=self.env,
61 universal_newlines=True,
62 ).strip()