Compare commits
11 Commits
c46f518ffc
...
249b77ba80
Author | SHA1 | Date | |
---|---|---|---|
249b77ba80 | |||
bf140ba190 | |||
67ecee958a | |||
e6e2dde492 | |||
f8a0785ecd | |||
1b6a69a598 | |||
54bed5b64c | |||
de5440adeb | |||
5be8f59a84 | |||
a0d284f308 | |||
fc88a589a8 |
34
README.md
34
README.md
@ -6,7 +6,7 @@ A python lib came from [Difficult Rocket](https://github.com/shenjackyuanjie/Dif
|
||||
|
||||
## Information/信息
|
||||
|
||||
- Version / 版本: 0.2.0-alpha0
|
||||
- Version / 版本: 0.2.0-rc.1
|
||||
- Author / 作者: shenjackyuanjie <3695888@qq.com>
|
||||
|
||||
> [shenjackyuanjie](https://github.com/shenjackyuanjie)
|
||||
@ -21,6 +21,8 @@ A python lib came from [Difficult Rocket](https://github.com/shenjackyuanjie/Dif
|
||||
|
||||
```bash title="install.sh"
|
||||
pip install lib-not-dr
|
||||
pip install lib-not-dr[nuitka]
|
||||
# install with nuitka support
|
||||
```
|
||||
|
||||
## 使用/Usage
|
||||
@ -52,9 +54,39 @@ logger.debug('and this message ends with none', end=' ')
|
||||
logger.trace('so this message will be in the same line', tag='same line!')
|
||||
```
|
||||
|
||||
### Nuitka pyproject paser
|
||||
|
||||
> WIP
|
||||
> 等待 0.2.0
|
||||
|
||||
```toml title="pyproject.toml"
|
||||
[tool.lndl.nuitka]
|
||||
main = "main.py"
|
||||
# --main=main.py
|
||||
standalone = true
|
||||
onefile = false
|
||||
```
|
||||
|
||||
```bash
|
||||
lndl_nuitka .
|
||||
lndl_nuitka . -- --onefile
|
||||
# add --onefile to nuitka
|
||||
lndl_nuitka . -y
|
||||
# run without confirmation
|
||||
lndl_nuitka . -n
|
||||
# do not run
|
||||
```
|
||||
|
||||
### Nuitka Compiler Helper
|
||||
|
||||
#### Warning/警告
|
||||
|
||||
::: warning
|
||||
> 已经弃用 Deprecated
|
||||
> 请改用 lndl_nuitka / python -m lndl_nuitka
|
||||
|
||||
:::
|
||||
|
||||
> simple example
|
||||
> 简单示例
|
||||
|
||||
|
@ -1,5 +1,25 @@
|
||||
# Change log / 更新日志
|
||||
|
||||
## 0.2.0-beta.2
|
||||
|
||||
### lndl-nuitka
|
||||
|
||||
- 可以使用 `--` 单独添加参数了
|
||||
- 例如 `lndl-nuitka -- --onefile`
|
||||
- 会将 `--onefile` 添加到 `nuitka` 的参数中
|
||||
|
||||
## 0.2.0-beta.0/1
|
||||
|
||||
### 重构
|
||||
|
||||
- 重构了文件目录结构
|
||||
- 保证 `setuptools` 可以正常工作
|
||||
|
||||
### lndl-nuitka
|
||||
|
||||
- 添加了脚本 `lndl-nuitka`
|
||||
- 用于解析 `pyproject.toml` 中的 `tool.lndl.nuitka` 部分
|
||||
|
||||
## 0.2.0-alpha0
|
||||
|
||||
### Logger
|
||||
|
@ -1,27 +1,75 @@
|
||||
[project]
|
||||
version = "0.2.0-alpha0"
|
||||
|
||||
name = "lib-not-dr"
|
||||
description = "A python lib created from Difficult Rocket development"
|
||||
readme = "README.md"
|
||||
authors = [
|
||||
{name = "shenjackyuanjie", email = "3695888@qq.com"}
|
||||
]
|
||||
|
||||
requires-python = ">=3.8"
|
||||
classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
"Intended Audience :: Developers",
|
||||
# Python 3 支持版本.
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
# Python 实现.
|
||||
"Programming Language :: Python :: Implementation :: CPython",
|
||||
"Programming Language :: Python :: Implementation :: PyPy",
|
||||
# 系统支持.
|
||||
"Operating System :: OS Independent",
|
||||
]
|
||||
license = { file = "LICENSE" }
|
||||
dynamic = ["version"]
|
||||
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
#[tool.setuptools]
|
||||
#packages = ["lib_not_dr", "lndl_nuitka"]
|
||||
|
||||
#[tool.setuptools.packages.find]
|
||||
#namespaces = false
|
||||
#include = [
|
||||
# "lib_not_dr",
|
||||
# "lndl_nuitka"
|
||||
#]
|
||||
#where = ["."]
|
||||
|
||||
#[tool.setuptools.package-dir]
|
||||
#lib_not_dr = "lib_not_dr"
|
||||
#lndl_nuitka = "lndl_nuitka"
|
||||
|
||||
[project.optional-dependencies]
|
||||
nuitka = [
|
||||
"tomli >= 2.0.1",
|
||||
"nuitka", # any version
|
||||
]
|
||||
|
||||
[tool.setuptools.dynamic]
|
||||
version = { attr = "lib_not_dr.__version__"}
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://github.com/shenjackyuanjie/lib-not-dr"
|
||||
Repository = "https://github.com/shenjackyuanjie/lib-not-dr"
|
||||
Changelog = "https://github.com/shenjackyuanjie/lib-not-dr/blob/main/docs/change_log.md"
|
||||
|
||||
[project.scripts]
|
||||
lndl_nuitka = "lndl_nuitka:main"
|
||||
|
||||
[tool.ruff]
|
||||
target-version = "py38"
|
||||
line-length = 150
|
||||
src = [
|
||||
"lib_not_dr"
|
||||
"src",
|
||||
]
|
||||
|
||||
[tool.lndl.nuitka]
|
||||
main = "test-only.py"
|
||||
onefile = false
|
||||
standalone = true
|
||||
|
||||
|
25
src/lib_not_dr/__init__.py
Normal file
25
src/lib_not_dr/__init__.py
Normal file
@ -0,0 +1,25 @@
|
||||
# -------------------------------
|
||||
# Difficult Rocket
|
||||
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
||||
# All rights reserved
|
||||
# -------------------------------
|
||||
|
||||
__version__ = '0.2.0-rc.1'
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from lib_not_dr import (
|
||||
logger,
|
||||
nuitka,
|
||||
types,
|
||||
command
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
'__version__',
|
||||
'logger',
|
||||
'nuitka',
|
||||
'types',
|
||||
'command'
|
||||
]
|
@ -7,7 +7,7 @@ import time
|
||||
|
||||
from pathlib import Path
|
||||
from string import Template
|
||||
from typing import List, Union, Optional, Dict, Tuple, TYPE_CHECKING
|
||||
from typing import List, Union, Optional, TYPE_CHECKING
|
||||
|
||||
from lib_not_dr.logger import LogLevel
|
||||
from lib_not_dr.types.options import Options
|
18
src/lib_not_dr/nuitka/__init__.py
Normal file
18
src/lib_not_dr/nuitka/__init__.py
Normal file
@ -0,0 +1,18 @@
|
||||
# -------------------------------
|
||||
# Difficult Rocket
|
||||
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
||||
# All rights reserved
|
||||
# -------------------------------
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from lib_not_dr.nuitka import (
|
||||
reader,
|
||||
compile
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"reader",
|
||||
"compile"
|
||||
]
|
@ -422,6 +422,9 @@ class CompilerHelper(Options):
|
||||
生成的 nuitka 构建脚本
|
||||
Generated nuitka build script
|
||||
"""
|
||||
warnings.warn("\033[33mlib_not_dr.nuitka.compile is deprecated "
|
||||
"use lib_not_dr.nuitka.reader.main or lndl-nuitka instead\033[0m",
|
||||
DeprecationWarning, stacklevel=2)
|
||||
cmd_list = [self.python_cmd, '-m', 'nuitka']
|
||||
# macos 和 非 macos icon 参数不同
|
||||
if platform.system() == 'Darwin':
|
140
src/lib_not_dr/nuitka/reader/__init__.py
Normal file
140
src/lib_not_dr/nuitka/reader/__init__.py
Normal file
@ -0,0 +1,140 @@
|
||||
# -------------------------------
|
||||
# Difficult Rocket
|
||||
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
||||
# All rights reserved
|
||||
# -------------------------------
|
||||
import sys
|
||||
import time
|
||||
import subprocess
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Dict, Union, List
|
||||
|
||||
from lib_not_dr.nuitka.reader.arg_parser import pyproject_toml, toml_path_cli, gen_subprocess_args
|
||||
|
||||
support_config_dict = Dict[str, Union[str, bool, List[Union[str, tuple]]]]
|
||||
|
||||
# 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
|
||||
|
||||
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)
|
||||
|
||||
arguments:
|
||||
--no-run (-n) : only show the config, not run nuitka
|
||||
--yes (-y) : run nuitka without asking
|
||||
-- --xxx : pass to nuitka
|
||||
|
||||
用法:
|
||||
python lndl-nuitka.py --help (或 -h)
|
||||
显示这个帮助信息
|
||||
|
||||
python lndl-nuitka.py <一个文件>
|
||||
python lndl-nuitka.py <一个路径>
|
||||
python lndl-nuitka.py (直接运行 会使用当前目录)
|
||||
|
||||
参数:
|
||||
--no-run (-n) : 只显示配置, 不运行 nuitka
|
||||
--yes (-y) : 不询问直接运行 nuitka
|
||||
-- --xxx : 传递给 nuitka 的参数
|
||||
|
||||
"""
|
||||
|
||||
TOML_READERS = (
|
||||
"tomllib", # stdlib toml reader after python 3.11
|
||||
"toml", # slow pure python toml reader
|
||||
"rtoml", # rust based toml reader
|
||||
"tomlkit", # pure python toml reader
|
||||
"tomli", # pure python toml reader
|
||||
"pytomlpp", # cpp based toml reader
|
||||
"qtoml", # pure python toml reader, but faster than toml
|
||||
)
|
||||
|
||||
|
||||
def get_toml_reader():
|
||||
for module_name in TOML_READERS:
|
||||
try:
|
||||
loaded = __import__(module_name).loads
|
||||
return loaded
|
||||
except ImportError:
|
||||
continue
|
||||
error_msg = """No toml reader found, please install any below by pip:
|
||||
%s
|
||||
or use Python 3.11+""" % " ".join(
|
||||
TOML_READERS
|
||||
)
|
||||
raise ImportError(error_msg) from None
|
||||
|
||||
|
||||
toml_loads = get_toml_reader()
|
||||
|
||||
|
||||
def display_config(subprocess_command: list) -> None:
|
||||
print(f"The config is:\n\033[34m{subprocess_command} \033[0m")
|
||||
print("shell command is:\n\033[34m", end="")
|
||||
print(" ".join(subprocess_command), '\033[0m')
|
||||
print(f"Working Dir: \033[32m {Path().cwd().absolute()} \033[0m")
|
||||
|
||||
|
||||
def run_nuitka(subprocess_command: list) -> None:
|
||||
start_time = time.time()
|
||||
subprocess.run(subprocess_command, shell=True)
|
||||
end_time = time.time()
|
||||
print(f"Time Elapsed: {end_time - start_time} seconds")
|
||||
|
||||
|
||||
def main(config: support_config_dict) -> None:
|
||||
"""
|
||||
enter point for python direct call
|
||||
:param config: nuitka config dict
|
||||
:return: None
|
||||
"""
|
||||
subprocess_command = gen_subprocess_args(config)
|
||||
display_config(subprocess_command)
|
||||
run_nuitka(subprocess_command)
|
||||
|
||||
|
||||
def cli_main() -> None:
|
||||
"""
|
||||
entering point of cli
|
||||
:return: None
|
||||
"""
|
||||
if "--help" in sys.argv or "-h" in sys.argv:
|
||||
print(USEAGE)
|
||||
sys.exit(0)
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print(USEAGE)
|
||||
if input("are you sure to run? (y/n)") not in ["y", "Y", "yes", "Yes"]:
|
||||
sys.exit(0)
|
||||
|
||||
toml_file = toml_path_cli()
|
||||
|
||||
with open(toml_file, "r", encoding="utf-8") as f:
|
||||
toml = toml_loads(f.read())
|
||||
|
||||
nuitka_config = pyproject_toml(toml)
|
||||
|
||||
subprocess_command = gen_subprocess_args(nuitka_config)
|
||||
display_config(subprocess_command)
|
||||
|
||||
exit_arg = ('-no-run', '-n')
|
||||
confirm_arg = ('-y', '-yes')
|
||||
if any(arg in sys.argv for arg in exit_arg):
|
||||
sys.exit(0)
|
||||
elif all(arg not in sys.argv for arg in confirm_arg):
|
||||
if input("are you sure to run? (y/n)") not in ["y", "Y", "yes", "Yes"]:
|
||||
sys.exit(0)
|
||||
|
||||
run_nuitka(subprocess_command)
|
168
src/lib_not_dr/nuitka/reader/arg_parser.py
Normal file
168
src/lib_not_dr/nuitka/reader/arg_parser.py
Normal file
@ -0,0 +1,168 @@
|
||||
# -------------------------------
|
||||
# Difficult Rocket
|
||||
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
||||
# All rights reserved
|
||||
# -------------------------------
|
||||
|
||||
import sys
|
||||
|
||||
from pathlib import Path
|
||||
from warnings import warn
|
||||
from typing import Iterable, Dict, Union, List
|
||||
|
||||
|
||||
def pyproject_toml(toml_data: dict) -> dict:
|
||||
"""
|
||||
:param toml_data: dict (from pyproject/ raw dict)
|
||||
:return: dict
|
||||
"""
|
||||
if "tool" not in toml_data:
|
||||
raise ValueError(f"No tool section in config file/dict")
|
||||
|
||||
if "lndl" not in toml_data["tool"]:
|
||||
raise ValueError(f"No lib-not-dr(lndl) section in config file/dict")
|
||||
|
||||
if "nuitka" not in toml_data["tool"]["lndl"]:
|
||||
raise ValueError(f"No lib-not-dr(lndl).nuitka section in config file/dict")
|
||||
|
||||
nuitka_config = toml_data["tool"]["lndl"]["nuitka"]
|
||||
|
||||
if "main" not 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 toml_path_cli() -> Path:
|
||||
"""
|
||||
get toml path from cli args
|
||||
:return: 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 get_cli_nuitka_args() -> dict:
|
||||
"""
|
||||
get -- from sys.argv
|
||||
:return: list
|
||||
"""
|
||||
# no -- in sys.argv
|
||||
if len(sys.argv) < 2:
|
||||
return {}
|
||||
if '--' not in sys.argv:
|
||||
print(f"invalid args: {sys.argv}")
|
||||
return {}
|
||||
|
||||
# start from --
|
||||
index = sys.argv.index('--')
|
||||
new_args = sys.argv[index + 1:]
|
||||
arg_dict = {}
|
||||
for arg in new_args:
|
||||
if not arg.startswith('--'):
|
||||
warn(f"invalid arg: {arg}")
|
||||
else:
|
||||
arg = arg[2:] # remove --
|
||||
# arg_name: --<name>=<value>
|
||||
arg_name = arg.split('=')[0]
|
||||
if '=' in arg:
|
||||
arg_value = arg.split('=')[1]
|
||||
else:
|
||||
arg_value = True
|
||||
arg_dict[arg_name] = arg_value
|
||||
|
||||
print(f"cli config: {arg_dict}")
|
||||
return arg_dict
|
||||
|
||||
|
||||
def merge_cli_config(toml_config: dict, cli_config: dict) -> dict:
|
||||
"""
|
||||
merge toml config and cli config
|
||||
:param toml_config:
|
||||
:param cli_config:
|
||||
:return:
|
||||
"""
|
||||
for name, value in cli_config.items():
|
||||
if name in toml_config:
|
||||
warn(f"\033[33mcli config will overwrite toml config\n{name}:{toml_config[name]} -> {value}\033[0m")
|
||||
if isinstance(toml_config[name], bool):
|
||||
if not isinstance(value, bool):
|
||||
warn(f"cli config {name} is bool but toml config is not\n{value} -> {value}")
|
||||
continue
|
||||
toml_config[name] = value
|
||||
elif isinstance(toml_config[name], str):
|
||||
if not isinstance(value, str):
|
||||
warn(f"cli config {name} is str but toml config is not\n{value} -> {value}")
|
||||
continue
|
||||
toml_config[name] = value
|
||||
elif isinstance(toml_config[name], Iterable):
|
||||
if not isinstance(value, str):
|
||||
warn(f"cli config {name} is Iterable but toml config is not\n{value} -> {value}")
|
||||
continue
|
||||
toml_config[name].append(value)
|
||||
else:
|
||||
toml_config[name] = value
|
||||
return toml_config
|
||||
|
||||
|
||||
def gen_subprocess_args(nuitka_config:
|
||||
Dict[str, Union[str, bool, List[Union[str, tuple]]]]) -> list:
|
||||
cmd_list = [sys.executable, "-m", "nuitka"]
|
||||
|
||||
nuitka_config = merge_cli_config(nuitka_config, get_cli_nuitka_args())
|
||||
|
||||
def parse_value(arg_name, arg_value) -> list:
|
||||
if isinstance(value, bool):
|
||||
warn(f"bool value is not supported in list config {arg_name}")
|
||||
return []
|
||||
elif isinstance(value, str):
|
||||
return [f"--{arg_name}={arg_value}"]
|
||||
else:
|
||||
return [f"--{arg_name}={arg_value[0]}={arg_value[1]}"]
|
||||
|
||||
for name, value in nuitka_config.items():
|
||||
if value is True:
|
||||
# --<name>
|
||||
cmd_list.append(f"--{name}")
|
||||
continue
|
||||
elif isinstance(value, str):
|
||||
# --<name>=<value>
|
||||
cmd_list.append(f"--{name}={value}")
|
||||
continue
|
||||
elif isinstance(value, Iterable):
|
||||
if '__spilt__' in value:
|
||||
# --<name>=<value1> --<name>=<value2> ...
|
||||
for item in value:
|
||||
if item == '__spilt__':
|
||||
continue
|
||||
cmd_list += parse_value(name, item)
|
||||
continue
|
||||
if all(isinstance(item, str) for item in value):
|
||||
# --<name>=<value1>,<value2>,...
|
||||
cmd_list.append(f"--{name}={','.join(value)}")
|
||||
elif all(isinstance(item, (tuple, list)) for item in value):
|
||||
# --<name>=<value1>=<value2>
|
||||
# --<name>=<value3>=<value4>,...
|
||||
for item in value:
|
||||
cmd_list.append(f"--{name}={item[0]}={item[1]}")
|
||||
else:
|
||||
# 处理混杂的情况
|
||||
for item in value:
|
||||
cmd_list += parse_value(name, item)
|
||||
continue
|
||||
|
||||
return cmd_list
|
@ -4,4 +4,9 @@
|
||||
# All rights reserved
|
||||
# -------------------------------
|
||||
|
||||
__version__ = '0.2.0-alpha0'
|
||||
from lib_not_dr.nuitka.reader import cli_main
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli_main()
|
||||
|
||||
main = cli_main
|
Loading…
Reference in New Issue
Block a user