"""
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币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- java毕业设计-基于SSM框架的传统服饰文化体验平台【代码+部署教程】
- 优化领域的模拟退火算法详解与实战
- NewFileTime-x64.zip.fgpg
- 基于Python和HTML的Chinese-estate-helper房地产爬虫及可视化设计源码
- 基于SpringBoot2.7.7的当当书城Java后端设计源码
- 基于Python和Go语言的开发工具集成与验证设计源码
- 基于Python与JavaScript的国内供应商管理系统设计源码
- aspose.words-20.12-jdk17
- 基于czsc库的Python时间序列分析设计源码
- 基于Java、CSS、JavaScript、HTML的跨语言智联平台设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功