-
Notifications
You must be signed in to change notification settings - Fork 10
/
runq
executable file
·190 lines (147 loc) · 5.4 KB
/
runq
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
#!/usr/bin/env python3
"""
#
# ___
# _ __ _ _ _ __ / _ \
# | '__| | | | '_ \| | | |
# | | | |_| | | | | |_| |
# |_| _\__,_|_| |_|\__\_\_____ __ __ _ _
# | |_| |__ ___ / _ \| ____| \/ | | | | _ __ _ _ _ __ _ __ ___ _ __
# | __| '_ \ / _ \ | | | | _| | |\/| | | | | | '__| | | | '_ \| '_ \ / _ \ '__|
# | |_| | | | __/ | |_| | |___| | | | |_| | | | | |_| | | | | | | | __/ |
# \__|_| |_|\___| \__\_\_____|_| |_|\___/ |_| \__,_|_| |_|_| |_|\___|_|
#
#
#
# Add argument with +
# Remove argument with -
# Avoid using the pre-configured arguments by prefixing nothing
#
#
# ./runq [config_path] [(+|-)argument(=value) ...]
#
# TODO:
# - Refactor the lazy search (w/ max depth)
# - Rename variable for clarity
# -
"""
from __future__ import print_function
import os
import sys
import shlex
import shutil
import argparse
# ──────────────────────────────────────────────────────────────────────────────
ARGS = []
CONFIG: dict[str, any] = {}
# ──────────────────────────────────────────────────────────────────────────────
def find(fn):
full_search :str = os.path.dirname(os.path.abspath(fn))
file_search: str = os.path.basename(fn)
if not os.path.exists(full_search):
return None
for root, _, files in os.walk(full_search, followlinks = True):
for f in files:
# remove trailing dir if the search path is not the CWD
fd = os.path.basename(f)
if fd.startswith(file_search) or fd.__eq__(file_search):
print(f'f={f}, fd={fd}, fn={fn}')
return os.path.join(root, f)
return None
def norm(fn):
fn = os.path.expandvars(fn)
if fn.find(os.sep) < 0:
return fn
old = fn.split(os.sep)
ret = []
# explicit root
if fn.startswith(os.sep):
ret.append('')
for i in old:
if i == '.':
continue
elif i == '..':
if ret:
ret.pop()
elif i:
ret.append(i)
return os.sep.join(ret)
def parse_config(file):
args = []
curr = []
with open(file) as fd:
for cs in fd:
# Comments
if cs.startswith(('#', '//')):
continue
# If line is not parsable -> continue
sp = shlex.split(cs)
if not sp:
continue
# If indented normalize the all input into a string
if cs.startswith((' ', '\t')):
curr.append('='.join(list(map(lambda x: norm(x), sp[:2]))))
# If not indented (root)
else:
# Append the previous arguments into
if curr:
args.append(','.join(curr))
curr = []
# append dash(-) to root arg
args.append('-{}'.format(sp[0]))
if curr:
args.append(','.join(curr))
return args
def parse_args():
# Create an argument parser
parser = argparse.ArgumentParser(description='runQ the CLi tool to run QEMU')
# Add a positional argument for numbers (accepts one or more values)
parser.add_argument('config_path', type=str, help='Path to a configuration file')
parser.add_argument('--smp', type=int, default=1, help='Number of cores during the simulation')
parser.add_argument('--gdb', action='store_true', help='Setup the debugger to run QEMU')
parser.add_argument('-d', '--dry', action='store_true', help='Print the arguments instead of executing them')
parser.add_argument('-b', '--binary', default=None, type=str, help='Use a different binary to run the config')
parser.add_argument('-e', '--extra', default="", type=str, help='Add extra arguments to libqflex')
# Parse the command-line arguments
args = parser.parse_args()
print(f"CONFIG_FILE: {args.config_path}")
if args.extra:
print(f"EXTRA ARGS: {args.extra}")
if args.binary:
print(f"BINARY: {args.binary}")
if args.smp:
print(f"SMP: {args.smp}")
if args.gdb:
print("DEBUGGING enabled")
if args.dry:
print("DRY RUN enabled")
return args
def main(*opts):
"""
Get options from the command line, parse them and run the
QEMU binaires using the computed argmuments
"""
args = parse_args()
# Refactor the too greedy search function
qemu:str = shutil.which(args.binary) if args.binary else find('qemu-aarch64')
if not args.config_path or not qemu:
print(f'qemu_path={qemu}')
exit("ERROR: Could not find 'qemu' or a config file")
# always provided
CONFIG['ROOT'] = os.path.dirname(os.path.abspath(args.config_path))
CONFIG['SMP'] = str(args.smp)
CONFIG['EXTRA'] = args.extra
#! Has to happen before the parsing otherwise no substitution
for k, v in CONFIG.items():
os.environ[k] = v
ARGS = [qemu]
c_args = parse_config(args.config_path)
ARGS.extend(c_args)
if args.gdb:
ARGS = ['gdb', '--silent', '--args'] + ARGS
if args.dry:
print(' '.join(ARGS))
else:
os.execvp(ARGS[0], ARGS)
if __name__ == '__main__':
main()