Last active
November 19, 2023 12:23
-
-
Save shenjackyuanjie/ed0d922dd6bc0d795657b736d4a51d4f to your computer and use it in GitHub Desktop.
A demo of lndl parse nuitka command
This file contains hidden or 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
# by shenjackyuanjie | |
# 2023 11 19 | |
# at python 3.10.11 | |
# 0.2.0 ? | |
USEAGE = """ | |
usage: | |
python lndl-nuitka.py --help (or -h) | |
show this help message | |
python lndl-nuitka.py <path-to-pyproject.toml> | |
python lndl-nuitka.py <path-to-dir> | |
python lndl-nuitka.py (with nothing, will use current dir) | |
then it will | |
- read the given file or | |
- find pyproject.toml in the given dir or | |
- find pyproject.toml in current dir | |
and read the `[tool.lndl.nuitka]` section | |
then it will run nuitka with the given config | |
用法: | |
python lndl-nuitka.py --help (或 -h) | |
显示这个帮助信息 | |
python lndl-nuitka.py <一个文件> | |
python lndl-nuitka.py <一个路径> | |
python lndl-nuitka.py (直接运行 会使用当前目录) | |
然后它会 | |
- 读取给定的文件 或 | |
- 在给定的路径中找到 pyproject.toml 或 | |
- 在当前目录中找到 pyproject.toml | |
并读取 `[tool.lndl.nuitka]` 部分 | |
然后它会使用给定的配置运行 nuitka | |
""" | |
import sys | |
import time | |
import subprocess | |
from pathlib import Path | |
def get_toml(): | |
try: | |
from toml import toml_loads | |
except ImportError: | |
try: | |
from rtoml import loads as toml_loads | |
except ImportError: | |
from tomlkit import parse as toml_loads | |
return toml_loads | |
toml_loads = get_toml() | |
def validate_toml(toml_data: dict, file_name: Path) -> dict: | |
if not 'tool' in toml_data: | |
raise ValueError(f"No tool section in {file_name}") | |
if not 'lndl' in toml_data['tool']: | |
raise ValueError(f"No lib-not-dr(lndl) section in {file_name}") | |
if not 'nuitka' in toml_data['tool']['lndl']: | |
raise ValueError(f"No lib-not-dr(lndl).nuitka section in {file_name}") | |
nuitka_config = toml_data['tool']['lndl']['nuitka'] | |
if not 'main' in nuitka_config: | |
raise ValueError("'main' not define in lib-not-dr(lndl).nuitka section\ndefine it with 'main = [<main.py>]'") | |
return nuitka_config | |
def gen_subprocess_args(nuitka_config: dict) -> list: | |
cmd_list = [sys.executable, '-m', 'nuitka'] | |
for name, value in nuitka_config.items(): | |
if value == True: | |
# --<name> | |
cmd_list += [f"--{name}"] | |
continue | |
elif isinstance(value, str): | |
# --<name>=<value> | |
cmd_list += [f"--{name}={value}"] | |
continue | |
elif isinstance(value, list): | |
# --<name>=<value1>,<value2>,... | |
cmd_list += [f"--{name}={','.join(value)}"] | |
continue | |
return cmd_list | |
def get_toml() -> Path: | |
if len(sys.argv) < 2: | |
raw_path = Path().cwd() | |
else: | |
raw_path = Path(sys.argv[1]) | |
if raw_path.is_file(): | |
return raw_path | |
elif raw_path.is_dir(): | |
if (raw_path / 'pyproject.toml').exists(): | |
return raw_path / 'pyproject.toml' | |
else: | |
raise FileNotFoundError(f"pyproject.toml not found in {raw_path}") | |
else: | |
raise FileNotFoundError(f"{raw_path} not found") | |
def main(): | |
toml_file = get_toml() | |
with open(toml_file, 'r') as f: | |
toml = toml_loads(f.read()) | |
nuitka_config = validate_toml(toml, toml_file) | |
subprocess_command = gen_subprocess_args(nuitka_config) | |
# printed in blue text | |
# \033[34m is the escape code for blue text | |
print(f"\033[34mRunning: {subprocess_command}\033[0m") | |
start_time = time.time() | |
subprocess.run(subprocess_command, shell=True) | |
end_time = time.time() | |
print(f"Time Elapsed: {end_time - start_time} seconds") | |
if __name__ == '__main__': | |
if '--help' in sys.argv or '-h' in sys.argv: | |
print(USEAGE) | |
sys.exit(0) | |
if len(sys.argv) < 2: | |
print(USEAGE) | |
if not input('are you sure to run? (y/n)') in ['y', 'Y', 'yes', 'Yes']: | |
sys.exit(0) | |
main() |
This file contains hidden or 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
[tool.lndl.nuitka] | |
main = ["matplotlib-demo.py"] | |
plugin-enable = ["pyqt5"] | |
include-qt-plugins = ["sensible,styles"] | |
nofollow-import-to = ["PIL"] | |
# include-module = [""] | |
lto = "yes" | |
msvc = "latest" | |
clang = true | |
output-dir = "build" | |
standalone = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment