From ddbb2f781dbb2861f7a4dcd71610038a029e5f88 Mon Sep 17 00:00:00 2001 From: sovlookup Date: Thu, 7 Jul 2022 11:21:03 +0800 Subject: [PATCH] add pdm pkg --- qpt/kernel/qinterpreter.py | 58 ++++++++++++++++++++++++----- qpt/modules/auto_requirements.py | 23 ++++++++---- qpt/version.py | 2 +- setup.py | 1 + unit_test/sanbox_pdm/.pdm.toml | 2 + unit_test/sanbox_pdm/main.py | 5 +++ unit_test/sanbox_pdm/pdm.lock | 15 ++++++++ unit_test/sanbox_pdm/pyproject.toml | 21 +++++++++++ unit_test/sanbox_pdm/qpt2exe.py | 6 +++ 9 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 unit_test/sanbox_pdm/.pdm.toml create mode 100644 unit_test/sanbox_pdm/main.py create mode 100644 unit_test/sanbox_pdm/pdm.lock create mode 100644 unit_test/sanbox_pdm/pyproject.toml create mode 100644 unit_test/sanbox_pdm/qpt2exe.py diff --git a/qpt/kernel/qinterpreter.py b/qpt/kernel/qinterpreter.py index 94163ce..6a92d63 100644 --- a/qpt/kernel/qinterpreter.py +++ b/qpt/kernel/qinterpreter.py @@ -1,8 +1,9 @@ # Author: Acer Zhang -# Datetime: 2021/5/26 +# Datetime: 2021/5/26 # Copyright belongs to the author. # Please indicate the source for reprinting. import os +import toml from collections import OrderedDict from qpt.kernel.qcode import intelligent_analysis, search_packages_dist_info, search_dep @@ -119,7 +120,8 @@ def __init__(self, if source is None: source = DEFAULT_PIP_SOURCE if pip_path: - pip_main = dynamic_load_package(packages_name="pip", lib_packages_path=pip_path).main + pip_main = dynamic_load_package( + packages_name="pip", lib_packages_path=pip_path).main else: from pip._internal.cli.main import main as pip_main self.pip_main = pip_main @@ -154,7 +156,8 @@ def pip_package_shell(self, opts = ArgManager() else: if not isinstance(opts, ArgManager): - Logging.warning(f"在键入以下指令时,opts传递的并非是ArgManager对象,可能存在意料之外的问题\n{opts}") + Logging.warning( + f"在键入以下指令时,opts传递的并非是ArgManager对象,可能存在意料之外的问题\n{opts}") # package兼容性 package = package.replace("-", "_") @@ -236,9 +239,11 @@ def analyze_dependence(self, action_mode=False): # action mode为静默模式 if save_file_path is None: - save_file_path = os.path.join(analyze_path, "requirements_with_opt.txt") + save_file_path = os.path.join( + analyze_path, "requirements_with_opt.txt") - requires, dep, ignore_requires = intelligent_analysis(analyze_path, return_all_info=True) + requires, dep, ignore_requires = intelligent_analysis( + analyze_path, return_all_info=True) with open(save_file_path, "w", encoding="utf-8") as req_file: req_file.write("# Here is the list of packages automatically derived by QPT\n" @@ -261,9 +266,11 @@ def analyze_dependence(self, dep_version = "" temp_write += f"#{dep_name}{dep_version}\n" - req_file.write("\n# ----------------------Ignored dependent packages---------------------\n") + req_file.write( + "\n# ----------------------Ignored dependent packages---------------------\n") for ignore_require_name, ignore_require_version in ignore_requires.items(): - req_file.write(f"#{ignore_require_name}=={ignore_require_version}\n") + req_file.write( + f"#{ignore_require_name}=={ignore_require_version}\n") req_file.write(temp_write) Logging.info(f"依赖分析完毕!\n" @@ -300,10 +307,39 @@ def analyze_requirements_file(file_path): f"报错信息如下:{e}") return requirements + @staticmethod + def analyze_pdm_requirements_file(file_path): + requirements = dict() + try: + pdm_config = toml.load(file_path) + pdm_config_project = pdm_config.get("project") + + if pdm_config_project is None: + raise Exception(f"{file_path}文件解析失败,project字段缺失\n" + f"报错信息如下:{e}") + + pdm_config_dependencies = pdm_config_project.get( + "dependencies") + + if pdm_config_dependencies is None: + raise Exception(f"{file_path}文件解析失败,dependencies字段缺失\n" + f"报错信息如下:{e}") + + for line in pdm_config_dependencies: + package, version, display = analysis_requirement_line(line) + requirements[package] = {"version": version, + "display": display_flag.get_flag(display), + "QPT_Flag": True if display else False} + except Exception as e: + raise Exception(f"{file_path}文件解析失败,文件可能被其他程序占用或格式异常\n" + f"报错信息如下:{e}") + return requirements + @staticmethod def save_requirements_file(requirements: dict, save_file_path, encoding="utf-8"): with open(save_file_path, "w", encoding=encoding) as file: - file.write("# -------This file is generated by QPT https://github.com/QPT-Family/QPT-----\n") + file.write( + "# -------This file is generated by QPT https://github.com/QPT-Family/QPT-----\n") for requirement, requirement_info in requirements.items(): version = requirement_info.get("version", "") display = requirement_info.get("display", "") @@ -341,8 +377,10 @@ def get_next_dep(dep_name: str, version=None): if sub_deps: for sub_dep in sub_deps.keys(): if sub_dep not in all_req: - get_next_dep(dep_name=sub_dep, version=packages_dist[sub_dep]) + get_next_dep(dep_name=sub_dep, + version=packages_dist[sub_dep]) for requirement in requirements: - get_next_dep(dep_name=requirement, version=requirements[requirement]) + get_next_dep(dep_name=requirement, + version=requirements[requirement]) return all_req diff --git a/qpt/modules/auto_requirements.py b/qpt/modules/auto_requirements.py index c1e8c15..7a7b1e3 100644 --- a/qpt/modules/auto_requirements.py +++ b/qpt/modules/auto_requirements.py @@ -30,14 +30,23 @@ def __init__(self, Logging.info(f"当前路径{os.path.abspath(path)}中不存在Requirements文件," f"请优先检查路径是否提供正确,必要时使用绝对路径") - if os.path.isfile(path): - Logging.info(f"正在读取{os.path.abspath(path)}下的依赖情况...") - requirements = QPT_MEMORY.pip_tool.analyze_requirements_file(path) + pdm_config_file = os.path.join(path, "pyproject.toml") + if os.path.isfile(pdm_config_file): + # 如果是pyproject.toml文件,则直接获取requirements + Logging.info( + f"[Auto]检测到{os.path.abspath(path)}为 pdm 项目,正在读取依赖情况...") + requirements = QPT_MEMORY.pip_tool.analyze_pdm_requirements_file( + pdm_config_file) else: - Logging.info(f"[Auto]正在分析{os.path.abspath(path)}下的依赖情况...") - requirements = QPT_MEMORY.pip_tool.analyze_dependence(path, - return_path=False, - action_mode=QPT_MEMORY.action_flag) + if os.path.isfile(path): + Logging.info(f"正在读取{os.path.abspath(path)}下的依赖情况...") + requirements = QPT_MEMORY.pip_tool.analyze_requirements_file( + path) + else: + Logging.info(f"[Auto]正在分析{os.path.abspath(path)}下的依赖情况...") + requirements = QPT_MEMORY.pip_tool.analyze_dependence(path, + return_path=False, + action_mode=QPT_MEMORY.action_flag) # ToDo 使用WhlDict代替dict + 在自动安装的时候,先考虑本地是否已经安装 # 获取所有Python依赖情况 diff --git a/qpt/version.py b/qpt/version.py index 2385d07..7c900c6 100644 --- a/qpt/version.py +++ b/qpt/version.py @@ -1 +1 @@ -version = "1.0b4.dev1" +version = "1.0b4.dev2" diff --git a/setup.py b/setup.py index 313cbfa..32c1dbc 100644 --- a/setup.py +++ b/setup.py @@ -23,6 +23,7 @@ "click", "pefile", "pillow", + "toml", "pip>=22.1.1"], python_requires='>3.5', include_package_data=True, diff --git a/unit_test/sanbox_pdm/.pdm.toml b/unit_test/sanbox_pdm/.pdm.toml new file mode 100644 index 0000000..9ffe801 --- /dev/null +++ b/unit_test/sanbox_pdm/.pdm.toml @@ -0,0 +1,2 @@ +[python] +path = "/home/sovlookup/.pyenv/shims/python3" diff --git a/unit_test/sanbox_pdm/main.py b/unit_test/sanbox_pdm/main.py new file mode 100644 index 0000000..b215a41 --- /dev/null +++ b/unit_test/sanbox_pdm/main.py @@ -0,0 +1,5 @@ +import toml + +project_config = toml.load("pyproject.toml") + +print(project_config.get("project").get("dependencies")) diff --git a/unit_test/sanbox_pdm/pdm.lock b/unit_test/sanbox_pdm/pdm.lock new file mode 100644 index 0000000..2e4243c --- /dev/null +++ b/unit_test/sanbox_pdm/pdm.lock @@ -0,0 +1,15 @@ +[[package]] +name = "toml" +version = "0.10.2" +requires_python = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" +summary = "Python Library for Tom's Obvious, Minimal Language" + +[metadata] +lock_version = "3.1" +content_hash = "sha256:7d858ebb81bd4c0b91fbf101a9bd245405906b651902aa4d06c5c40acbdc6724" + +[metadata.files] +"toml 0.10.2" = [ + {file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"}, + {file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"}, +] diff --git a/unit_test/sanbox_pdm/pyproject.toml b/unit_test/sanbox_pdm/pyproject.toml new file mode 100644 index 0000000..d241693 --- /dev/null +++ b/unit_test/sanbox_pdm/pyproject.toml @@ -0,0 +1,21 @@ +[project] +name = "" +version = "" +description = "" +authors = [ + {name = "sovlookup", email = "gonorth@qq.com"}, +] +dependencies = [ + "toml>=0.10.2", +] +requires-python = ">=3.8" +license = {text = "MIT"} + +[tool.pdm] + +[tool.pdm.scripts] +start = "python main.py" + +[build-system] +requires = ["pdm-pep517>=0.12.0"] +build-backend = "pdm.pep517.api" diff --git a/unit_test/sanbox_pdm/qpt2exe.py b/unit_test/sanbox_pdm/qpt2exe.py new file mode 100644 index 0000000..c429b69 --- /dev/null +++ b/unit_test/sanbox_pdm/qpt2exe.py @@ -0,0 +1,6 @@ +from qpt.executor import CreateExecutableModule + +module = CreateExecutableModule(work_dir="./", + launcher_py_path="./main.py", + save_path="./../out") +module.make()