forked from twohoursonelife/OneLifeData7
-
Notifications
You must be signed in to change notification settings - Fork 2
/
readableAssets.py
309 lines (228 loc) · 10 KB
/
readableAssets.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
import sys
import uuid
from pathlib import Path
from hashlib import sha256
### inputs
inAction = os.getenv("GITHUB_ACTIONS") is not None
if inAction:
REPO = os.environ.get("REPO")
PR_NUM = os.environ.get("PRNUM")
COMMIT_A = os.environ.get("COMMIT_A")
COMMIT_B = os.environ.get("COMMIT_B")
else:
args = sys.argv
helpMsg = f"""
Parse asset changes to be more readable.
Usage:
python {args[0]} <commit_SHA_A> <commit_SHA_B>
If no commit_SHA is provided, the script defaults to HEAD~ and HEAD.
If one commit_SHA is provided, the script defaults to commit_SHA~ and commit_SHA.
"""
if len(args) > 3:
print(helpMsg)
sys.exit()
elif len(args) == 3:
COMMIT_A = args[1]
COMMIT_B = args[2]
elif len(args) == 2:
COMMIT_B = args[1]
COMMIT_A = COMMIT_B + "~"
else:
print(helpMsg)
response = input("Input Y to go with the default, diff-ing HEAD~ and HEAD.")
if response != "Y": sys.exit()
COMMIT_A = "HEAD~"
COMMIT_B = "HEAD"
print("\n")
### util
def read_txt(path):
with open(path, 'r', encoding='utf-8-sig') as f:
text = f.read()
return text
def read_file_even_deleted(path):
if not Path(path).is_file():
recovered_file_content = run_command(f"git show {COMMIT_A}:{path}")
return recovered_file_content
return read_txt(path)
def run_command(command):
f = os.popen(command)
output = f.read()
status = f.close()
if status:
sys.exit(1)
return output
def set_multiline_output(name, value):
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
delimiter = uuid.uuid1()
print(f'{name}<<{delimiter}', file=fh)
print(value, file=fh)
print(delimiter, file=fh)
### diff-ing the changes
changes_all, changes_deleted, changes_added, renamed_pairs = [], [], [], []
commands = [
f"git diff --name-only --no-renames {COMMIT_A} {COMMIT_B}",
f"git diff --diff-filter=D --name-only --no-renames {COMMIT_A} {COMMIT_B}",
f"git diff --diff-filter=A --name-only --no-renames {COMMIT_A} {COMMIT_B}",
f"git diff --name-status {COMMIT_A} {COMMIT_B}"
]
changes = []
for command in commands:
output = run_command(command)
output_list = []
if output != "":
output_list = output.strip().split("\n")
changes.append(output_list)
changes_all, changes_deleted, changes_added, renamed_pairs = changes
if renamed_pairs:
renamed_pairs = [e.split("\t") for e in renamed_pairs if e[0] == "R"]
renamed_before = [e[1] for e in renamed_pairs]
renamed_after = [e[2] for e in renamed_pairs]
### so we only read each object once
objects_dict = {}
def get_object_name_by_id(object_id):
if object_id <= 0: return str(object_id)
if object_id in objects_dict.keys(): return objects_dict[object_id]
object_path = f"objects/{object_id}.txt"
object_file_content = read_file_even_deleted( object_path ).splitlines()
if len(object_file_content) < 2:
object_name = str(object_id)
else:
object_name = object_file_content[1]
objects_dict[object_id] = object_name
return object_name
def read_category_as_object_list(content):
list_str = content[content.find("\n", content.find("numObjects="))+1:].splitlines()
list_int = [int(e.split()[0]) for e in list_str]
return list_int
### go through the changed files, parse object IDs into object names
object_lines, transition_lines, category_lines, other_lines = [], [], [], []
for changed_file in changes_all:
# whether the change is an added file, or a deleted one, or a modified one
# note that renamed files are configured to show as deleted of old and added of new
sign = "."
if changed_file in changes_added:
sign = "+"
elif changed_file in changes_deleted:
sign = "-"
if inAction:
sign = f"`{sign}`"
# the hash is used to link to the changed file on github site
file_hash = 0
if changed_file in renamed_before:
index = renamed_before.index(changed_file)
file_hash = sha256(renamed_after[index].encode('utf-8')).hexdigest()
else:
file_hash = sha256(changed_file.encode('utf-8')).hexdigest()
change_processed = False
if 'objects/' in changed_file or 'categories/' in changed_file:
id_str = changed_file.replace("objects/","").replace("categories/","").replace(".txt","").strip()
if id_str.isnumeric():
object_id = int(id_str)
object_name = get_object_name_by_id(object_id)
object_name = object_name.replace("#", "<span>#</span>")
if inAction:
if 'categories/' in changed_file:
category_before, category_after = [], []
if changed_file not in changes_added:
category_before_output = run_command(f"git show {COMMIT_A}:{changed_file}")
category_before = read_category_as_object_list(category_before_output)
if changed_file not in changes_deleted:
category_after_output = run_command(f"git show {COMMIT_B}:{changed_file}")
category_after = read_category_as_object_list(category_after_output)
added = [e for e in category_after if e not in category_before]
removed = [e for e in category_before if e not in category_after]
category_details = ""
if len(added) > 0:
category_details += "\n".join( [ f"+ {e} {get_object_name_by_id(e)}" for e in added] )
if len(removed) > 0:
category_details += "\n".join( [ f"- {e} {get_object_name_by_id(e)}" for e in removed] )
line = f"""
{sign} [{object_id}](https://github.com/{REPO}/pull/{PR_NUM}/files#diff-{file_hash}) {object_name}
<details>
<summary>Details</summary>
```diff
{category_details}
```
</details>
"""
else:
line = f"{sign} [{object_id}](https://github.com/{REPO}/pull/{PR_NUM}/files#diff-{file_hash}) {object_name}"
else:
line = f"{sign} {object_id} {object_name}"
if 'objects/' in changed_file:
object_lines.append(line)
elif 'categories/' in changed_file:
category_lines.append(line)
change_processed = True
elif 'transitions/' in changed_file:
transition_file_content = read_file_even_deleted( changed_file )
transition_file_content_parts = transition_file_content.split()
filename_parts = changed_file.replace("transitions/","").replace(".txt","").split("_")
a = int(filename_parts[0])
b = int(filename_parts[1])
c = int(transition_file_content_parts[0])
d = int(transition_file_content_parts[1])
flag = ""
if len(filename_parts) > 2: flag = "_".join(filename_parts[2:])
a_name = get_object_name_by_id(a)
b_name = get_object_name_by_id(b)
c_name = get_object_name_by_id(c)
d_name = get_object_name_by_id(d)
fields = [
"a", "b", "c", "d",
"flag",
"autoDecaySeconds",
"actorMinUseFraction",
"targetMinUseFraction",
"reverseUseActorFlag",
"reverseUseTargetFlag",
"move",
"desiredMoveDist",
"noUseActorFlag",
"noUseTargetFlag"
]
trans_keyValuePairs = []
trans_keyValuePairs.append( (fields[0], a) )
trans_keyValuePairs.append( (fields[1], b) )
trans_keyValuePairs.append( (fields[2], c) )
trans_keyValuePairs.append( (fields[3], d) )
trans_keyValuePairs.append( (fields[4], flag) )
for i in range(2, len(transition_file_content_parts)):
trans_keyValuePairs.append( (fields[i+3], transition_file_content_parts[i]) )
if flag != "": flag = f"({flag})"
if inAction:
transition_details = "\n".join( [ f"{e[0]}: {e[1]}" for e in trans_keyValuePairs] )
transition_line = f"""
{sign} [{a} + {b} = {c} + {d} {flag}](https://github.com/{REPO}/pull/{PR_NUM}/files#diff-{file_hash})
<details>
<summary><code class="notranslate">{a_name}</code> + <code class="notranslate">{b_name}</code> = <code class="notranslate">{c_name}</code> + <code class="notranslate">{d_name}</code></summary>
```yaml
{transition_details}
```
</details>
"""
else:
transition_line = f"{sign} {a} + {b} = {c} + {d} {flag}\n{a_name} + {b_name} = {c_name} + {d_name} {flag}\n\n"
transition_lines.append(transition_line)
change_processed = True
if not change_processed:
if inAction:
line = f"{sign} [link](https://github.com/{REPO}/pull/{PR_NUM}/files#diff-{file_hash}) {changed_file}"
else:
line = f"{sign} {changed_file}"
other_lines.append(line)
### assemble the output message
message = ""
if len(object_lines) > 0:
message += "## Objects:\n\n" + "\n".join(object_lines) + "\n\n"
if len(category_lines) > 0:
message += "## Categories:\n\n" + "\n".join(category_lines) + "\n\n"
if len(transition_lines) > 0:
message += "## Transitions:\n\n" + "".join(transition_lines) + "\n\n"
if len(other_lines) > 0:
message += "## Others:\n\n" + "\n".join(other_lines)
if inAction:
set_multiline_output("OUTPUT_MESSAGE", message)
else:
print(message)