-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_cmd.py
More file actions
70 lines (55 loc) · 1.79 KB
/
parse_cmd.py
File metadata and controls
70 lines (55 loc) · 1.79 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
from pathlib import Path
import shlex
import re
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('cmd_input', type=Path, help='input file containg flash command')
parser.add_argument('json_output', type=Path, help='output file with parsed flash arguments')
script_args = parser.parse_args()
raw_cmd = script_args.cmd_input.read_text().strip()
args = iter(shlex.split(raw_cmd))
arg = next(args)
if re.fullmatch('python3?', arg):
arg = next(args)
assert arg == '/build/Sming/Sming/Components/esptool/esptool/esptool.py'
arg = next(args)
assert arg == '-p'
arg = next(args)
assert arg == '/dev/ttyUSB0'
important_args = []
while True:
arg = next(args)
if re.fullmatch('0x[a-f0-9]*', arg):
break
important_args.append(arg)
segments = []
sming_path = Path('/', 'build', 'Sming')
out_path = Path('out')
while True:
assert re.fullmatch('0x[a-f0-9]*', arg)
addr = arg
file = Path(next(args))
if file.is_relative_to(sming_path):
file = file.relative_to(sming_path)
segments.append((addr, 'SMING_HOME', str(file)))
elif file.is_relative_to(out_path):
assert file.parts[0] == 'out'
assert file.parts[1] == 'Esp8266'
assert file.parts[2] in {'debug', 'release'}
assert file.parts[3] == 'firmware'
file = Path(*file.parts[4:])
segments.append((addr, 'FIRMWARE', str(file)))
else:
raise ValueError(f'Unexpected file path: {file}')
try:
arg = next(args)
except StopIteration:
break
print(important_args)
print(segments)
script_args.json_output.write_text(json.dumps({
'esptool': str(Path('/build/Sming/Sming/Components/esptool/esptool/esptool.py').relative_to(sming_path)),
'arguments': important_args,
'segments': [list(x) for x in segments]}
))