diff --git a/packer/main.py b/packer/main.py deleted file mode 100644 index 10482dc..0000000 --- a/packer/main.py +++ /dev/null @@ -1,6 +0,0 @@ -def main(): - print("Hello from sarasacw-omrf-packer!") - - -if __name__ == "__main__": - main() diff --git a/packer/pyproject.toml b/packer/pyproject.toml index b766acc..f1282b1 100644 --- a/packer/pyproject.toml +++ b/packer/pyproject.toml @@ -3,7 +3,17 @@ name = "sarasacw-omrf-packer" version = "0.1.0" description = "Add your description here" readme = "README.md" +authors = [ + { name = "yyc12345", email = "yyc12321@outlook.com" } +] requires-python = ">=3.13" dependencies = [ "jinja2==3.1.6", ] + +[project.scripts] +sarasacw-omrf-packer = "sarasacw_omrf_packer:main" + +[build-system] +requires = ["uv_build>=0.8.0,<0.9"] +build-backend = "uv_build" diff --git a/packer/src/sarasacw_omrf_packer/__init__.py b/packer/src/sarasacw_omrf_packer/__init__.py new file mode 100644 index 0000000..8365f85 --- /dev/null +++ b/packer/src/sarasacw_omrf_packer/__init__.py @@ -0,0 +1,13 @@ +from .archiver import pack +from .cargo import get_artifacts +from .cli import Cli, parse +from .cmake import render as render_cmake +from .pkgconfig import render as render_pkgconfig + + +def main() -> None: + cli = parse() + get_artifacts(cli.manifest_path) + render_cmake(cli.output_dir) + render_pkgconfig(cli.output_dir) + pack(cli.output_dir, cli.output_dir + ".zip") diff --git a/packer/src/sarasacw_omrf_packer/archiver.py b/packer/src/sarasacw_omrf_packer/archiver.py new file mode 100644 index 0000000..42df4df --- /dev/null +++ b/packer/src/sarasacw_omrf_packer/archiver.py @@ -0,0 +1,10 @@ +import zipfile +from pathlib import Path + + +def pack(dist_dir: str, output_zip: str) -> None: + dist = Path(dist_dir) + with zipfile.ZipFile(output_zip, "w", zipfile.ZIP_DEFLATED) as zf: + for path in dist.rglob("*"): + if path.is_file(): + zf.write(path, path.relative_to(dist)) diff --git a/packer/src/sarasacw_omrf_packer/cargo.py b/packer/src/sarasacw_omrf_packer/cargo.py new file mode 100644 index 0000000..834375f --- /dev/null +++ b/packer/src/sarasacw_omrf_packer/cargo.py @@ -0,0 +1,22 @@ +import json +import subprocess + + +def get_metadata(manifest_path: str) -> dict: + cmd = [ + "cargo", + "metadata", + "--format-version=1", + f"--manifest-path={manifest_path}", + ] + proc = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + stdout, _ = proc.communicate() + return json.loads(stdout) + + +def get_artifacts(manifest_path: str) -> list[str]: + metadata = get_metadata(manifest_path) + target_directory = metadata["target_directory"] + return [target_directory] diff --git a/packer/src/sarasacw_omrf_packer/cli.py b/packer/src/sarasacw_omrf_packer/cli.py new file mode 100644 index 0000000..f380261 --- /dev/null +++ b/packer/src/sarasacw_omrf_packer/cli.py @@ -0,0 +1,19 @@ +import argparse +from dataclasses import dataclass + + +@dataclass +class Cli: + manifest_path: str + output_dir: str + + +def parse() -> Cli: + parser = argparse.ArgumentParser( + prog="sarasacw-omrf-packer", + description="Create distribution and generate CMake and pkg-config files.", + ) + parser.add_argument("--manifest-path", default="Cargo.toml") + parser.add_argument("--output-dir", default="dist") + args = parser.parse_args() + return Cli(manifest_path=args.manifest_path, output_dir=args.output_dir) diff --git a/packer/src/sarasacw_omrf_packer/cmake.py b/packer/src/sarasacw_omrf_packer/cmake.py new file mode 100644 index 0000000..0c8ce86 --- /dev/null +++ b/packer/src/sarasacw_omrf_packer/cmake.py @@ -0,0 +1,13 @@ +from pathlib import Path + +from jinja2 import Environment, FileSystemLoader, select_autoescape + + +def render(output_dir: str) -> None: + templates_dir = Path(__file__).parent / "templates" + env = Environment( + loader=FileSystemLoader(templates_dir), + autoescape=select_autoescape(), + ) + out = Path(output_dir) + out.mkdir(parents=True, exist_ok=True) diff --git a/packer/src/sarasacw_omrf_packer/pkgconfig.py b/packer/src/sarasacw_omrf_packer/pkgconfig.py new file mode 100644 index 0000000..0c8ce86 --- /dev/null +++ b/packer/src/sarasacw_omrf_packer/pkgconfig.py @@ -0,0 +1,13 @@ +from pathlib import Path + +from jinja2 import Environment, FileSystemLoader, select_autoescape + + +def render(output_dir: str) -> None: + templates_dir = Path(__file__).parent / "templates" + env = Environment( + loader=FileSystemLoader(templates_dir), + autoescape=select_autoescape(), + ) + out = Path(output_dir) + out.mkdir(parents=True, exist_ok=True) diff --git a/packer/src/sarasacw_omrf_packer/templates/cmake.in b/packer/src/sarasacw_omrf_packer/templates/cmake.in new file mode 100644 index 0000000..e69de29 diff --git a/packer/src/sarasacw_omrf_packer/templates/pkgconfig.pc.in b/packer/src/sarasacw_omrf_packer/templates/pkgconfig.pc.in new file mode 100644 index 0000000..e69de29 diff --git a/packer/uv.lock b/packer/uv.lock index afe2fd8..add9c67 100644 --- a/packer/uv.lock +++ b/packer/uv.lock @@ -69,7 +69,7 @@ wheels = [ [[package]] name = "sarasacw-omrf-packer" version = "0.1.0" -source = { virtual = "." } +source = { editable = "." } dependencies = [ { name = "jinja2" }, ]