From c2409e813ef64d25e8bd4fe91adce6e1db240800 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Sun, 28 Jul 2024 19:43:25 -0400 Subject: [PATCH] build: shared data fix Make sure the scripts directory is included in the "data/share" folder in the wheel. In addition, remove the stray .gitignore that is included when building a wheel from an sdist. Signed-off-by: Eric Callahan --- .gitignore | 1 + pdm_build.py | 103 ++++++++++++++++++++++++++++++++++++++ pyproject.toml | 6 ++- scripts/pdm_build_dist.py | 80 ----------------------------- 4 files changed, 109 insertions(+), 81 deletions(-) create mode 100644 pdm_build.py delete mode 100644 scripts/pdm_build_dist.py diff --git a/.gitignore b/.gitignore index fd664c3..b394201 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ start_moonraker .pdm-python build dist +share diff --git a/pdm_build.py b/pdm_build.py new file mode 100644 index 0000000..47f5f5e --- /dev/null +++ b/pdm_build.py @@ -0,0 +1,103 @@ +# Wheel Setup Script for generating metadata +# +# Copyright (C) 2023 Eric Callahan +# +# This file may be distributed under the terms of the GNU GPLv3 license + +from __future__ import annotations +import pathlib +import subprocess +import shlex +import json +import shutil +from datetime import datetime, timezone +from typing import Dict, Any, TYPE_CHECKING + +if TYPE_CHECKING: + from pdm.backend.hooks.base import Context + +__package_name__ = "moonraker" +__dependencies__ = "scripts/system-dependencies.json" + +def _run_git_command(cmd: str) -> str: + prog = shlex.split(cmd) + process = subprocess.Popen( + prog, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + ret, err = process.communicate() + retcode = process.wait() + if retcode == 0: + return ret.strip().decode() + return "" + +def get_commit_sha(source_path: pathlib.Path) -> str: + cmd = f"git -C {source_path} rev-parse HEAD" + return _run_git_command(cmd) + +def retrieve_git_version(source_path: pathlib.Path) -> str: + cmd = f"git -C {source_path} describe --always --tags --long --dirty" + return _run_git_command(cmd) + + +def pdm_build_clean(context: Context) -> None: + share_path: pathlib.Path = context.root.joinpath("share") + if share_path.exists(): + shutil.rmtree(str(share_path)) + +def pdm_build_initialize(context: Context) -> None: + context.ensure_build_dir() + proj_name: str = context.config.metadata['name'] + build_dir = pathlib.Path(context.build_dir) + data_path = context.root.joinpath(f"share/{proj_name}") + pkg_path = build_dir.joinpath(__package_name__) + pkg_path.mkdir(parents=True, exist_ok=True) + rinfo_path: pathlib.Path = pkg_path.joinpath("release_info") + rinfo_data: str = "" + if context.root.joinpath(".git").exists(): + build_ver: str = context.config.metadata['version'] + build_time = datetime.now(timezone.utc) + urls: Dict[str, str] = context.config.metadata['urls'] + release_info: Dict[str, Any] = { + "project_name": proj_name, + "package_name": __package_name__, + "urls": {key.lower(): val for key, val in urls.items()}, + "package_version": build_ver, + "git_version": retrieve_git_version(context.root), + "commit_sha": get_commit_sha(context.root), + "build_time": datetime.isoformat(build_time, timespec="seconds") + } + if __dependencies__: + deps = pathlib.Path(context.root).joinpath(__dependencies__) + if deps.is_file(): + dep_info: Dict[str, Any] = json.loads(deps.read_bytes()) + release_info["system_dependencies"] = dep_info + # Write the release info to both the package and the data path + rinfo_data = json.dumps(release_info, indent=4) + rinfo_path.write_text(rinfo_data) + else: + rinfo_path = context.root.joinpath(f"{proj_name}/release_info") + if rinfo_path.is_file(): + rinfo_data = rinfo_path.read_text() + else: + rinfo_data = "" + data_path.mkdir(parents=True, exist_ok=True) + if rinfo_data: + data_path.joinpath("release_info").write_text(rinfo_data) + scripts_path: pathlib.Path = context.root.joinpath("scripts") + scripts_dest: pathlib.Path = data_path.joinpath("scripts") + scripts_dest.mkdir() + for item in scripts_path.iterdir(): + if item.name in ("__pycache__", "python_wheels"): + continue + if item.is_dir(): + shutil.copytree(str(item), str(scripts_dest.joinpath(item.name))) + else: + shutil.copy2(str(item), str(scripts_dest)) + git_ignore = build_dir.joinpath(".gitignore") + if git_ignore.is_file(): + git_ignore.unlink() + +def pdm_build_finalize(context: Context, artifact: pathlib.Path) -> None: + share_path: pathlib.Path = context.root.joinpath("share") + if share_path.exists(): + shutil.rmtree(str(share_path)) diff --git a/pyproject.toml b/pyproject.toml index 16f0fd6..3356c14 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,8 +67,12 @@ write_template = "__version__ = '{}'\n" [tool.pdm.build] excludes = ["./**/.git", "moonraker/moonraker.py"] includes = ["moonraker"] +source-includes = ["scripts"] editable-backend = "path" -custom-hook = "scripts/pdm_build_dist.py" +custom-hook = "pdm_build.py" + +[tool.pdm.build.wheel-data] +data = [{path = "share/moonraker/**/*", relative-to = "."}] [project.scripts] moonraker = "moonraker.server:main" diff --git a/scripts/pdm_build_dist.py b/scripts/pdm_build_dist.py deleted file mode 100644 index d3279e1..0000000 --- a/scripts/pdm_build_dist.py +++ /dev/null @@ -1,80 +0,0 @@ -# Wheel Setup Script for generating metadata -# -# Copyright (C) 2023 Eric Callahan -# -# This file may be distributed under the terms of the GNU GPLv3 license - -from __future__ import annotations -import pathlib -import subprocess -import shlex -import json -import shutil -from datetime import datetime, timezone -from typing import Dict, Any, TYPE_CHECKING - -if TYPE_CHECKING: - from pdm.backend.hooks.base import Context - -__package_name__ = "moonraker" -__dependencies__ = "scripts/system-dependencies.json" - -def _run_git_command(cmd: str) -> str: - prog = shlex.split(cmd) - process = subprocess.Popen( - prog, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - ret, err = process.communicate() - retcode = process.wait() - if retcode == 0: - return ret.strip().decode() - return "" - -def get_commit_sha(source_path: pathlib.Path) -> str: - cmd = f"git -C {source_path} rev-parse HEAD" - return _run_git_command(cmd) - -def retrieve_git_version(source_path: pathlib.Path) -> str: - cmd = f"git -C {source_path} describe --always --tags --long --dirty" - return _run_git_command(cmd) - -def pdm_build_initialize(context: Context) -> None: - context.ensure_build_dir() - build_ver: str = context.config.metadata['version'] - proj_name: str = context.config.metadata['name'] - urls: Dict[str, str] = context.config.metadata['urls'] - build_dir = pathlib.Path(context.build_dir) - rel_dpath = f"{__package_name__}-{build_ver}.data/data/share/{proj_name}" - data_path = build_dir.joinpath(rel_dpath) - pkg_path = build_dir.joinpath(__package_name__) - build_time = datetime.now(timezone.utc) - release_info: Dict[str, Any] = { - "project_name": proj_name, - "package_name": __package_name__, - "urls": {key.lower(): val for key, val in urls.items()}, - "package_version": build_ver, - "git_version": retrieve_git_version(context.root), - "commit_sha": get_commit_sha(context.root), - "build_time": datetime.isoformat(build_time, timespec="seconds") - } - if __dependencies__: - deps = pathlib.Path(context.root).joinpath(__dependencies__) - if deps.is_file(): - dep_info: Dict[str, Any] = json.loads(deps.read_bytes()) - release_info["system_dependencies"] = dep_info - # Write the release info to both the package and the data path - rinfo_data = json.dumps(release_info, indent=4) - data_path.mkdir(parents=True, exist_ok=True) - pkg_path.mkdir(parents=True, exist_ok=True) - data_path.joinpath("release_info").write_text(rinfo_data) - pkg_path.joinpath("release_info").write_text(rinfo_data) - scripts_path = context.root.joinpath("scripts") - scripts_dest = data_path.joinpath("scripts") - scripts_dest.mkdir() - for item in scripts_path.iterdir(): - if item.name == "__pycache__": - continue - if item.is_dir(): - shutil.copytree(str(item), str(scripts_dest.joinpath(item.name))) - else: - shutil.copy2(str(item), str(scripts_dest))