-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathxcactivitylog.py
144 lines (119 loc) · 4.13 KB
/
xcactivitylog.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
from enum import Enum
import os
import subprocess
import struct
"""this file use to convert xcactivitylog. ignore it's structure, only extract pattern log string out"""
class TokenType(Enum):
Null = 0
String = 1
Integer = 2
Double = 3
Array = 4
Class = 5
Instance = 6
Json = 7
def tokenizer(path):
# pipe byte stream
process = subprocess.Popen(["gunzip", "--stdout", path], stdout=subprocess.PIPE)
buffer = bytearray()
assert process.stdout
head = process.stdout.read(4)
if head != b"SLF0":
raise ValueError(f"invalid file {path}, should be a xcactivitylog")
def null_handler(index):
del buffer[: index + 1]
return (TokenType.Null, None)
def int_handler(type):
def handler(index):
v = int(buffer[:index])
del buffer[: index + 1]
return (type, v)
return handler
def double_handler(index):
v = buffer[:index]
v = bytes.fromhex(v.decode())
v = struct.unpack("<d", v)[0]
del buffer[: index + 1]
return (TokenType.Double, v)
def str_handler(type):
def handler(index):
length = int(buffer[:index])
start = index + 1
available = len(buffer) - start
if length > available:
bstr = buffer[start:]
bstr += process.stdout.read(length - available) # type: ignore
del buffer[:]
else:
bstr = buffer[start : (length + start)]
del buffer[: start + length]
return (type, bstr.decode())
return handler
handler_map = {
ord(b'"'): str_handler(TokenType.String),
ord(b"-"): null_handler,
ord(b"#"): int_handler(TokenType.Integer),
ord(b"^"): double_handler,
ord(b"("): int_handler(TokenType.Array),
ord(b"%"): str_handler(TokenType.Class),
ord(b"@"): int_handler(TokenType.Instance),
ord(b"*"): str_handler(TokenType.Json),
}
i = 0
while v := process.stdout.read(16):
buffer.extend(v)
l = len(buffer)
while i < l:
v = buffer[i]
if handler := handler_map.get(v):
yield handler(i)
l = len(buffer)
i = 0 # consume and reset
else:
i += 1
def extract_compile_log(path):
for type, value in tokenizer(path):
# print(type, value)
if type != TokenType.String:
continue
assert isinstance(value, str)
if not value.startswith(
(
"CompileSwiftSources ",
# xcode will emit SwiftDriver and SwiftDriver\\ Compilation.
# in my test, when build fail, will only emit SwiftDriver.
# but in some dependency case, only emit SwiftDriver\\ Compilation .
# so keep both. final will overwrite it
"SwiftDriver ",
"SwiftDriver\\ Compilation ",
"CompileC ",
"ProcessPCH",
)
):
continue
lines = value.splitlines()
if len(lines) > 1:
yield from iter(lines)
yield "" # a empty line means section log end
def newest_logpath(metapath: str, scheme=None):
"""returns None if no metapath or no logpath"""
if not os.path.exists(metapath):
return None
import plistlib
with open(metapath, "rb") as f:
meta = plistlib.load(f)
if scheme:
valid = lambda v: v["schemeIdentifier-schemeName"] == scheme
else:
valid = bool
logs = [v for v in meta["logs"].values() if valid(v)]
if not logs:
return None
logs.sort(key=lambda v: v["timeStoppedRecording"], reverse=True)
return os.path.join(os.path.dirname(metapath), logs[0]["fileName"])
def metapath_from_buildroot(build_root):
return os.path.join(build_root, "Logs/Build/LogStoreManifest.plist")
# def play():
# newest_logpath("LogStoreManifest.plist")
# for l in extract_compile_log("36C1B4AD-7938-4FD2-B8EE-D0EDCCB00396.xcactivitylog"):
# print(l)