Skip to content

Commit

Permalink
Add building from macOS support.
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoodsend committed Sep 26, 2023
1 parent 3e71b96 commit e9d1417
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion polycotylus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def machine():
"""The current architecture normalized to Linux's naming convention."""
import platform
native = platform.machine()
return {"AMD64": "x86_64"}.get(native, native)
return {"AMD64": "x86_64", "arm64": "aarch64"}.get(native, native)


from ._exceptions import PolycotylusUsageError
Expand Down
2 changes: 1 addition & 1 deletion polycotylus/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __call__(self, parser, namespace, key, option_string=None):
" * Clear a setting: polycotylus --configure docker=")
parser.add_argument("--list-localizations", action=ListLocalizationAction,
choices=["language", "region", "modifier"])
parser.add_argument("--architecture", default=polycotylus.machine())
parser.add_argument("--architecture")
parser.add_argument("--post-mortem", action="store_true",
help="Enter an in-container interactive shell whenever an "
"error occurs in a docker container")
Expand Down
5 changes: 3 additions & 2 deletions polycotylus/_arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _package_manager_queries(cls):
pacman -Qq > /base-packages
pacman -Sp --needed base-devel > /sdk-packages
pacman -Si python > /python-version
""", tty=True)
""", architecture=cls.supported_architectures[cls.preferred_architecture], tty=True)
_read = lambda path: container.file(path).decode()
cls._available_packages = set(re.findall("([^\n]+)", _read("/packages")))
preinstalled = re.findall("([^\n]+)", _read("/base-packages"))
Expand Down Expand Up @@ -195,7 +195,8 @@ def test(self, package):
@lru_cache()
def available_licenses(cls):
out = []
container = _docker.run(cls.image, verbosity=0)
container = _docker.run(cls.image, verbosity=0,
architecture=cls.preferred_architecture)
with container["/usr/share/licenses/common"] as tar:
for member in tar.getmembers():
m = re.fullmatch("common/([^/]+)/license.txt", member.name)
Expand Down
6 changes: 5 additions & 1 deletion polycotylus/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BaseDistribution(abc.ABC):

def __init__(self, project, architecture=None):
self.project = project
self.architecture = architecture or machine()
self.architecture = architecture or self.preferred_architecture
if self.architecture not in self.supported_architectures:
raise _exceptions.PolycotylusUsageError(_exceptions._unravel(f"""
Architecture "{self.architecture}" is not available on
Expand All @@ -42,6 +42,10 @@ def __init__(self, project, architecture=None):
def distro_root(self):
return self.project.root / ".polycotylus" / self.name

@_misc.classproperty
def preferred_architecture(_, cls):
return machine() if machine() in cls.supported_architectures else "x86_64"

@classmethod
def available_packages(cls):
cls._package_manager_queries()
Expand Down
2 changes: 1 addition & 1 deletion polycotylus/_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __enter__(self):
try:
self._httpd = ThreadingHTTPServer(("", self.port), handler)
except OSError as ex:
if ex.errno != 98: # pragma: no cover
if ex.errno not in {98, 48}: # pragma: no cover
raise
from polycotylus._exceptions import PolycotylusUsageError, _unravel
raise PolycotylusUsageError(_unravel("""
Expand Down
2 changes: 1 addition & 1 deletion polycotylus/_void.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Void(BaseDistribution):

@_misc.classproperty
def image(self, cls):
architecture = "x86_64" if self is None else self.architecture
architecture = cls.preferred_architecture if self is None else self.architecture
return f"ghcr.io/void-linux/void-linux:latest-mini-{architecture}{cls.libc_tag}"

def _build_image(self, target):
Expand Down
5 changes: 3 additions & 2 deletions tests/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from PIL import Image
import pytest

from polycotylus import _docker, _exceptions
from polycotylus import _docker, _exceptions, machine
from polycotylus._mirror import mirrors
from polycotylus._project import Project

Expand Down Expand Up @@ -74,7 +74,8 @@ def test_python_extras(self):
python3 -c 'import {", ".join(imports)}'
""")
with mirror:
_docker.run(self.base_image, script)
_docker.run(self.base_image, script,
architecture=self.cls.preferred_architecture)


def qemu(cls):
Expand Down
13 changes: 7 additions & 6 deletions tests/test_alpine.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_abuild_lint():
{mirror.install}
apk add -q atools
apkbuild-lint /io/APKBUILD
""", volumes=[(self.distro_root, "/io")])
""", volumes=[(self.distro_root, "/io")], architecture=self.docker_architecture)


def test_dumb_text_viewer():
Expand All @@ -64,7 +64,7 @@ def test_dumb_text_viewer():
assert "gcc" not in self.apkbuild()

_docker.run(Alpine.image, ["ash", "-c", "set -e; source /io/APKBUILD"],
volumes=[(self.distro_root, "/io")])
volumes=[(self.distro_root, "/io")], architecture=self.docker_architecture)
apks = self.build()
assert len(apks) == 2
assert "pyc" in apks
Expand All @@ -85,12 +85,13 @@ def test_dumb_text_viewer():

with mirror:
script = "sudo apk add py3-pip && pip show dumb_text_viewer"
assert "Name: dumb-text-viewer" in _docker.run(installed, script).output
assert "Name: dumb-text-viewer" in _docker.run(
installed, script, architecture=self.docker_architecture).output

assert _docker.run(installed, """
sudo apk add -q xdg-utils shared-mime-info
xdg-mime query default text/plain
""").output.strip() == "underwhelming_software-dumb_text_viewer.desktop"
""", architecture=self.docker_architecture).output.strip() == "underwhelming_software-dumb_text_viewer.desktop"


def test_png_source_icon(polycotylus_yaml):
Expand Down Expand Up @@ -271,8 +272,8 @@ def test_architecture_errors(monkeypatch):
monkeypatch.setattr(shutil, "which", lambda x: None)
monkeypatch.setattr(platform, "system", lambda: "Linux")
with pytest.raises(_exceptions.PolycotylusUsageError,
match='Emulating "aarch64" requires the "qemu-aarch64-static" command'):
Alpine(Project.from_root(shared.ubrotli), "aarch64")
match='Emulating "ppc64le" requires the "qemu-ppc64le-static" command'):
Alpine(Project.from_root(shared.ubrotli), "ppc64le")


def test_fussy_arch():
Expand Down
8 changes: 5 additions & 3 deletions tests/test_arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ def test_dumb_text_viewer():

with mirror:
script = "sudo pacman -S --noconfirm --needed python-pip && pip show dumb_text_viewer"
assert "Name: dumb-text-viewer" in _docker.run(installed, script).output
info = _docker.run(installed, "pacman -Q --info dumb-text-viewer").output
assert "Name: dumb-text-viewer" in _docker.run(
installed, script, architecture=self.docker_architecture).output
info = _docker.run(installed, "pacman -Q --info dumb-text-viewer",
architecture=self.docker_architecture).output
assert "Brénainn" in re.search("Packager *: *(.*)", info)[1]

with container[pycache.relative_to(sysroot)] as tar:
Expand Down Expand Up @@ -99,7 +101,7 @@ def test_kitchen_sink(monkeypatch):
package = self.build()["main"]
installed = self.test(package).commit()
script = "pacman -Q --info python-99---s1lly---name---packag3--x--y--z"
container = _docker.run(installed, script)
container = _docker.run(installed, script, architecture=self.docker_architecture)
assert re.search(r"""Description *: 🚀 🦄 "quoted" 'quoted again' \$\$\$""",
container.output)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_void.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class TestCommon(shared.Base):
cls = Void
base_image = "ghcr.io/void-linux/void-linux:latest-mini-x86_64-musl"
base_image = Void.image
package_install = "xbps-install -ySu xbps"


Expand Down

0 comments on commit e9d1417

Please sign in to comment.