-
Notifications
You must be signed in to change notification settings - Fork 1
/
vjoule_handler.py
39 lines (28 loc) · 1.06 KB
/
vjoule_handler.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
import os
import sys
import re
import time
vjoule_cmd = os.environ.get("VJOULE_CMD", "vjoule")
python_cmd = os.environ.get("PYTHON_CMD", "python3")
def parse_results(input: str) -> dict:
pattern = r'(CPU|RAM)\s+([\d.]+)\s+J'
matches = re.findall(pattern, input)
# Create a dictionary from the matches
results = {key: float(value) for key, value in matches}
results['total'] = sum(results.values())
return results
def eval_command(cmd: str, *args) -> dict:
args_as_str = " ".join(args)
command = f"{vjoule_cmd} {cmd} {args_as_str}"
print("### - " + str(time.time()) + " - Start of command - " + command )
stream = os.popen(command)
print("### - " + str(time.time()) + " - End of command - " + command)
return parse_results(stream.read())
def eval_python(filepath: str, *args) -> dict:
args_as_str = " ".join([str(a) for a in args])
return eval_command(f"{python_cmd} {filepath} {args_as_str}")
def main() -> int:
eval_python(sys.argv[1], *sys.argv[2:])
return 0
if __name__ == '__main__':
sys.exit(main())