import hashlib
import os
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/pyProject/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)
m = hashlib.md5(msg1.encode())
return m.hexdigest()
def main(self):
requi_file_md5 = self.md5(self.read_file(self.requi_file))
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)
print("安装完成")
else:
print("未发现缓存文件,开始安装依赖模块。")
os.system(self.command)
self.write_file(self.local_file, requi_file_md5)
if __name__ == '__main__':
ir = InstallRequire()
ir.main()