-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
82 lines (75 loc) · 2.44 KB
/
main.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
import time
import os
import sys
import socket
import shutil
from mainfunc import *
from utils.arguments import parse_args
from utils.log import log, loginit, logexit
def main_wrapper(args):
args = vars(parse_args(args))
args = loginit(args)
log(args, level = 'ALL')
M = args['main']
def cleancheck(args):
clean_flag = False
if args['clean_logs']:
clean_flag = True
if not args['force_clean']:
log('run over, try to clean log folder `%s`. are you sure to '
'clean? if not, press Ctrl+C' % args['log_folder'])
try:
input()
except KeyboardInterrupt:
clean_flag = False
except EOFError:
log('got EOF, stop cleaning', level = 'WARN')
clean_flag = False
lf = os.path.split(args['log_folder'])
if len(lf[1]) == 0: # last folder name not split
lf = os.path.split(lf[0])
assert len(lf[1]) != 0
lf = lf[1]
os.makedirs('./results/cleaned', exist_ok = True)
srcfile = '%s/main.log' % args['log_folder']
destfile = './results/%s%s_%s.log' % (
'cleaned/' if clean_flag else '',
lf,
socket.gethostname()
)
open(destfile, 'w').write(open(srcfile).read())
if clean_flag:
logexit()
if 'linux' in sys.platform:
os.system('rm -r %s' % args['log_folder'])
else:
print('[ERROR] not in linux, cannot remove log folder! please '
'remove it manually.')
try:
if M.lower() == 'dqn':
main = DQNMain(**args)
elif M.lower() == 'determined':
main = DeterminedMain(**args)
elif M in globals():
main = globals()[M](**args)
else:
M = M + 'Main'
if M in globals():
main = globals()[M](**args)
else:
raise NotImplementedError('unknown main ' + M[:-4])
main.main()
except (Exception, KeyboardInterrupt) as e:
try:
del main
except Exception:
pass
time.sleep(0.3)
log('some error cooured! will show below.', level = 'ERROR')
cleancheck(args)
raise e
del main
time.sleep(0.3)
cleancheck(args)
if __name__ == "__main__":
main_wrapper(sys.argv)