Skip to content

Commit

Permalink
feat(MONTEREY): add support for monterey
Browse files Browse the repository at this point in the history
  • Loading branch information
niall-byrne committed Jun 17, 2022
1 parent 75dfef5 commit 1ebf394
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
os: [10.15, 11]
os: [10.15, 11, 12]
platform: [x86_64]
python-version: [3.8.10]

Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
os: [10.15, 11]
os: [10.15, 11, 12]
platform: [x86_64]
python-version: [3.8.10]

Expand Down Expand Up @@ -284,7 +284,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
os: [10.15, 11]
os: [10.15, 11, 12]
platform: [x86_64]
python-version: [3.8.10]

Expand Down
12 changes: 11 additions & 1 deletion build.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ import ansible
import site
import pkg_resources

from PyInstaller.utils.hooks import exec_statement

block_cipher = None

certificates = exec_statement("""
import ssl
print(ssl.get_default_verify_paths().cafile)
""").strip().split()

cert_datas = [(f, 'lib') for f in certificates]

a = Analysis(
["entrypoint.py"],
pathex=[],
Expand All @@ -31,12 +40,13 @@ a = Analysis(
"mac_maker",
"mac_maker",
),
],
] + cert_datas,
hiddenimports=[
"ansible.cli.galaxy",
"ansible.cli.playbook",
"ansible.utils.display",
"configparser",
"dataclasses",
"distutils.version",
"logging.handlers" "jinja2",
"pty",
Expand Down
4 changes: 4 additions & 0 deletions entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import os
import sys

from mac_maker import cli
from multiprocessing import freeze_support, set_start_method

freeze_support()
set_start_method('spawn')
os.environ['SSL_CERT_FILE'] = os.path.join(sys._MEIPASS, 'lib', 'cert.pem')
cli.cli()
29 changes: 29 additions & 0 deletions mac_maker/ansible_controller/interpreter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Python interpreter discovery class."""

import os
from pathlib import Path


class InterpreterNotFound(Exception):
"""Raised when a valid Python interpreter is not found."""


class Interpreter:
"""The local Python interpreter used by Ansible."""

options = [
Path("/usr/bin/python"),
Path("/usr/bin/python3"),
]

def get_interpreter_path(self) -> Path:
"""Return the path to a valid python interpreter on this system.
:returns: The path to a valid python interpreter.
"""

for interpreter in self.options:
if os.path.exists(interpreter):
return interpreter

raise InterpreterNotFound("No Python interpreter found.")
12 changes: 9 additions & 3 deletions mac_maker/ansible_controller/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .. import config
from ..utilities.mixins.text_file import TextFileWriter
from ..utilities.state import TypeState
from .interpreter import Interpreter


class InventoryFile(TextFileWriter):
Expand All @@ -17,6 +18,7 @@ class InventoryFile(TextFileWriter):
def __init__(self, state: TypeState) -> None:
self.log = logging.getLogger(config.LOGGER_NAME)
self.state = state
self.interpreter = Interpreter()

def _is_already_present(self) -> bool:
return os.path.exists(self.state['inventory'])
Expand All @@ -30,10 +32,14 @@ def write_inventory_file(self) -> None:
if self._is_already_present():
return

self._ensure_path_exists()
self.write_text_file(
config.ANSIBLE_INVENTORY_CONTENT, self.state['inventory']
content = config.ANSIBLE_INVENTORY_CONTENT
content += (
"ansible_python_interpreter=" +
str(self.interpreter.get_interpreter_path()) + "\n"
)

self._ensure_path_exists()
self.write_text_file(content, self.state['inventory'])
self.log.debug(
"InventoryFile: Inventory has been written to %s.",
self.state['inventory'],
Expand Down
40 changes: 40 additions & 0 deletions mac_maker/ansible_controller/tests/test_interpreter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Test the Interpreter class."""

from pathlib import Path
from unittest import TestCase, mock

from .. import interpreter

INTERPRETER_MODULE = interpreter.__name__


class TestInterpreter(TestCase):
"""Test the Interpreter class."""

def setUp(self) -> None:
self.interpreter = interpreter.Interpreter()

def test_initialize(self) -> None:
for initialized_interpreter in self.interpreter.options:
self.assertIsInstance(initialized_interpreter, Path)

@mock.patch(INTERPRETER_MODULE + ".os")
def test_first_interpreter_valid(self, m_os: mock.Mock) -> None:

m_os.path.exists.side_effect = [True]
self.assertEqual(
self.interpreter.options[0], self.interpreter.get_interpreter_path()
)

@mock.patch(INTERPRETER_MODULE + ".os")
def test_last_interpreter_valid(self, m_os: mock.Mock) -> None:
m_os.path.exists.side_effect = [False, True]
self.assertEqual(
self.interpreter.options[1], self.interpreter.get_interpreter_path()
)

@mock.patch(INTERPRETER_MODULE + ".os")
def test_no_interpreter_valid(self, m_os: mock.Mock) -> None:
m_os.path.exists.side_effect = [False, False]
with self.assertRaises(interpreter.InterpreterNotFound):
self.interpreter.get_interpreter_path()
17 changes: 13 additions & 4 deletions mac_maker/ansible_controller/tests/test_inventory.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Test the InventoryFile class."""

from logging import Logger
from pathlib import Path
from unittest import TestCase, mock

from ... import config
from ...utilities import filesystem, state
from .. import inventory
from .. import interpreter, inventory

INVENTORY_MODULE = inventory.__name__

Expand All @@ -14,7 +15,6 @@ class TestInventoryFile(TestCase):
"""Test the InventoryFile class."""

def setUp(self) -> None:
super().setUp()
self.root_folder = "/root/mock/dir1"
self.filesystem = filesystem.FileSystem(self.root_folder)
self.state = state.State()
Expand All @@ -27,14 +27,17 @@ def test_initialize(self) -> None:
Logger,
)
self.assertEqual(self.inventory.state, self.loaded_state)
self.assertIsInstance(self.inventory.interpreter, interpreter.Interpreter)

@mock.patch(INVENTORY_MODULE + ".os")
@mock.patch(INVENTORY_MODULE + ".TextFileWriter.write_text_file")
@mock.patch(INVENTORY_MODULE + ".Interpreter.get_interpreter_path")
def test_write_inventory_file(
self, m_write: mock.Mock, m_os: mock.Mock
self, m_interpreter: mock.Mock, m_write: mock.Mock, m_os: mock.Mock
) -> None:

m_os.path.exists.return_value = False
m_interpreter.return_value = Path("/usr/bin/mock")

self.inventory.write_inventory_file()

Expand All @@ -43,8 +46,14 @@ def test_write_inventory_file(
exist_ok=True,
)

expected_inventory = config.ANSIBLE_INVENTORY_CONTENT

expected_inventory += "ansible_python_interpreter="
expected_inventory += str(m_interpreter.return_value)
expected_inventory += "\n"

m_write.assert_called_once_with(
config.ANSIBLE_INVENTORY_CONTENT, self.loaded_state['inventory']
expected_inventory, self.loaded_state['inventory']
)

@mock.patch(INVENTORY_MODULE + ".os")
Expand Down
1 change: 0 additions & 1 deletion mac_maker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
'[all]\n'
'localhost\t'
'ansible_connection=local\t'
'ansible_python_interpreter=/usr/bin/python\n'
)
ANSIBLE_JOB_SPEC_MESSAGE = "--- Job Spec Created ---"
ANSIBLE_JOB_SPEC_READ_MESSAGE = "--- Job Spec Loaded ---"
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
ansible = "^5.2.0"
click = ">=8.0.0,<9.0.0"
jsonschema = "^4.3.3"
pyinstaller = "^4.3"
pyinstaller = "^5.1"
python = ">=3.8.0,<3.9.0"
requests = "^2.25.1"

Expand Down Expand Up @@ -89,7 +89,6 @@
darglint = "^1.8.1"
parameterized = "^0.8.1"
pydocstyle = "^6.1.1"
pyinstaller = "^4.3,!=4.4"
types-PyYAML = "^6.0.1"
types-pkg-resources = "^0.1.3"
types-requests = "^2.26.3"
Expand Down
10 changes: 5 additions & 5 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ wrong_args(){
exit 127
}

binary() {
build_binary() {

poetry build
poetry run pip install ./dist/mac_maker-*-py3-none-any.whl
poetry run pyinstaller --onefile build.spec
poetry run pyinstaller build.spec

pushd dist || exit 127
VERSION_NAME="${2}_$(uname -m)"
Expand All @@ -31,7 +31,7 @@ binary() {
popd || true
}

python() {
build_python() {

brew install pyenv coreutils openssl readline sqlite3 xz zlib

Expand All @@ -49,10 +49,10 @@ main() {
case ${1} in

pyenv)
python "$@"
build_python "$@"
;;
binary)
binary "$@"
build_binary "$@"
;;
*)
wrong_args
Expand Down

0 comments on commit 1ebf394

Please sign in to comment.