From b232b230c3a50e5b4ebfc3425c9040a7d933b0ef Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 2 Jun 2016 12:57:13 -0400 Subject: [PATCH] Add Archlinux support This commit adds archlinux support to bindep. It uses the pacman package manager's query interface to determine if the packages are installed. Change-Id: Idf69042b9072a2943f14ae9469cd42a252cc6b82 --- bindep/depends.py | 24 +++++++++++++++++++++++ bindep/tests/test_depends.py | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/bindep/depends.py b/bindep/depends.py index aefc946..1702fad 100644 --- a/bindep/depends.py +++ b/bindep/depends.py @@ -205,6 +205,9 @@ class Depends(object): elif distro in ["gentoo"]: atoms.add("emerge") self.platform = Emerge() + elif distro in ["arch"]: + atoms.add("pacman") + self.platform = Pacman() return ["platform:%s" % (atom,) for atom in sorted(atoms)] @@ -294,6 +297,27 @@ class Emerge(Platform): return elements[0] +class Pacman(Platform): + """pacman specific implementation. + + This shells out to pacman + """ + + def get_pkg_version(self, pkg_name): + try: + output = subprocess.check_output( + ['pacman', '-Q', pkg_name], + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + if e.returncode == 1 and e.output.endswith('was not found'): + return None + raise + # output looks like + # version + elements = output.strip().split(' ') + return elements[1] + + def _eval_diff(operator, diff): """Return the boolean result for operator given diff. diff --git a/bindep/tests/test_depends.py b/bindep/tests/test_depends.py index 21f56cc..bf21e73 100644 --- a/bindep/tests/test_depends.py +++ b/bindep/tests/test_depends.py @@ -31,6 +31,7 @@ from bindep.depends import _eval from bindep.depends import Depends from bindep.depends import Dpkg from bindep.depends import Emerge +from bindep.depends import Pacman from bindep.depends import Rpm @@ -145,6 +146,13 @@ class TestDepends(TestCase): depends.platform_profiles(), Contains("platform:dpkg")) self.assertIsInstance(depends.platform, Dpkg) + def test_arch_implies_pacman(self): + with self._mock_lsb("Arch"): + depends = Depends("") + self.assertThat( + depends.platform_profiles(), Contains("platform:pacman")) + self.assertIsInstance(depends.platform, Pacman) + def test_finds_profiles(self): depends = Depends(dedent("""\ foo @@ -411,6 +419,36 @@ class TestEmerge(TestCase): stderr=subprocess.STDOUT) +class TestPacman(TestCase): + + def test_unknown_package(self): + platform = Pacman() + + def _side_effect_raise(*args, **kwargs): + raise subprocess.CalledProcessError( + 1, [], "error: package 'foo' was not found") + + mock_checkoutput = self.useFixture( + fixtures.MockPatchObject(subprocess, "check_output")).mock + mock_checkoutput.side_effect = _side_effect_raise + + self.assertEqual(None, platform.get_pkg_version("foo")) + mock_checkoutput.assert_called_once_with( + ['pacman', '-Q', 'foo'], + stderr=subprocess.STDOUT) + self.assertEqual(None, platform.get_pkg_version("foo")) + + def test_installed_version(self): + platform = Pacman() + mock_checkoutput = self.useFixture( + fixtures.MockPatchObject(subprocess, "check_output")).mock + mock_checkoutput.return_value = 'foo 4.0.0-2' + self.assertEqual("4.0.0-2", platform.get_pkg_version("foo")) + mock_checkoutput.assert_called_once_with( + ['pacman', '-Q', 'foo'], + stderr=subprocess.STDOUT) + + class TestRpm(TestCase): # NOTE: test_not_installed is not implemented as rpm seems to only be aware