Skip to content

Commit

Permalink
feat: 改用pypi获取元信息
Browse files Browse the repository at this point in the history
  • Loading branch information
MarleneJiang committed Aug 28, 2023
1 parent f3eb8a0 commit 51178d5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 78 deletions.
38 changes: 8 additions & 30 deletions .github/actions_scripts/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,7 @@
import re
from typing import Any

import requests
from utils import set_action_outputs

BASE_URL = "https://pypi.org/pypi"
CODE = 404


def get_response(name: str) -> dict[str, Any] | None:
"""Request response from PyPi API."""
target_url = f"{BASE_URL}/{name}/json"
response = requests.get(target_url, timeout=5)
if response.status_code == CODE:
return None
return response.json()


def check_pypi(name: str) -> bool:
"""Check module filename for conflict."""
if name == "null":
return False
response = get_response(name)

if response:
module_name = response.get("info", {}).get("package_url", "").split("/")[-2]
return name.lower() == module_name.lower()

return False
from utils import Pypi, set_action_outputs


def parse_title(title: str) -> dict[str, Any]:
Expand All @@ -43,15 +17,19 @@ def parse_title(title: str) -> dict[str, Any]:
msg = "标题格式错误"
raise ValueError(msg)

def raise_value_error(msg: str) -> None:
"""Raise ValueError."""
raise ValueError(msg)

def main() -> None:
"""信息解析:1. 标题的type解析,如果不符合就报错 2. 提取name、module_name、pypi_name,如果不符合就报错 3. pypi_name在pip网站中检查,不存在则报错."""
title = os.environ["TITLE"]
pypi_name = os.environ["PYPI_NAME"]
try:
if check_pypi(pypi_name) is False:
set_action_outputs({"result": "error", "output": "输入的pypi_name存在问题"})
return
pypi = Pypi(pypi_name)
if pypi.check_pypi(pypi_name) is False:
msg = "输入的pypi_name存在问题"
raise_value_error(msg)
parsed = parse_title(title)
set_action_outputs(
{
Expand Down
59 changes: 11 additions & 48 deletions .github/actions_scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import subprocess

from utils import set_action_outputs
from utils import Pypi, set_action_outputs

PYPI_NAME = os.environ["PYPI_NAME"]
MODULE_NAME = os.environ["MODULE_NAME"]
Expand Down Expand Up @@ -51,51 +51,14 @@ def alicebot_test() -> None:

def get_meta_info() -> None:
"""获取模块的元信息."""
from importlib.metadata import metadata

metadata = metadata(PYPI_NAME)
name = metadata.get_all("Name")
if name is None:
set_action_outputs({"result": "error", "output": "模块名称获取失败"})
return
description = metadata.get_all("Summary")
if description is None:
set_action_outputs({"result": "error", "output": "模块描述获取失败"})
return
author = metadata.get_all("Author")
if author is None:
email = metadata.get_all("Author-email")
if email is not None and "<" in email[0]: # PDM发包问题
author = [email[0].split("<")[0].strip()]
else:
set_action_outputs({"result": "error", "output": "作者信息获取失败"})
return
license_info = metadata.get_all("License")
if license_info is None:
license_info = [""]
homepage = metadata.get_all("Home-page")
if homepage is None:
homepage = [""]
tags = metadata.get_all("Keywords")
if tags is None:
set_action_outputs({"result": "error", "output": "标签信息获取失败"})
return
tags = metadata.get_all("Keywords")
if tags is None:
set_action_outputs({"result": "error", "output": "标签信息获取失败"})
return
set_action_outputs(
{
"result": "success",
"output": "模块元信息获取成功",
"name": name[0],
"description": description[0],
"author": author[0],
"license": license_info[0],
"homepage": homepage[0],
"tags": str(tags),
}
)
try:
pypi = Pypi(PYPI_NAME)
data = pypi.get_info()
data["result"] = "success"
data["output"] = "获取module元信息成功"
set_action_outputs(data)
except ValueError as e:
set_action_outputs({"result": "error", "output": str(e)})


if __name__ == "__main__":
Expand All @@ -105,7 +68,7 @@ def get_meta_info() -> None:
try:
if TYPE!="bot":
alicebot_test()
except Exception: # noqa: BLE001
set_action_outputs({"result": "error", "output": "无法在alicebot中正常运行"})
except ValueError as e:
set_action_outputs({"result": "error", "output": str(e)})
else:
get_meta_info()
75 changes: 75 additions & 0 deletions .github/actions_scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import os
from pathlib import Path
from typing import Any

import requests


def set_action_outputs(output_pairs: dict[str, str]) -> None:
Expand All @@ -14,3 +17,75 @@ def set_action_outputs(output_pairs: dict[str, str]) -> None:
else:
for key, value in output_pairs.items():
print(f"::set-output name={key}::{value}") # noqa: T201

class Pypi :
"""获取pypi info."""

BASE_URL = "https://pypi.org/pypi"
CODE = 404
name = None

def __init__(self, name: str) -> None:
"""Init."""
self.name = name
self.data = self.get_response(name).get("info", {})
def get_response(self,name: str) -> dict[str, Any]:
"""Request response from PyPi API."""
target_url = f"{self.BASE_URL}/{name}/json"
response = requests.get(target_url, timeout=5)
if response.status_code == self.CODE:
msg = "pypi_name检查出错"
raise ValueError(msg)
return response.json()
def get(self,key:str) -> str | None:
"""."""
if self.data == {}:
msg = "module pypi info为空"
raise ValueError(msg)
return self.data.get(key, None)
def check_pypi(self,name: str) -> bool:
"""Check module filename for conflict."""
if name == "null":
return False
package_url = self.get("package_url")
if package_url is not None:
module_name = package_url.split("/")[-2]
return name.lower() == module_name.lower()
return False

def get_info(self)->dict[str,str]:
"""."""
name = self.get("name")
if name is None:
msg = "模块名称获取失败"
raise ValueError(msg)
description = self.get("summary")
if description is None:
msg = "模块描述获取失败"
raise ValueError(msg)
author = self.get("author")
if author is None:
email = self.get("author_email")
if email is not None and "<" in email[0]: # PDM发包问题
author = email[0].split("<")[0].strip()
else:
msg = "作者信息获取失败"
raise ValueError(msg)
license_info = self.get("license")
if license_info is None:
license_info = ""
homepage = self.get("home_page")
if homepage is None:
homepage = ""
tags = self.get("keywords")
if tags is None:
msg = "标签信息获取失败"
raise ValueError(msg)
return {
"name": name,
"description": description,
"author": author,
"license": license_info,
"homepage": homepage,
"tags": tags,
}

0 comments on commit 51178d5

Please sign in to comment.