-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
139 lines (105 loc) · 3.14 KB
/
build.py
File metadata and controls
139 lines (105 loc) · 3.14 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import zipfile
from datetime import datetime
import shutil
import hashlib
import sys
use_pto = "-pto" in sys.argv
SRC_DIR = "src"
MUSIC_DIR = os.path.join(SRC_DIR, "Music")
BUILDS_DIR = "builds"
LATEST_DIR = os.path.join(BUILDS_DIR, "latest")
PREVIOUS_DIR = os.path.join(BUILDS_DIR, "previous")
MUSIC_HASH_FILE = os.path.join(BUILDS_DIR, "music.hash")
if use_pto:
LATEST_DIR = os.path.join(BUILDS_DIR, "pto")
PREVIOUS_DIR = os.path.join(BUILDS_DIR, "pto-prev")
MUSIC_HASH_FILE = os.path.join(BUILDS_DIR, "pto-music.hash")
# =====================
# Utils
# =====================
def ensure_dirs():
os.makedirs(LATEST_DIR, exist_ok=True)
os.makedirs(PREVIOUS_DIR, exist_ok=True)
def move_old_builds():
for file in os.listdir(LATEST_DIR):
if file.endswith(".pk3"):
shutil.move(
os.path.join(LATEST_DIR, file),
os.path.join(PREVIOUS_DIR, file)
)
def zip_folder(folder_path, output_zip, exclude=None):
exclude = exclude or set()
with zipfile.ZipFile(output_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(folder_path):
for file in files:
full_path = os.path.join(root, file)
if any(full_path.startswith(e) for e in exclude):
continue
rel_path = os.path.relpath(full_path, folder_path)
zipf.write(full_path, rel_path)
# =====================
# Hashing
# =====================
def hash_music_folder():
hasher = hashlib.sha256()
if not os.path.exists(MUSIC_DIR):
return None
for root, _, files in os.walk(MUSIC_DIR):
for file in sorted(files):
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, MUSIC_DIR)
hasher.update(rel_path.encode())
with open(full_path, "rb") as f:
hasher.update(f.read())
return hasher.hexdigest()
def load_previous_music_hash():
if not os.path.exists(MUSIC_HASH_FILE):
return None
with open(MUSIC_HASH_FILE, "r") as f:
return f.read().strip()
def save_music_hash(hash_value):
with open(MUSIC_HASH_FILE, "w") as f:
f.write(hash_value)
# =====================
# Music PK3
# =====================
def build_music_pk3(output_zip):
with zipfile.ZipFile(output_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(MUSIC_DIR):
for file in files:
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, SRC_DIR)
zipf.write(full_path, rel_path)
init_lua = """\
-- Auto-generated init.lua
rawset(_G, "FH_PTO_BUILD", true)
"""
zipf.writestr("init.lua", init_lua)
# =====================
# Main
# =====================
timestamp = datetime.now().strftime("%Y-%m-%d---%H-%M-%S")
main_pk3 = f"FangsHeistRecode---{timestamp}.pk3"
main_output = os.path.join(LATEST_DIR, main_pk3)
ensure_dirs()
move_old_builds()
if use_pto:
# Main PK3 without music
zip_folder(
SRC_DIR,
main_output,
exclude={MUSIC_DIR}
)
# Hash check
new_hash = hash_music_folder()
old_hash = load_previous_music_hash()
if new_hash != old_hash:
music_pk3 = f"FangsHeistRecode-Music---{timestamp}.pk3"
music_output = os.path.join(BUILDS_DIR, music_pk3)
build_music_pk3(music_output)
save_music_hash(new_hash)
else:
# Normal single PK3 build
zip_folder(SRC_DIR, main_output)
print(main_output)