"""
Copyright 2020
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# standard library
import logging
import os
import pty
from logging import getLogger
from os import PathLike
from pathlib import Path
from select import select
from subprocess import PIPE, Popen
from typing import Union
# pypi library
import click
# Support extension
SUPPORT_TYPE = (".txt", ".rst")
# Logger settings
handler = logging.StreamHandler()
logger = getLogger("console")
logger.addHandler(handler)
logger.setLevel(level="ERROR")
def read_requirments(ctx, req_path: Union[str, PathLike]) -> str:
logger.debug(f"[DEBUG] Get input path: {req_path}")
path: PathLike = Path(req_path)
cwd: PathLike = Path().cwd()
if not path.is_file():
click.secho(
f"FileNotFoundError: Cannot find requirement.txt at {path.resolve()}",
fg="red",
bold=True,
)
ctx.abort()
elif not (cwd / "pyproject.toml").is_file() and not toml_at_root():
click.secho(
f"FileNotFoundError: Cannot find pyproject.toml at current folder, perhaps `poetry init` first?",
fg="red",
bold=True,
)
ctx.abort()
elif path.suffix not in SUPPORT_TYPE:
click.secho(
f"FileNotSupportError: extension '{path.suffix}' is not a supported type"
)
ctx.abort()
logger.debug(f"[DEBUG] Found requirements.txt and pyproject.toml!")
with open(path, "r") as file:
deps = file.readlines()
return [f"{dep.strip().split(';')[0]}" for dep in deps]
def inpty(ctx, cmd, env=None, log=None):
"""
execute command and use select + pty to pull process stdout and stderr
to file-object 'log' while simultaneously logging to stdout
"""
try:
exec_env = {}
exec_env.update(os.environ)
# copy the OS environment into our local environment
if env is not None:
exec_env.update(env)
# create a pipe to receive stdout and stderr from process
master, slave = pty.openpty()
p = Popen(cmd, shell=False, env=exec_env, stdout=slave, stderr=slave)
# Loop while the process is executing
while p.poll() is None:
# Loop long as the selct mechanism indicates there
# is data to be read from the buffer
while len(select([master], [], [], 0)[0]) == 1:
# Read up to a 1 KB chunk of data
buf = os.read(master, 1024)
# Stream data to our stdout's fd of 0
os.write(0, buf)
if log is not None:
log.write(buf)
except Exception:
ctx.abort()
else:
return p.returncode
finally:
# cleanup
os.close(master)
os.close(slave)
def toml_at_root():
try:
root = Popen(["git", "rev-parse", "--show-toplevel"], stdout=PIPE, stderr=PIPE)
root = root.stdout.decode().strip()
return True if (Path(root) / "pyproject.toml").is_file() else False
except Exception:
return False
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源分类:Python库 所属语言:Python 资源全名:req2toml-1.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
req2toml-1.1.0.tar.gz (7个子文件)
req2toml-1.1.0
PKG-INFO 684B
pyproject.toml 855B
req2toml
utils.py 3KB
__init__.py 557B
console.py 2KB
LICENSE 11KB
setup.py 726B
共 7 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 2004-2023年上市公司战略激进度数据(含原始数据+计算代码+计算结果).zip
- 全面指南:Django框架资源大全,助力高效学习与应用
- Mac苹果签名软件.zip
- SharpXFileParser 是用 C# 编写的 DirectX X 文件 (.x) 解析器 .zip
- Windows Update Blocker v1.7.0中文版.zip
- Win版本苹果签名软件.zip
- Seeing# 是一个由 Direct3D 提供支持的 C# 3D,2D 渲染库 它适用于桌面应用程序(Win.Forms、Wpf、WinUI)或 Windows Store 应用.zip
- SEED 的项目 PICA PICA 创建过程中使用的资产.zip
- 计算机组装模拟软件.zip
- sdkmesh 解码器.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功