Skip to content

Commit

Permalink
Add func to get macos version from build number
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed May 14, 2020
1 parent b482be0 commit 2e57fac
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/inventory/test_macos_build_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unittest import TestCase
from zentral.contrib.inventory.conf import macos_version_from_build


class MacOSBuildTestCase(TestCase):
def test_wrong_errors(self):
for build in ("A", "", "9"):
with self.assertRaises(ValueError) as cm:
macos_version_from_build(build)
self.assertEqual(cm.exception.args[0], "Bad build number")

def test_too_old_errors(self):
for build in ("11G56", "9A581"):
with self.assertRaises(ValueError) as cm:
macos_version_from_build(build)
self.assertEqual(cm.exception.args[0], "Cannot parse build str for macos < 10.8")

def test_ok(self):
for build, (name, minor, patch) in (("12E4022", ("OS X", 8, 4)),
("15G31", ("OS X", 11, 6)),
("19A471t", ("macOS", 15, 0)),
("19D76", ("macOS", 15, 3))):
self.assertEqual(macos_version_from_build(build),
{"name": name,
"major": 10,
"minor": minor,
"patch": patch,
"build": build})
29 changes: 29 additions & 0 deletions zentral/contrib/inventory/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

# machine snapshot platforms

LINUX = "LINUX"
Expand Down Expand Up @@ -127,3 +129,30 @@ def has_deb_packages(machine_snapshot):
return False
os_name = os_name.lower()
return "ubuntu" in os_name or "debian" in os_name


MACOS_BUILD_RE = re.compile(r"(?P<minor>[0-9]{1,2})(?P<patch_letter>[A-Z])[1-9]+[a-z]?")


def macos_version_from_build(build):
match = MACOS_BUILD_RE.match(build)
if match:
minor = int(match.group("minor")) - 4
if minor < 8:
# the patch letters are not always consecutive for older versions
# probably because of the different architectures.
raise ValueError("Cannot parse build str for macos < 10.8")
if minor < 12:
name = "OS X"
else:
name = "macOS"
patch = ord(match.group("patch_letter")) - 65
return {
"name": name,
"major": 10,
"minor": minor,
"patch": patch,
"build": build
}
else:
raise ValueError("Bad build number")

0 comments on commit 2e57fac

Please sign in to comment.