Skip to content

Commit

Permalink
Merge pull request #74 from SOVLOOKUP/开发分支
Browse files Browse the repository at this point in the history
兼容 pdm 包管理器创建的项目,并添加测试
  • Loading branch information
GT-ZhangAcer authored Jul 7, 2022
2 parents 2672328 + ddbb2f7 commit f7ff52c
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 18 deletions.
58 changes: 48 additions & 10 deletions qpt/kernel/qinterpreter.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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("-", "_")
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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", "")
Expand Down Expand Up @@ -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
23 changes: 16 additions & 7 deletions qpt/modules/auto_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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依赖情况
Expand Down
2 changes: 1 addition & 1 deletion qpt/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.0b4.dev1"
version = "1.0b4.dev2"
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"click",
"pefile",
"pillow",
"toml",
"pip>=22.1.1"],
python_requires='>3.5',
include_package_data=True,
Expand Down
2 changes: 2 additions & 0 deletions unit_test/sanbox_pdm/.pdm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[python]
path = "/home/sovlookup/.pyenv/shims/python3"
5 changes: 5 additions & 0 deletions unit_test/sanbox_pdm/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import toml

project_config = toml.load("pyproject.toml")

print(project_config.get("project").get("dependencies"))
15 changes: 15 additions & 0 deletions unit_test/sanbox_pdm/pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions unit_test/sanbox_pdm/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions unit_test/sanbox_pdm/qpt2exe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from qpt.executor import CreateExecutableModule

module = CreateExecutableModule(work_dir="./",
launcher_py_path="./main.py",
save_path="./../out")
module.make()

0 comments on commit f7ff52c

Please sign in to comment.