Skip to content

Commit

Permalink
util.py, binaries.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanpulver committed Jul 2, 2024
1 parent c8e5ff3 commit 11f4dc8
Show file tree
Hide file tree
Showing 2 changed files with 504 additions and 85 deletions.
44 changes: 31 additions & 13 deletions binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@
import subprocess
import sys
from collections import OrderedDict
from typing import Generator, Tuple


class environment:
class Environment:
"""
Environment class to handle the build and distribution process for different operating systems.
"""

WIN = "win"
LINUX = "linux"
MACOS = "macos"

def __init__(self):
def __init__(self) -> None:
"""
Initialize the environment based on the BINARY_OS environment variable.
"""
os_mapping = {
"windows-latest": self.WIN,
"ubuntu-20.04": self.LINUX,
Expand All @@ -25,7 +31,13 @@ def __init__(self):
self.os = os_mapping[os.getenv("BINARY_OS")]

@property
def python(self):
def python(self) -> Generator[Tuple[int, str], None, None]:
"""
Generator to yield the architecture and corresponding Python executable path.
Yields:
Generator[Tuple[int, str], None, None]: Architecture and Python executable path.
"""
for arch, python in self.PYTHON_BINARIES[self.os].items():
yield arch, python

Expand All @@ -49,11 +61,15 @@ def python(self):
}
}

def run(self, command):
"""Runs the given command via subprocess.check_output.
def run(self, command: str) -> None:
"""
Runs the given command via subprocess.run.
Exits with -1 if the command wasn't successfull.
Args:
command (str): The command to run.
Exits:
Exits with -1 if the command wasn't successful.
"""
try:
print(f"RUNNING: {command}")
Expand All @@ -68,16 +84,18 @@ def run(self, command):
print(e.output and e.output.decode('utf-8'))
sys.exit(-1)

def install(self):
def install(self) -> None:
"""
Install required dependencies
"""
for arch, python in self.python:
self.run(f"{python} -m pip install pyinstaller")
self.run(f"{python} -m pip install -r test_requirements.txt")

def dist(self):
"""Runs Pyinstaller producing a binary for every platform arch."""
def dist(self) -> None:
"""
Runs PyInstaller to produce a binary for every platform architecture.
"""
for arch, python in self.python:

# Build the binary
Expand All @@ -102,9 +120,9 @@ def dist(self):
else:
self.run(f"cp {binary_path} {artifact_path}")

def test(self):
def test(self) -> None:
"""
Runs tests for every available arch on the current platform.
Runs tests for every available architecture on the current platform.
"""
for arch, python in self.python:
self.run(f"{python} -m pytest --log-level=DEBUG")
Expand All @@ -116,7 +134,7 @@ def test(self):
print("usage: binaries.py [install|test|dist]")
sys.exit(-1)

env = environment()
env = Environment()

# Runs the command in sys.argv[1] (install|test|dist)
getattr(env, sys.argv[1])()
Expand Down
Loading

0 comments on commit 11f4dc8

Please sign in to comment.