-
Notifications
You must be signed in to change notification settings - Fork 377
/
test_nowx.py
executable file
·124 lines (107 loc) · 4.69 KB
/
test_nowx.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
"""test_nowx.py - ensure that there's no dependency on wx when headless
CellProfiler is distributed under the GNU General Public License.
See the accompanying file LICENSE for details.
Copyright (c) 2003-2009 Massachusetts Institute of Technology
Copyright (c) 2009-2012 Broad Institute
All rights reserved.
Please see the AUTHORS file for credits.
Website: http://www.cellprofiler.org
"""
__version__="$Revision$"
import tempfile
import traceback
import unittest
from cellprofiler.modules.tests import example_images_directory
import __builtin__
def import_all_but_wx(name,
globals = __builtin__.globals(),
locals = __builtin__.locals(),
fromlist=[], level=-1,
default_import = __builtin__.__import__):
if name == "wx" or name.startswith("wx."):
raise ImportError("Not allowed to import wx!")
return default_import(name, globals, locals, fromlist, level)
class TestNoWX(unittest.TestCase):
def setUp(self):
from cellprofiler.preferences import set_headless
set_headless()
self.old_import = __builtin__.__import__
__builtin__.__import__ = import_all_but_wx
def tearDown(self):
__builtin__.__import__ = self.old_import
def example_dir(self):
return example_images_directory()
def test_01_01_can_import(self):
import os
self.assertTrue(hasattr(os, "environ"))
def test_01_02_throws_on_wx_import(self):
def import_wx():
import wx
self.assertRaises(ImportError, import_wx)
def test_01_03_import_modules(self):
'''Import cellprofiler.modules and make sure it doesn't import wx'''
import cellprofiler.modules
def test_01_04_instantiate_all(self):
'''Instantiate each module and make sure none import wx'''
import cellprofiler.modules as M
for name in M.get_module_names():
try:
M.instantiate_module(name)
except:
print "Module %s probably imports wx" % name
traceback.print_exc()
def test_01_05_load_pipeline(self):
import cellprofiler.pipeline as cpp
import os
pipeline_file = os.path.join(self.example_dir(),
"ExampleSBSImages", "ExampleSBS.cp")
self.assertTrue(os.path.isfile(pipeline_file), "No ExampleSBS.cp file")
pipeline = cpp.Pipeline()
def callback(caller, event):
self.assertFalse(isinstance(event, cpp.LoadExceptionEvent))
pipeline.add_listener(callback)
pipeline.load(pipeline_file)
def test_01_06_run_pipeline(self):
import cellprofiler.pipeline as cpp
import cellprofiler.cpmodule as cpm
import os
from cellprofiler.preferences import set_default_image_directory, set_default_output_directory
example_sbs_dir = os.path.join(self.example_dir(),
"ExampleSBSImages")
set_default_image_directory(example_sbs_dir)
output_dir = tempfile.mkdtemp()
set_default_output_directory(output_dir)
try:
pipeline_file = os.path.join(example_sbs_dir, "ExampleSBS.cp")
pipeline = cpp.Pipeline()
def callback(caller, event):
self.assertFalse(isinstance(event, (cpp.LoadExceptionEvent,
cpp.RunExceptionEvent)))
pipeline.add_listener(callback)
pipeline.load(pipeline_file)
while True:
removed_something = False
for module in reversed(pipeline.modules()):
self.assertTrue(isinstance(module, cpm.CPModule))
if module.module_name in ("SaveImages",
"CalculateStatistics",
"ExportToSpreadsheet"):
pipeline.remove_module(module.module_num)
removed_something = True
break
if not removed_something:
break
m = pipeline.run(image_set_end = 1)
del m
finally:
for file_name in os.listdir(output_dir):
try:
os.remove(os.path.join(output_dir, file_name))
except Exception, e:
print "Failed to remove %s" % os.path.join(output_dir, file_name), e
traceback.print_exc()
try:
os.rmdir(output_dir)
except:
print "Failed to remove %s" % output_dir
traceback.print_exc()