forked from brantp/rtd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_safe.py
executable file
·39 lines (32 loc) · 1.17 KB
/
run_safe.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
#!/usr/bin/env python
import os,sys
def safe_script(cmd,donefile,donesuffix='.done',scriptfile=None,force_write=False):
if scriptfile is None:
scriptfile = donefile+'.sh'
if os.path.exists(scriptfile) and not force_write:
#file present; use
pass
else:
try:
os.unlink(scriptfile)
except:
pass
open(scriptfile,'w').write('#!/usr/bin/env sh\nset -e\n%s\n' % (cmd))
ret = os.system('chmod +x %s' % scriptfile)
if ret != 0:
raise OSError, 'cannot set execute for %s' % scriptfile
return '%s run_safe.py \"%s\" %s' % (sys.executable, scriptfile,donefile+donesuffix)
def unfinished_cmds(to_run_dict,finished_ext='.done'):
cmds = []
for finished_base,cmd in to_run_dict.items():
if not os.path.exists(finished_base+finished_ext):
cmds.append(cmd)
return cmds
if __name__ == "__main__":
cmd,done = sys.argv[1:]
if os.path.exists(done):
print >> sys.stderr, 'completion flag exists: %s' % done
else:
ret = os.system(cmd)
if ret == 0:
os.system('touch %s' % done)