|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""This script builds the dfetch executable using Nuitka.""" |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import tomllib as toml |
| 7 | +from typing import Union |
| 8 | + |
| 9 | + |
| 10 | +def parse_option( |
| 11 | + option_name: str, option_value: Union[bool, str, list, dict] |
| 12 | +) -> list[str]: |
| 13 | + """ |
| 14 | + Convert a config value to CLI args for Nuitka. |
| 15 | + Handles booleans, strings, lists, and nested dicts. |
| 16 | + """ |
| 17 | + args = [] |
| 18 | + cli_key = f"--{option_name.replace('_','-')}" |
| 19 | + |
| 20 | + if isinstance(option_value, bool): |
| 21 | + if option_value: |
| 22 | + args.append(cli_key) |
| 23 | + elif isinstance(option_value, str): |
| 24 | + args.append(f"{cli_key}={option_value}") |
| 25 | + elif isinstance(option_value, list): |
| 26 | + for v in option_value: |
| 27 | + if isinstance(v, dict): |
| 28 | + parts = [f"{v[k]}" for k in v] |
| 29 | + args.append(f"{cli_key}={'='.join(parts)}") |
| 30 | + else: |
| 31 | + args.append(f"{cli_key}={v}") |
| 32 | + else: |
| 33 | + args.append(f"{cli_key}={option_value}") |
| 34 | + |
| 35 | + return args |
| 36 | + |
| 37 | + |
| 38 | +# Load pyproject.toml |
| 39 | +with open("pyproject.toml", "r", encoding="UTF-8") as pyproject_file: |
| 40 | + pyproject = toml.loads(pyproject_file.read()) |
| 41 | +nuitka_opts = pyproject.get("tool", {}).get("nuitka", {}) |
| 42 | + |
| 43 | + |
| 44 | +if sys.platform.startswith("win"): |
| 45 | + nuitka_opts["output-filename"] = nuitka_opts["output-filename-win"] |
| 46 | +elif sys.platform.startswith("linux"): |
| 47 | + nuitka_opts["output-filename"] = nuitka_opts["output-filename-linux"] |
| 48 | +elif sys.platform.startswith("darwin"): |
| 49 | + nuitka_opts["output-filename"] = nuitka_opts["output-filename-macos"] |
| 50 | + |
| 51 | +for key in ["output-filename-win", "output-filename-linux", "output-filename-macos"]: |
| 52 | + nuitka_opts.pop(key, None) |
| 53 | + |
| 54 | +command = [sys.executable, "-m", "nuitka"] |
| 55 | +for key, value in nuitka_opts.items(): |
| 56 | + command.extend(parse_option(key, value)) |
| 57 | + |
| 58 | +command.append(os.path.join("dfetch", "__main__.py")) |
| 59 | + |
| 60 | +print(command) |
| 61 | +subprocess.check_call(command) |
0 commit comments