/
run.py
114 lines (80 loc) · 2.91 KB
/
run.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
from sys import argv
from subprocess import run as run_command
PYALIAS_PATH = argv[0]
CURRENT_PATH = argv[1]
FULL_FILE_PATH = argv[2].split('=')[1]
ARGS = list(filter(lambda arg: '--py' not in arg, argv[4:]))
ENV_ARGS = list(filter(lambda arg: '--py' in arg, argv[4:]))
DEV_MODE = '--pydev' in ENV_ARGS
LANG = FULL_FILE_PATH.split('.')[-1]
FILE_PATH = FULL_FILE_PATH.split('.')[0]
FULL_PATH = f'{CURRENT_PATH}/{FULL_FILE_PATH}'
TEMP_PATH = f'{"/".join(PYALIAS_PATH.split("/")[:-1])}/temp'
FILE_NAME = FILE_PATH.split('/')[-1]
def main():
if not FULL_FILE_PATH:
show_error('file not found')
if LANG in ['c']:
run_c()
elif LANG in ['cpp']:
run_cpp()
elif LANG in ['java']:
run_java()
elif LANG in ['py']:
run_python()
else:
show_error('unsupported programming language')
def run_c():
output_file_path = f'{TEMP_PATH}/run'
compile_command = ['gcc', FULL_PATH, '-o', output_file_path]
execution_command = [output_file_path]
execution_command.extend(ARGS)
show_log(f'compiled: {compile_command}')
show_log(f'executed: {execution_command}')
result = run_command(compile_command)
if result.returncode == 0:
run_command(execution_command)
def run_cpp():
output_file_path = f'{TEMP_PATH}/run'
compile_command = ['c++', FULL_PATH, '-o', output_file_path]
execution_command = [output_file_path]
execution_command.extend(ARGS)
show_log(f'compiled: {compile_command}')
show_log(f'executed: {execution_command}')
result = run_command(compile_command)
if result.returncode == 0:
run_command(execution_command)
def run_java():
package = FILE_PATH.split('/')
if len(package) > 2:
package = '/'.join(FILE_PATH.split('/')[-2:])
other_path = FILE_PATH.replace(package, "")
compile_command =\
f'(cd {other_path} && javac {package}.{LANG} -d {TEMP_PATH})'
execution_command = f'(cd {TEMP_PATH} && java {package})'
else:
compile_command = f'javac {FULL_PATH} -d {TEMP_PATH}'
execution_command = f'(cd {TEMP_PATH} && java {FILE_PATH})'
show_log(f'compiled: {compile_command}')
show_log(f'executed: {execution_command}')
result = run_command(compile_command, shell=True)
if result.returncode == 0:
run_command(execution_command, shell=True)
def run_python():
execution_command = ['python3', '-B', FULL_PATH]
execution_command.extend(ARGS)
show_log(f'executed: {execution_command}')
run_command(execution_command)
def show_log(message):
if DEV_MODE:
print(f'\033[33m{message.capitalize()}\033[0;0m')
def show_error(error, stop_exec=True):
print(f'\n\033[31m"{error.capitalize()}"\033[0;0m\n')
if stop_exec:
exit(1)
if __name__ == '__main__':
try:
main()
except Exception as err:
show_error('sorry, unexpected error', stop_exec=not DEV_MODE)
raise