This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_updater.py
44 lines (38 loc) · 1.69 KB
/
template_updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import base64
import os
from pathlib import Path
from lib.utils import Downloader, HashUtils
from lib.file import FileCtrl
DEFAULT_MIRROR = 'https://gitee.com/kmou424/wfhelperpy_template/raw/main/'
PWD = os.getcwd()
TEMPLATE_PATH = PWD + '\\template'
TEMPLATE_HASH_FILENAME = 'template_hash.txt'
TEMPLATE_HASH_FILEPATH = TEMPLATE_PATH + '\\' + TEMPLATE_HASH_FILENAME
FileCtrl.checkDir(TEMPLATE_PATH)
if Path(TEMPLATE_HASH_FILEPATH).exists():
os.remove(TEMPLATE_HASH_FILEPATH)
Downloader.downloadFile(
url=DEFAULT_MIRROR + '/' + TEMPLATE_HASH_FILENAME,
filepath=TEMPLATE_HASH_FILEPATH
)
cnt = 0
with open(TEMPLATE_HASH_FILEPATH, 'r') as file:
all_lines = file.readlines()
for line in all_lines:
if line != '' and line is not None:
line = base64.b64decode(line.encode('utf-8')).decode('utf-8')
cnt += 1
name_hash = line.split(' ')
path = TEMPLATE_PATH + '\\' + name_hash[0]
print('[{cnt}] Checking file {filename}'.format(cnt=cnt, filename=name_hash[0]))
if not Path(path).exists():
print('[Error] File is missing, will download it for now')
FileCtrl.checkDir(os.path.dirname(path))
Downloader.downloadFile(DEFAULT_MIRROR + name_hash[0].replace('\\', '/'), path)
else:
if name_hash[1] != HashUtils.getFileHash(path):
print('[Warning] This file has an update, will download it for now')
os.remove(path)
Downloader.downloadFile(DEFAULT_MIRROR + name_hash[0].replace('\\', '/'), path)
else:
print('[Info] OK')