forked from jma127/pcu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
139 lines (116 loc) · 3.32 KB
/
helper.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import cStringIO
import os
import pickle
import shlex
import shutil
import subprocess
import sys
import time
import globals
import settings
# Windows or not
def iswindows ():
return sys.platform.startswith('win')
# Safely copy files
def copy (src, dest):
try:
shutil.copy(src, dest)
return True
except IOError:
return False
# Safely make a directory
def makedir (dir):
try:
os.makedirs(dir)
return True
except OSError:
return False
# Safely delete a directory
def deldir (dir):
try:
shutil.rmtree(dir)
return True
except OSError:
return False
# Get the total CPU time of a process
def gettime (pid):
if iswindows():
return 0.0
psoutput = subprocess.check_output(shlex.split('ps -p {:d} -o time='.format(pid))).split(':')
if len(psoutput) == 0:
return 0.0
return float(psoutput[0]) * 60.0 + float(psoutput[1])
# Execute a process with time constraints and custom I/O
# Return value: (exitcode, cputime, stdout, stderr)
def runproc (cmd, workingdir, stdin=None, timelim=10.0, sleepintvl=0.01):
stdinname = os.path.join(workingdir, 'stdin.tmp')
stdoutname = os.path.join(workingdir, 'stdout.tmp')
stderrname = os.path.join(workingdir, 'stderr.tmp')
write(stdinname, stdin, printempty=True)
stdinfile = open(stdinname, 'rb', 1)
stdoutfile = open(stdoutname, 'wb', 1)
stderrfile = open(stderrname, 'wb', 1)
proc = subprocess.Popen(shlex.split(cmd), cwd=workingdir, stdin=stdinfile,
stdout=stdoutfile, stderr=stderrfile)
cputime = 0.0
while True:
retcode = proc.poll()
if retcode is not None:
stdoutfile.close()
stderrfile.close()
return (retcode, cputime, read(stdoutname), read(stderrname))
newtime = gettime(proc.pid)
if newtime > cputime:
cputime = newtime
if cputime > timelim:
proc.kill()
time.sleep(sleepintvl)
stdoutfile.close()
stderrfile.close()
return (None, None, read(stdoutname), read(stderrname))
time.sleep(sleepintvl)
# Print test case input/output
def printdesc (str, desc, div, printeof=True):
if str is None or len(str) == 0:
return
print ('>>> ' + desc + ' ' + div)[:len(div)]
if settings.eof is None or not printeof:
print str
else:
print str + settings.eof
# Read string from file
def read (filename):
try:
file = open(filename, 'rb')
str = file.read()
file.close()
return str
except IOError:
return None
# Write string to file
def write (filename, str, printempty=False):
try:
if str is not None and len(str) > 0:
file = open(filename, 'wb')
file.write(str)
file.close()
elif printempty:
file = open(filename, 'wb')
file.write('')
file.close()
except IOError:
pass
# Read dict from file
def readdict (file):
data = read(file)
if data is None:
return {}
return pickle.loads(data)
# Write dict to file
def writedict (file, dict):
newdict = {}
for key in dict:
if key in globals.reusable:
newdict[key] = dict[key]
data = pickle.dumps(newdict)
write(file, data)