-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_patch.py
More file actions
94 lines (78 loc) · 3.21 KB
/
install_patch.py
File metadata and controls
94 lines (78 loc) · 3.21 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pickle
from tempfile import TemporaryDirectory
import shutil
import traceback
import os
from pathlib import Path
from make_patch import INSTRUCTION_FILENAME, get_folders_by_depth
from rimlink.rimlink import HashStructure
import time
def install_patch(patch_file: str):
print("Starting Patch installation")
assert ".zip" in patch_file
with TemporaryDirectory() as temp_dir:
temp_folder_path = Path(temp_dir)
print("Unpacking patch")
shutil.unpack_archive(patch_file, temp_folder_path)
print("Patch unpacked")
instruction_file_path = Path(temp_folder_path, INSTRUCTION_FILENAME)
print("Copying over folders")
with open(instruction_file_path, "rb") as instruction_file:
instructions = pickle.load(instruction_file)
assert isinstance(instructions, dict)
assert [isinstance(x, list) for x in instructions.values()]
add_folders = get_folders_by_depth(instructions["add"])
for depth in sorted(add_folders.keys()):
for add_folder in add_folders[depth]:
assert isinstance(add_folder, HashStructure), type(add_folder)
if add_folder.relativePath() == "":
continue
try:
os.mkdir(add_folder.relativePath())
except FileExistsError:
pass
print("Folders copied over")
print("Copying over files")
for new_file in instructions["add"]:
assert isinstance(new_file, HashStructure)
if not new_file.file:
continue
new_file_current_path = Path(temp_folder_path, new_file.relativePath())
shutil.copy(new_file_current_path, new_file.relativePath())
print("Now doing files overwrites")
for new_file in instructions["modify"]:
assert isinstance(new_file, HashStructure)
if not new_file.file:
continue
os.remove(new_file.relativePath())
new_file_current_path = Path(temp_folder_path, new_file.relativePath())
shutil.copy(new_file_current_path, new_file.relativePath())
print("Done copying over all files")
print("Now deleting old files")
for delete_file in instructions["delete"]:
assert isinstance(delete_file, HashStructure)
if delete_file.file:
try:
os.remove(delete_file.relativePath())
except FileNotFoundError:
pass
print("Now deleting old folders")
for delete_file in instructions["delete"]:
assert isinstance(delete_file, HashStructure)
if not delete_file.file:
try:
shutil.rmtree(delete_file.relativePath())
except Exception:
pass
print("Done deleting old files and folders")
print("Patch finished installing")
if __name__ == "__main__":
try:
install_patch("patch.zip")
except Exception as e:
print(e)
traceback.print_exc()
print("Execution Failed!")
print("Execution complete")
while True:
time.sleep(60)