-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(python): WIP backport of changes from skalt/pagefind_python
- Loading branch information
Showing
30 changed files
with
756 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
external-sources=true | ||
source-path=SCRIPTDIR | ||
disable=SC2002 | ||
# SC2002: ignore "useless cat" warning: starting pipes with `cat` improves composability |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
output | ||
# ^ from src/tests/integration.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[virtualenvs] | ||
in-project = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
this_file = Path(__file__) | ||
this_dir = Path(__file__).parent | ||
python_root = this_dir.parent.parent.resolve().absolute() | ||
upstream_version_file = python_root / "pagefind_version.txt" | ||
dist_dir = python_root / "dist" | ||
vendor_dir = python_root / "vendor" | ||
|
||
|
||
def setup_logging() -> None: | ||
logging.basicConfig( | ||
level=os.environ.get("PAGEFIND_PYTHON_LOG_LEVEL") or logging.INFO | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import tarfile | ||
import tempfile | ||
from pathlib import Path | ||
from typing import List | ||
|
||
from . import dist_dir, setup_logging | ||
from .binary_only_wheel import ( | ||
LLVM_TRIPLES_TO_PYTHON_WHEEL_PLATFORMS, | ||
write_pagefind_bin_only_wheel, | ||
) | ||
from .get_pagefind_release import download | ||
|
||
__candidates = ( | ||
"pagefind", | ||
"pagefind.exe", | ||
"pagefind_extended", | ||
"pagefind_extended.exe", | ||
) | ||
|
||
|
||
def find_bin(dir: Path) -> Path: | ||
for file in dir.iterdir(): | ||
if file.is_file() and file.name in __candidates: | ||
return file | ||
raise FileNotFoundError(f"Could not find any of {__candidates} in {dir}") | ||
|
||
|
||
def get_llvm_triple(tar_gz: Path) -> str: | ||
assert tar_gz.name.endswith(".tar.gz") | ||
# parse the llvm triple from the archive name | ||
llvm_triple = tar_gz.name | ||
llvm_triple = llvm_triple.removesuffix(".tar.gz") | ||
llvm_triple = llvm_triple.removeprefix(f"pagefind-{tag_name}-") | ||
llvm_triple = llvm_triple.removeprefix(f"pagefind_extended-{tag_name}-") | ||
return llvm_triple | ||
|
||
|
||
def check_platforms(certified: List[Path]) -> None: | ||
for compressed_archive in certified: | ||
llvm_triple = get_llvm_triple(compressed_archive) | ||
platform = LLVM_TRIPLES_TO_PYTHON_WHEEL_PLATFORMS.get(llvm_triple) | ||
if platform is None: | ||
raise ValueError(f"Unsupported platform: {llvm_triple}") | ||
|
||
|
||
if __name__ == "__main__": | ||
setup_logging() | ||
certified, tag_name = download("latest", dry_run=False) | ||
# create a temp directory to hold the extracted binaries | ||
check_platforms(certified) | ||
dist_dir.mkdir(exist_ok=True) | ||
for tar_gz in certified: | ||
llvm_triple = get_llvm_triple(tar_gz) | ||
platform = LLVM_TRIPLES_TO_PYTHON_WHEEL_PLATFORMS.get(llvm_triple) | ||
if platform is None: | ||
raise ValueError(f"Unsupported platform: {llvm_triple}") | ||
|
||
# FIXME: avoid writing the extracted bin to disk | ||
# unpack the tar.gz archive | ||
name = tar_gz.name.removesuffix(".tar.gz") | ||
with tempfile.TemporaryDirectory(prefix=name + "~") as _temp_dir: | ||
temp_dir = Path(_temp_dir) | ||
with tarfile.open(tar_gz, "r:gz") as tar: | ||
tar.extractall(_temp_dir) | ||
write_pagefind_bin_only_wheel( | ||
executable=find_bin(temp_dir), | ||
output_dir=dist_dir, | ||
version=tag_name.removeprefix("v"), | ||
platform=platform, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# HACK: This script is a hack to build the API package without using poetry to lock the | ||
# optional dependencies. It might be preferable to use setuptools directly rather than | ||
# work around poetry. | ||
|
||
from . import python_root, setup_logging | ||
import subprocess | ||
|
||
pyproject_toml = python_root / "pyproject.toml" | ||
|
||
|
||
def main() -> None: | ||
original = pyproject_toml.read_text() | ||
temp = "" | ||
for line in original.splitlines(): | ||
if line.endswith("#!!opt"): | ||
temp += line.removeprefix("# ") + "\n" | ||
else: | ||
temp += line + "\n" | ||
with pyproject_toml.open("w") as f: | ||
f.write(temp) | ||
subprocess.run(["poetry", "build"], check=True) | ||
with pyproject_toml.open("w") as f: | ||
f.write(original) | ||
|
||
|
||
if __name__ == "__main__": | ||
setup_logging() | ||
main() |
Oops, something went wrong.