pytest-jequnauto/utils/other_tools/install_tool/install_requirements.py

92 lines
3.0 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
# @Time : 2022/5/10 14:02
# @Author : 余少琪
# @Email : 1603453211@qq.com
# @File : install_requirements
# @describe: 判断程序是否每次会更新依赖库,如有更新,则自动安装
"""
import os
import chardet
from common.setting import ensure_path_sep
from utils.logging_tool.log_control import INFO
from utils import config
os.system("pip3 install chardet")
class InstallRequirements:
""" 自动识别安装最新的依赖库 """
def __init__(self):
self.version_library_comparisons_path = ensure_path_sep("\\utils\\other_tools\\install_tool\\") \
+ "version_library_comparisons.txt"
self.requirements_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) \
+ os.sep + "requirements.txt"
self.mirror_url = config.mirror_source
# 初始化时,获取最新的版本库
# os.system("pip freeze > {0}".format(self.requirements_path))
def read_version_library_comparisons_txt(self):
"""
获取版本比对默认的文件
@return:
"""
with open(self.version_library_comparisons_path, 'r', encoding="utf-8") as file:
return file.read().strip(' ')
@classmethod
def check_charset(cls, file_path):
"""获取文件的字符集"""
with open(file_path, "rb") as file:
data = file.read(4)
charset = chardet.detect(data)['encoding']
return charset
def read_requirements(self):
"""获取安装文件"""
file_data = ""
with open(
self.requirements_path,
'r',
encoding=self.check_charset(self.requirements_path)
) as file:
for line in file:
if "" in line:
line = line.replace("", "")
file_data += line
with open(
self.requirements_path,
"w",
encoding=self.check_charset(self.requirements_path)
) as file:
file.write(file_data)
return file_data
def text_comparison(self):
"""
版本库比对
@return:
"""
read_version_library_comparisons_txt = self.read_version_library_comparisons_txt()
read_requirements = self.read_requirements()
if read_version_library_comparisons_txt == read_requirements:
INFO.logger.info("程序中未检查到更新版本库,已为您跳过自动安装库")
# 程序中如出现不同的文件,则安装
else:
INFO.logger.info("程序中检测到您更新了依赖库,已为您自动安装")
os.system(f"pip3 install -r {self.requirements_path}")
with open(self.version_library_comparisons_path, "w",
encoding=self.check_charset(self.requirements_path)) as file:
file.write(read_requirements)
if __name__ == '__main__':
InstallRequirements().text_comparison()