-
Notifications
You must be signed in to change notification settings - Fork 8
/
upd_archive_byexp.py
executable file
·73 lines (63 loc) · 2.21 KB
/
upd_archive_byexp.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
#!/usr/bin/env python3
import os
import re
from tqdm import tqdm
import shutil
import glob
base = '<absolute-path-to-code>/'
archive = os.path.join(base, 'archive')
archive_byexp = os.path.join(base, 'archive_byexp')
logs = os.path.join(base, 'logs')
slurm_logs = os.path.join(base, 'slurmlogs')
def getexperiment(run):
scriptname = re.match(r'^(.*)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}.*$', run).group(1)
# script = os.path.join(archive, run, 'scripts', '_'.join(run.split('_')[:-2])+'.sh')
script = os.path.join(archive, run, 'scripts', scriptname+'.sh')
configpath = None
with open(script) as f:
for l in f:
if '#' in l:
l = l[:l.index('#')]
l = l.split()
if '--config' in l:
configpath = l[l.index('--config')+1]
break
assert configpath is not None, run
# raise RuntimeError(configpath)
with open(os.path.join(archive, run, configpath)) as f:
for l in f:
if 'expname' in l:
res = '='.join(l.split('=')[1:]).strip()
if not os.path.exists(os.path.join(logs, res)):
res = None
return res
print('reading exps...')
runs = list(sorted(os.listdir(archive)))
exps = dict()
for run in runs:
exp = getexperiment(run)
if exp is not None:
exps[exp] = run
# print(run, exp)
# 1/0
print('updating...')
os.makedirs(archive_byexp, exist_ok=True)
for exp, run in exps.items():
for link_fn in [os.path.join(archive_byexp, exp), os.path.join(logs, exp, 'archive')]:
source = os.path.join(archive, run)
need_to_link = None
if os.path.exists(link_fn):
if os.path.realpath(os.readlink(link_fn)) != os.path.realpath(source):
print('recreating for', os.path.realpath(os.readlink(link_fn)), os.path.realpath(source))
os.remove(link_fn)
need_to_link = True
else:
need_to_link = False
else:
need_to_link = True
if need_to_link:
print(link_fn, source)
try:
os.symlink(source, link_fn)
except PermissionError:
print('permission error')