"""
1、获取
"""
import hashlib
import os
# 判断:是取用win电脑还是Mac电脑,取家目录
user_home = os.environ['USERPROFILE'] if os.environ.get('USERPROFILE') else os.environ.get('HOME')
class InstallRequire:
local_file = os.path.join(user_home, '.cache') # 存放本地缓存文件
requi_file = '/Users/ray/PycharmProjects/tmz/day9/requirements.txt'
command = 'pip install -r requirements.txt'
@property # 负责把一个方法变成属性调用
def check_local_file(self):
"""查找文件"""
if os.path.exists(self.local_file):
return True
@staticmethod # 静态方法,不止在该处可以调用,任意地方都可以调用
def read_file(file_name):
"""读文件"""
with open(file_name, encoding='utf-8') as fr:
return fr.read()
@staticmethod # 静态方法,不止在该处可以调用,任意地方都可以调用
def write_file(file_name, content):
"""写文件"""
with open(file_name, 'w', encoding='utf-8') as fw:
return fw.write(content)
@staticmethod # 静态方法,不止在该处可以调用,任意地方都可以调用
def md5(msg):
"""进行md5加密"""
msg1 = str(msg) # 将传的值转换为str类型
m = hashlib.md5(msg1.encode()) # encode:转换编码格式 bytes:不可逆
return m.hexdigest()
def main(self): # 串起流程
requi_file_md5 = self.md5(self.read_file(self.requi_file)) # 将文件进行md5加密
if self.check_local_file: # 如果查找到文件,就执行下面代码
local_file_md5 = self.read_file(self.local_file)
if local_file_md5 == requi_file_md5:
print("两个缓存文件一致,无需安装依赖模块")
else:
print("两个缓存文件不一致,开始安装")
os.system(self.command)
self.write_file(self.local_file, requi_file_md5) # 生成新的md5更新到缓存文件
print("安装完成")
else: # 如果没查找到文件,就执行安装依赖,安装完成后生成一个本地缓存文件
print("未发现缓存文件,开始安装依赖模块。")
os.system(self.command)
self.write_file(self.local_file, requi_file_md5)
if __name__ == '__main__':
ir = InstallRequire()
ir.main()
网友评论