add detection of darwin (MacOS/OSX)

Change-Id: I02a7e78f4392811dfbf8389bfa8a9fa4fa1ba987
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
This commit is contained in:
Sorin Sbarnea 2018-02-20 19:38:27 +00:00
parent 752f1b56d7
commit f58e5af965
2 changed files with 47 additions and 0 deletions

View File

@ -19,6 +19,7 @@ from locale import getpreferredencoding
import logging
import os.path
from parsley import makeGrammar
import platform
import subprocess
import sys
@ -275,6 +276,13 @@ class Depends(object):
return sorted(profiles)
def platform_profiles(self):
if platform.system() == 'Darwin':
atoms = set(['darwin'])
# detect available macos package managers
if os.system('which brew >/dev/null') == 0:
atoms.add('brew')
self.platform = Brew()
return ["platform:%s" % (atom,) for atom in sorted(atoms)]
try:
output = subprocess.check_output(
["lsb_release", "-cirs"],
@ -336,6 +344,27 @@ class Platform(object):
raise NotImplementedError(self.get_pkg_version)
class Brew(Platform):
"""brew specific platform implementation."""
def get_pkg_version(self, pkg_name):
try:
output = subprocess.check_output(
['brew', 'list', '--versions',
pkg_name],
stderr=subprocess.STDOUT).decode(getpreferredencoding(False))
except subprocess.CalledProcessError as e:
if (e.returncode == 1):
return None
raise
# output looks like
# git 2.15.1_1 2.15.0
output = output.strip()
elements = output.split(' ')[1:]
# brew supports multiple versions, we will only return the first one
return elements[0]
class Dpkg(Platform):
"""dpkg specific platform implementation.

View File

@ -16,6 +16,7 @@
# limitations under the License.
import contextlib
import platform
import subprocess
from textwrap import dedent
@ -63,6 +64,17 @@ class TestDepends(TestCase):
mock_checkoutput.assert_called_once_with(["lsb_release", "-cirs"],
stderr=subprocess.STDOUT)
@contextlib.contextmanager
def _mock_platform_darwin(self, system):
r_val = system
mock_checkoutput = self.useFixture(
fixtures.MockPatchObject(
platform,
'system',
return_value=r_val)).mock
yield mock_checkoutput
mock_checkoutput.assert_called_once_with()
def test_detects_amazon_linux(self):
with self._mock_lsb("AmazonAMI"):
depends = Depends("")
@ -76,6 +88,12 @@ class TestDepends(TestCase):
self.assertThat(platform_profiles, Contains("platform:centos"))
self.assertThat(platform_profiles, Contains("platform:redhat"))
def test_detects_darwin(self):
with self._mock_platform_darwin("Darwin"):
depends = Depends("")
platform_profiles = depends.platform_profiles()
self.assertThat(platform_profiles, Contains("platform:darwin"))
def test_detects_rhel(self):
with self._mock_lsb("RedHatEnterpriseServer"):
depends = Depends("")