-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
91 lines (69 loc) · 3.07 KB
/
runner.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
import os
import sys
import json
import time
import shutil
import pathlib
import tempfile
import argparse
import subprocess
import requests
from relia_gr_runner.grc_manager import GrcManager
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=pathlib.Path, required=True)
parser.add_argument('--host', default="localhost")
parser.add_argument('--base-url', default=None)
parser.add_argument('--device-id', default='my-device-id')
parser.add_argument('--session-id', default='my-session-id')
args = parser.parse_args()
grc_content_serialized = open(args.input).read()
target_filename = 'target_file'
grc_processor = GrcManager(grc_content_serialized, target_filename)
# TODO
if args.base_url:
uploader_base_url = args.base_url
else:
uploader_base_url = f'http://{args.host}:6001'
session_id = args.session_id
device_id = args.device_id
print(f"Resetting device {device_id}")
print(requests.delete(uploader_base_url + f"/api/download/sessions/{session_id}/devices/{device_id}").json())
with tempfile.TemporaryDirectory(prefix='relia-') as tmpdir:
grc_filename = os.path.join(tmpdir, 'user_file.grc')
py_filename = os.path.join(tmpdir, f'{target_filename}.py')
grc_processor.save(tmpdir, 'user_file.grc')
open(os.path.join(tmpdir, 'relia.json'), 'w').write(json.dumps({
'uploader_base_url': uploader_base_url,
'session_id': session_id,
'device_id': device_id,
}))
# Create the "files" directory
os.mkdir(os.path.join(tmpdir, "files"))
# TODO: at some point copy the INPUT files (if any)
command = ['grcc', grc_filename, '-o', tmpdir]
try:
print(subprocess.check_output(command, cwd=tmpdir, text=True))
except subprocess.CalledProcessError as err:
print("Error processing grcc:", file=sys.stderr)
print("", file=sys.stderr)
print(f" $ {' '.join(command)}", file=sys.stderr)
print(err.output, file=sys.stderr)
print(" $", file=sys.stderr)
print("", file=sys.stderr)
tmp_directory = pathlib.Path(tempfile.gettempdir())
error_tmp_directory = tmp_directory / f"relia-error-tmp-{time.time()}"
os.mkdir(error_tmp_directory)
shutil.copy(os.path.join(tmpdir, 'user_file.grc'), error_tmp_directory)
shutil.copy(os.path.join(tmpdir, 'relia.json'), error_tmp_directory)
print(f"You can reproduce it going to the directory {error_tmp_directory} and running the command:", file=sys.stderr)
print("", file=sys.stderr)
print(f" $ cd {error_tmp_directory}", file=sys.stderr)
print(f" $ grcc {error_tmp_directory / 'user_file.grc'} -o {error_tmp_directory}", file=sys.stderr)
print("", file=sys.stderr)
raise
print(tmpdir)
subprocess.run([sys.executable, py_filename], cwd=tmpdir)
input("Press any key to finish")
if __name__ == '__main__':
main()