forked from bokeh/bokeh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_tester.py
More file actions
279 lines (211 loc) · 8.67 KB
/
Copy pathinteractive_tester.py
File metadata and controls
279 lines (211 loc) · 8.67 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from __future__ import print_function
import argparse
import importlib
import os
from shutil import rmtree
from six.moves import input
import sys
import textwrap
import time
import json
# TODO:
# catch and log exceptions in examples files that fail to open
DIRECTORIES = {
'plotting-file' : '../../examples/plotting/file',
'plotting-notebook': '../../examples/plotting/notebook',
'server' : '../../examples/plotting/server',
'webgl' : '../../examples/webgl',
'models-file' : '../../examples/models/file',
'models-server' : '../../examples/models/server',
}
DEFAULT_TEST_FILES = [
'../../examples/plotting/file/stocks.py',
'../../examples/plotting/file/glucose.py',
'../../examples/plotting/server/hover.py',
]
SESSION_FILE = os.path.abspath("INTERACTIVE_TESTER_SESSION.json")
def get_parser():
"""Create the parser that will be used to add arguments to the script.
"""
parser = argparse.ArgumentParser(description=textwrap.dedent("""
Tests a selection of .py or .ipynb bokeh example files.
The --location option allows you to select a specific examples subdirectory to test all files in,
ignoring __init__.py
Location arguments can be any valid path to a folder with the examples, like:
-l /path/to/my/examplesyou can choose:
or any of the pre-built keywords that point to the related examples:
- plotting-file
- plotting-notebook
- server
- models-file
- models-server
"""), formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--no-log', action='store_true', dest='nolog', default=False,
help="don't save a log of any errors discovered")
parser.add_argument('-l', '--location', action='store', default=False,
help="example directory in which you wish to test")
parser.add_argument('--reuse-session', action='store_true', dest='reuseSession', default=False,
help="do not clean last session log and start from where you left")
parser.add_argument('--notebook-options', action='store', dest='notebookOptions', default="",
help="options to be forwarded to ipython notebook to customize it's behaviour")
return parser
def depend_check(dependency):
"""
Make sure a given dependency is installed
"""
try:
importlib.import_module(dependency)
found = True
except ImportError as e:
print("%s\nPlease use conda or pip to install the necessary dependency." % (e))
found = False
return found
def save_session(session):
"""
Save the session object to the SESSION_FILE
Args:
session(dict): dict with all the example files and results of each run
"""
with open(SESSION_FILE, 'w') as res_file:
json.dump(session, res_file)
def get_session():
"""
Return last stored session
"""
try:
with open(SESSION_FILE, 'r') as res_file:
return json.load(res_file)
except IOError:
return {}
def clean_session():
"""
Removes previous session file
"""
if os.path.exists(SESSION_FILE):
os.remove(SESSION_FILE)
def main(testing_ground=None, notebook_options=""):
"""
Collect and run .py or .ipynb examples from a set list or given examples directory, ignoring __init__.py
User input is collected to determine a properly or improperly displayed page
"""
# Create a testing directory if one does not exist, then cd into it
testing_directory = 'tmp_test'
if not os.path.exists(testing_directory):
os.mkdir(testing_directory)
os.chdir(testing_directory)
if testing_ground:
log_name = results.location
TestFiles = [
fileName for fileName in os.listdir('%s/.' % testing_ground)
if fileName.endswith(('.py', '.ipynb')) and fileName != '__init__.py'
]
else:
log_name = "fast"
TestFiles = DEFAULT_TEST_FILES
Log = []
lastSession = get_session()
for index, fileName in enumerate(TestFiles):
if testing_ground:
fileName = "%s/%s" % (testing_ground, fileName)
try:
if not fileName in lastSession:
lastSession[fileName] = "TESTING..."
save_session(lastSession)
command = get_cmd(fileName, notebook_options)
opener(fileName, command)
if results.nolog:
# Don't display 'next file' message after opening final file in a dir
if index != len(TestFiles)-1:
input("\nPress enter to open next file ")
else:
ErrorReport = test_status()
if ErrorReport:
Log.append("\n\n%s: \n %s" % (fileName, ErrorReport))
lastSession[fileName] = ErrorReport
save_session(lastSession)
else:
prevRes = lastSession[fileName]
if prevRes == "TESTING...":
print("RESULT OF %s LAST RUN NOT REGISTERED!!" % fileName)
ErrorReport = test_status()
lastSession[fileName] = ErrorReport
save_session(lastSession)
else:
print("%s detected in last session: SKIPPING" % fileName)
except (KeyboardInterrupt, EOFError):
break
# exit the testing directory and delete it
os.chdir('../')
rmtree(testing_directory)
if Log:
logger(Log, log_name)
def get_cmd(some_file, notebook_options=""):
"""Determines how to open a file depending
on whether it is a .py or a .ipynb file
"""
if some_file.endswith('.py'):
command = "python"
elif some_file.endswith('.ipynb'):
command = "ipython notebook %s" % notebook_options
return command
def opener(some_file, command):
"""Print to screen what file is being opened and then open the file using
the command method provided.
"""
print("\nOpening %s\n" % some_file.strip('../'))
os.system("%s %s" % (command, some_file))
def test_status():
"""Collect user input to determine if a file displayed correctly or incorrectly.
In the case of incorrectly displayed plots, an 'ErrorReport' string is returned.
"""
status = input("Did the plot(s) display correctly? (y/n) ")
while not status.startswith(('y', 'n')):
print("")
status = input("Unexpected answer. Please type y or n. ")
if status.startswith('n'):
ErrorReport = input("Please describe the problem: ")
return ErrorReport
def logger(error_array, name):
"""
Log errors by appending to a .txt file. The name and directory the file is saved into
is provided by the name and log_dir args.
"""
logfile = "%s_examples_testlog.txt" % name
if os.path.exists(logfile):
os.remove(logfile)
with open(logfile, 'a') as f:
print("")
print("\nWriting error log to %s" % logfile)
for error in error_array:
f.write("%s\n" % error)
if __name__ == '__main__':
if not depend_check('bokeh'):
sys.exit(1)
parser = get_parser()
results = parser.parse_args()
if results.location:
if results.location and results.location in DIRECTORIES:
target = results.location
test_dir = DIRECTORIES[target]
elif os.path.exists(results.location):
# in case target is not one of the recognized keys and is a
# valid path we can run the examples in that folder
test_dir = results.location
print("Running examples in custom location:", test_dir)
else:
print("Test location '%s' not recognized.\nPlease type 'python interactive_tester.py -h' for a list of valid test directories."
% results.location)
sys.exit(1)
else:
test_dir = None
if results.location == 'server' or test_dir is None:
print("Server examples require the bokeh server. Make sure you've typed 'bokeh serve' in another terminal tab.")
time.sleep(4)
if test_dir is None or 'notebook' in results.location:
print("Notebook examples require ipython-notebook. Make sure you have conda installed ipython-notebook")
time.sleep(4)
if not results.reuseSession:
print("cleaning previous session file...",)
clean_session()
print("OK")
main(test_dir, notebook_options=results.notebookOptions)