From 12320e2a5927b9c407f22009542e13120362d2fd Mon Sep 17 00:00:00 2001 From: Xtao_dada Date: Sun, 19 Dec 2021 00:22:44 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20auto=20install=20extra=20?= =?UTF-8?q?pypi=20packages.=20(#158)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 支持自动安装额外的 pypi 扩展包 --- .gitignore | 1 + pagermaid/__init__.py | 16 ++++------------ pagermaid/utils.py | 18 +++++++++++++++++- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 38af8a1..5526fd6 100644 --- a/.gitignore +++ b/.gitignore @@ -127,6 +127,7 @@ dump.rdb keyword.list requirements2.txt languages/custom.yml +unknown_errors.txt # Spyder project settings .spyderproject diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index 24a4264..9ddad02 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -6,13 +6,7 @@ from concurrent.futures import CancelledError import sentry_sdk from sentry_sdk.integrations.redis import RedisIntegration -python36 = True -try: - from asyncio.exceptions import CancelledError as CancelError - - python36 = False -except: - pass +from asyncio import CancelledError as CancelError from subprocess import run, PIPE from datetime import datetime from time import time @@ -279,7 +273,8 @@ def before_send(event, hint): AlreadyInConversationError, ConnectedError, KeyboardInterrupt, OSError, AuthKeyDuplicatedError, ResponseError, SlowModeWaitError, PeerFloodError, MessageEditTimeExpiredError, PeerIdInvalidError, - AuthKeyUnregisteredError, UserBannedInChannelError, AuthKeyError)): + AuthKeyUnregisteredError, UserBannedInChannelError, AuthKeyError, + CancelError)): return elif exc_info and isinstance(exc_info[1], UserDeactivatedBanError): # The user has been deleted/deactivated @@ -288,9 +283,6 @@ def before_send(event, hint): except Exception as exc: print(exc) exit(1) - if not python36: - if exc_info and isinstance(exc_info[1], CancelError): - return if time() <= report_time + 30: report_time = time() return @@ -303,7 +295,7 @@ report_time = time() start_time = datetime.utcnow() git_hash = run("git rev-parse HEAD", stdout=PIPE, shell=True).stdout.decode() sentry_sdk.init( - "https://ae2b937dba4a4f948f13ae1c902b30a3@o416616.ingest.sentry.io/5312335", + "https://88b676b38216473db3eb3dd5e1da0133@o416616.ingest.sentry.io/5312335", traces_sample_rate=1.0, release=git_hash, before_send=before_send, diff --git a/pagermaid/utils.py b/pagermaid/utils.py index 08a2f6e..213d273 100644 --- a/pagermaid/utils.py +++ b/pagermaid/utils.py @@ -1,9 +1,13 @@ """ Libraries for python modules. """ import aiohttp +import subprocess + +from importlib.util import find_spec +from sys import executable from os import remove from os.path import exists -from typing import Any +from typing import Any, Optional from emoji import get_emoji_regexp from random import choice @@ -214,6 +218,18 @@ def clear_emojis(target: str) -> str: return get_emoji_regexp().sub(u'', target) +def pip_install(package: str, version: Optional[str] = "", alias: Optional[str] = "") -> bool: + """ Auto install extra pypi packages """ + if not alias: + # when import name is not provided, use package name + alias = package + if find_spec(alias) is None: + subprocess.call([executable, "-m", "pip", "install", f"{package}{version}"]) + if find_spec(package) is None: + return False + return True + + async def admin_check(event): if event.is_private: return False