forked from SeattleTestbed/utf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutfutil.py
172 lines (117 loc) · 3.38 KB
/
utfutil.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import subprocess
import os
import tempfile
import pickle
import sys # This is to get the python executable that will launch the external process
def execute(args):
"""
<Purpose>
Given the list of arguments, this function will execute them within Python.
<Arguments>
args: Command line arguments.
<Exceptions>
None
<Side Effects>
None
<Returns>
Output after execution:
(Standard Out, Standard Error)
"""
process = subprocess.Popen(args,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
(stdoutdata, stderrdata) = process.communicate()
return (stdoutdata, stderrdata)
def execute_repy(args):
"""
<Purpose>
Given the list of arguments, this function will execute them within Repy
Execution Environment.
<Arguments>
args: Command line arguments
<Exceptions>
None
<Side Effects>
None
<Returns>
Output after execution:
(Standard Out, Standard Error)
"""
python = sys.executable
repy = 'repy.py'
args = [python, repy] + args
return execute(args)
def parse_directive(source_text, directive):
"""
<Purpose>
Given the file source, 'source-text', this function will search for the given directive.
<Arguments>
source_text: The source in which we are searching for a pragma directive.
directive: The pragma directive we are searching for.
<Exceptions>
None
<Side Effects>
None
<Returns>
Return all relevant information for the specified directive:
[(Directive, Type, Argument)... ]
"""
result = []
directive_string = '#' + directive
for line in source_text.splitlines():
if line.startswith(directive_string):
stripped = line[len(directive_string):].strip()
(pragma_type, separator, arg) = stripped.partition(' ')
result.append((directive, pragma_type, arg))
return result
def kill(identifier):
"""
<Purpose>
Given the identifier, this function will search for the corresponding file
name, deserialize the file descriptor and kill the corresponding process.
<Arguments>
Identifier.
<Exceptions>
None
<Side Effects>
None
<Returns>
None
"""
temp_dir = tempfile.gettempdir()
file_path = identifier
if temp_dir:
file_path = os.path.join(temp_dir, file_path)
temp_file_handle = open(file_path)
temp_file_handle = open(file_path)
process = pickle.load(temp_file_handle)
temp_file_handle.close()
process.kill()
os.remove(file_path)
def spawn(args, identifier):
"""
<Purpose>
Given the list of arguments, this function will execute them and save the
corresponding file descriptor using the identifier as a name.
<Arguments>
Command.
Identifier.
<Exceptions>
None
<Side Effects>
None
<Returns>
None
"""
# We have to pipe the output. Otherwise, the process's output fills up and its
# write attempt eventually blocks, preventing it from continuing.
proccess = subprocess.Popen(args,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
temp_dir = tempfile.gettempdir()
file_path = identifier
if temp_dir:
file_path = os.path.join(temp_dir, file_path)
temp_file_handle = open(file_path, 'w')
pickle.dump(proccess, temp_file_handle, 2)
temp_file_handle.close()