Skip to content

Commit

Permalink
Update typing + spelling + readme (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Oct 16, 2024
1 parent 08bfb82 commit a6197e5
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Version of system builds:
- GCC 13.2.1
- Cython 3.0.10
- numpy 1.26.3
- scikit-build 0.18.0
- cffi 1.16.0
- scikit-build 0.18.1
- cffi 1.17.1


## Misc
Expand Down
17 changes: 9 additions & 8 deletions builder/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Hass.io Builder main application."""

from __future__ import annotations

from pathlib import Path
import shutil
from subprocess import CalledProcessError, TimeoutExpired
import sys
from tempfile import TemporaryDirectory
from typing import Optional

import click
import click_pathlib
Expand Down Expand Up @@ -62,7 +63,7 @@
@click.option(
"--prebuild-dir",
type=click_pathlib.Path(exists=True),
help="Folder with include allready builded wheels for upload.",
help="Folder which includes already built wheels for upload.",
)
@click.option(
"--single",
Expand All @@ -84,14 +85,14 @@
"--timeout", default=345, type=int, help="Max runtime for pip before abort."
)
def builder(
apk: Optional[str],
pip: Optional[str],
apk: str | None,
pip: str | None,
index: str,
skip_binary: str,
requirement: Optional[Path],
requirement_diff: Optional[Path],
constraint: Optional[Path],
prebuild_dir: Optional[Path],
requirement: Path | None,
requirement_diff: Path | None,
constraint: Path | None,
prebuild_dir: Path | None,
single: bool,
local: bool,
test: bool,
Expand Down
4 changes: 3 additions & 1 deletion builder/apk.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Install APKs for build on host system."""

from __future__ import annotations

import subprocess
import sys


def install_apks(apks: str) -> None:
"""Install all apk string formated as 'package1;package2'."""
"""Install all apk string formatted as 'package1;package2'."""
packages = " ".join(apks.split(";"))

subprocess.run(
Expand Down
34 changes: 17 additions & 17 deletions builder/infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import dataclass
from pathlib import Path
import re
from typing import List, Set, Dict, Final
from typing import Final

from awesomeversion import AwesomeVersion
import requests
Expand Down Expand Up @@ -49,9 +49,9 @@ def create_wheels_list(base_index: str) -> str:
return f"{base_index}/{_MUSLLINUX}/"


def create_package_map(packages: List[str]) -> Dict[str, AwesomeVersion]:
def create_package_map(packages: list[str]) -> dict[str, AwesomeVersion]:
"""Create a dictionary from package base name to package and version string."""
results = {}
results: dict[str, AwesomeVersion] = {}
for package in packages.copy():
find = _RE_REQUIREMENT.match(package)
if not find:
Expand All @@ -62,11 +62,11 @@ def create_package_map(packages: List[str]) -> Dict[str, AwesomeVersion]:
return results


def extract_packages_from_index(index: str) -> Dict[str, List[WhlPackage]]:
def extract_packages_from_index(index: str) -> dict[str, list[WhlPackage]]:
"""Extract packages from index which match the supported."""
available_data = requests.get(index, allow_redirects=True, timeout=60).text

result = {}
result: dict[str, list[WhlPackage]] = {}
for match in _RE_PACKAGE_INDEX.finditer(available_data):
package = WhlPackage(
match["name"], AwesomeVersion(match["ver"]), match["abi"], match["plat"]
Expand All @@ -80,10 +80,10 @@ def extract_packages_from_index(index: str) -> Dict[str, List[WhlPackage]]:


def check_existing_packages(
package_index: Dict[str, List[WhlPackage]], package_map: Dict[str, AwesomeVersion]
) -> Set[str]:
package_index: dict[str, list[WhlPackage]], package_map: dict[str, AwesomeVersion]
) -> set[str]:
"""Return the set of package names that already exist in the index."""
found: Set[str] = set({})
found: set[str] = set({})
for package, version in package_map.items():
if package in package_index and any(
sub_package.version == version for sub_package in package_index[package]
Expand All @@ -93,10 +93,10 @@ def check_existing_packages(


def check_available_binary(
package_index: Dict[str, List[WhlPackage]],
package_index: dict[str, list[WhlPackage]],
skip_binary: str,
packages: List[str],
constraints: List[str],
packages: list[str],
constraints: list[str],
) -> str:
"""Check if binary exists and ignore this skip."""
if skip_binary == ":none:":
Expand All @@ -108,7 +108,7 @@ def check_available_binary(
package_map = create_package_map(packages + constraints)

# View of package map limited to packages in --skip-binary
binary_package_map = {}
binary_package_map: dict[str, AwesomeVersion] = {}
for binary in list_binary:
if not (version := package_map.get(binary)):
print(
Expand All @@ -119,7 +119,7 @@ def check_available_binary(
binary_package_map[binary] = version

print(f"Checking if binaries already exist for packages {binary_package_map}")
list_found: Set[str] = check_existing_packages(package_index, binary_package_map)
list_found = check_existing_packages(package_index, binary_package_map)
print(f"Packages already exist: {list_found}")
list_needed = binary_package_map.keys() - list_found

Expand All @@ -132,11 +132,11 @@ def check_available_binary(


def remove_local_wheels(
package_index: Dict[str, List[WhlPackage]],
skip_exists: List[str],
packages: List[str],
package_index: dict[str, list[WhlPackage]],
skip_exists: list[str],
packages: list[str],
wheels_dir: Path,
) -> str:
) -> None:
"""Remove existing wheels if they already exist in the index to avoid syncing."""
package_map = create_package_map(packages)
binary_package_map = {
Expand Down
19 changes: 10 additions & 9 deletions builder/pip.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Pip build commands."""

from __future__ import annotations

import os
from pathlib import Path
from typing import List, Optional

from .utils import run_command

Expand All @@ -13,7 +14,7 @@ def build_wheels_package(
output: Path,
skip_binary: str,
timeout: int,
constraint: Optional[Path] = None,
constraint: Path | None = None,
) -> None:
"""Build wheels from a requirements file into output."""
cpu = os.cpu_count() or 4
Expand All @@ -38,7 +39,7 @@ def build_wheels_requirement(
output: Path,
skip_binary: str,
timeout: int,
constraint: Optional[Path] = None,
constraint: Path | None = None,
) -> None:
"""Build wheels from a requirements file into output."""
cpu = os.cpu_count() or 4
Expand Down Expand Up @@ -74,9 +75,9 @@ def build_wheels_local(
)


def parse_requirements(requirement: Path) -> List[str]:
def parse_requirements(requirement: Path) -> list[str]:
"""Parse a requirement files into an array."""
requirement_list = set()
requirement_list: set[str] = set()
with requirement.open("r") as data:
for line in data:
line = line.strip()
Expand All @@ -96,8 +97,8 @@ def parse_requirements(requirement: Path) -> List[str]:


def extract_packages(
requirement: Path, requirement_diff: Optional[Path] = None
) -> List[str]:
requirement: Path, requirement_diff: Path | None = None
) -> list[str]:
"""Extract packages they need build."""
packages = parse_requirements(requirement)

Expand All @@ -110,13 +111,13 @@ def extract_packages(
return list(set(packages) - set(packages_diff))


def write_requirement(requirement: Path, packages: List[str]) -> None:
def write_requirement(requirement: Path, packages: list[str]) -> None:
"""Write packages list to a requirement file."""
requirement.write_text("\n".join(packages))


def install_pips(index: str, pips: str) -> None:
"""Install all pipy string formated as 'package1;package2'."""
"""Install all pipy string formatted as 'package1;package2'."""
packages = " ".join(pips.split(";"))

run_command(
Expand Down
6 changes: 4 additions & 2 deletions builder/upload/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Supported upload function."""

from __future__ import annotations

from pathlib import Path
from importlib import import_module


def run_upload(plugin: str, local: Path, remote: str) -> None:
def run_upload(plugin_name: str, local: Path, remote: str) -> None:
"""Load a plugin and start upload."""
plugin = import_module(f".{plugin}", "builder.upload")
plugin = import_module(f".{plugin_name}", "builder.upload")

# Run upload
plugin.upload(local, remote)
2 changes: 2 additions & 0 deletions builder/upload/rsync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Upload plugin rsync."""

from __future__ import annotations

from pathlib import Path

from ..utils import run_command
Expand Down
7 changes: 4 additions & 3 deletions builder/utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"""Some utils for builder."""

from __future__ import annotations

from functools import cache
import os
from pathlib import Path
import subprocess
import sys
from typing import Dict, Optional, Tuple

import requests


@cache
def alpine_version() -> Tuple[str, str]:
def alpine_version() -> tuple[str, str]:
"""Return alpine version for index server."""
version = Path("/etc/alpine-release").read_text(encoding="utf-8").split(".")

Expand All @@ -37,7 +38,7 @@ def check_url(url: str) -> None:


def run_command(
cmd: str, env: Optional[Dict[str, str]] = None, timeout: Optional[int] = None
cmd: str, env: dict[str, str] | None = None, timeout: int | None = None
) -> None:
"""Implement subprocess.run but handle timeout different."""
subprocess.run(
Expand Down
8 changes: 5 additions & 3 deletions builder/wheel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Utils for wheel."""

from __future__ import annotations

from contextlib import suppress
from pathlib import Path
import re
import shutil
from subprocess import CalledProcessError
from tempfile import TemporaryDirectory
from typing import Dict, Final
from typing import Final

from awesomeversion import AwesomeVersion

Expand Down Expand Up @@ -59,9 +61,9 @@ def check_abi_platform(abi: str, platform: str) -> bool:
return True


def fix_wheels_unmatch_requirements(wheels_folder: Path) -> Dict[str, AwesomeVersion]:
def fix_wheels_unmatch_requirements(wheels_folder: Path) -> dict[str, AwesomeVersion]:
"""Check Wheels against our min requirements."""
result = {}
result: dict[str, AwesomeVersion] = {}
for wheel_file in wheels_folder.glob("*.whl"):
package = _RE_PACKAGE_FULL.fullmatch(wheel_file.name)
if not package:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_check_available_binary_skip_constraint() -> None:


def test_check_available_binary_for_missing_constraint() -> None:
"""Test case where skip binary is for constraint notin the index."""
"""Test case where skip binary is for constraint not in the index."""
package_index = infra.extract_packages_from_index("https://example.com")
assert (
infra.check_available_binary(
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_remove_local_wheel(tmppath: Path) -> None:
wheels_dir=tmppath,
)

# grpc is removed
# grpcio is removed
assert {p.name for p in tmppath.glob("*.whl")} == {
"google_cloud_pubsub-2.9.0-py2.py3-none-any.whl",
}
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_remove_local_wheel_preserves_newer(tmppath: Path) -> None:
wheels_dir=tmppath,
)

# grpc is removed
# grpcio is not removed
assert {p.name for p in tmppath.glob("*.whl")} == {
"grpcio-1.43.0-cp310-cp310-musllinux_1_2_x86_64.whl",
"google_cloud_pubsub-2.9.0-py2.py3-none-any.whl",
Expand Down

0 comments on commit a6197e5

Please sign in to comment.