11# -*- coding: utf-8 -*-
22"""
33Test MOOSE installation with moose-examples.
4-
54"""
65
76from __future__ import print_function , division
87
9- __author__ = "Dilawar Singh"
10- __copyright__ = "Copyright 2016, Dilawar Singh"
11- __credits__ = ["NCBS Bangalore" ]
12- __license__ = "GNU GPL"
13- __version__ = "1.0.0"
14- __maintainer__ = "Dilawar Singh"
15- 16- __status__ = "Development"
8+ __author__ = "Dilawar Singh"
9+ __copyright__ = "Copyright 2016, Dilawar Singh"
10+ __credits__ = ["NCBS Bangalore" ]
11+ __license__ = "GNU GPL"
12+ __version__ = "1.0.0"
13+ __maintainer__ = "Dilawar Singh"
14+ 15+ __status__ = "Development"
1716
1817import sys
1918import os
2625from collections import defaultdict
2726import time
2827
29- logging .basicConfig (
30- level = logging .DEBUG ,
31- format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s' ,
32- datefmt = '%m-%d %H:%M'
33- )
34- console = logging .StreamHandler ()
35- console .setLevel (logging .WARNING )
36- formatter = logging .Formatter ('%(name)-12s: %(levelname)-8s %(message)s' )
37- console .setFormatter (formatter )
38- _logger = logging .getLogger ('moose.test' )
39- _logger .addHandler (console )
40-
4128test_data_url_ = 'https://github.com/BhallaLab/moose-examples/archive/master.zip'
4229test_repo_url_ = 'https://github.com/BhallaLab/moose-examples'
43- test_dir_ = os .path .join ( tempfile .gettempdir ( ), 'moose-examples' )
30+ test_dir_ = os .path .join (tempfile .gettempdir (), 'moose-examples' )
4431
45- ignored_dict_ = defaultdict ( list )
46- test_status_ = defaultdict ( list )
32+ ignored_dict_ = defaultdict (list )
33+ test_status_ = defaultdict (list )
4734total_ = 0
4835
4936matplotlibrc_ = '''
5340
5441# Handle CTRL+C
5542def signal_handler (signal , frame ):
56- print ( 'You pressed Ctrl+C!' )
57- print_test_stat ( )
43+ print ('You pressed Ctrl+C!' )
44+ print_test_stat ()
5845 quit (- 1 )
5946
6047signal .signal (signal .SIGINT , signal_handler )
@@ -64,139 +51,144 @@ class Command(object):
6451 def __init__ (self , cmd ):
6552 self .cmd = cmd
6653 self .process = None
67- self .fnull = open ( os .devnull , 'w' )
54+ self .fnull = open (os .devnull , 'w' )
6855
69- def __repr__ ( self ):
70- return ' ' .join ( self .cmd )
56+ def __repr__ (self ):
57+ return ' ' .join (self .cmd )
7158
72- def run (self , timeout ):
59+ def run (self , timeout , ** kwargs ):
7360 def target ():
74- _logger .info ( "Running %s" % self )
75- self .process = subprocess .Popen (
76- self . cmd , shell = False
77- , stdout = self . fnull , stderr = subprocess . STDOUT
78- )
61+ logging .info ("Running %s" % self )
62+ self .process = subprocess .Popen (self . cmd ,
63+ shell = False ,
64+ stdout = self . fnull ,
65+ stderr = subprocess . STDOUT )
7966 self .process .communicate ()
8067
81- thread = threading .Thread ( target = target )
68+ thread = threading .Thread (target = target )
8269 thread .start ()
8370 thread .join (timeout )
8471 if thread .is_alive ():
8572 self .process .terminate ()
8673 thread .join ()
8774
8875 if self .process .stderr is not None :
89- _logger .warn ( '%s' % self .process .stderr .read () )
76+ logging .warn ('%s' % self .process .stderr .read ())
9077
9178 return self .process .returncode
9279
93- def init_test_dir ( ):
80+
81+ def init_test_dir ():
9482 global test_dir_
9583 global test_url_
96- if ( not os .path .exists ( test_dir_ ) ):
97- os .makedirs ( test_dir_ )
98- _logger .info ( "Donwloading test repository" )
84+ if ( not os .path .exists (test_dir_ ) ):
85+ os .makedirs (test_dir_ )
86+ logging .info ("Donwloading test repository" )
9987 subprocess .call (
100- [ 'git' , 'clone' , '--depth=10' , test_repo_url_ , test_dir_ ]
101- )
102- os . chdir ( test_dir_ )
88+ [ 'git' , 'clone' , '--depth=10' , test_repo_url_ , test_dir_ ])
89+ os . chdir ( test_dir_ )
90+
10391
104- def suitable_for_testing ( script ):
92+ def suitable_for_testing (script ):
10593 with open (script , 'r' , encoding = 'utf8' ) as f :
10694 txt = f .read ()
107- if not re .search ( r'main\(\s*\)' , txt ):
108- _logger .debug ( 'Script %s does not contain main( )' % script )
95+ if not re .search (r'main\(\s*\)' , txt ):
96+ logging .debug ('Script %s does not contain main( )' % script )
10997 return False , 'main( ) not found'
110- if re .search ( r'raw_input\(\s*\)' , txt ):
111- _logger .debug ( 'Script %s requires user input' % script )
98+ if re .search (r'raw_input\(\s*\)' , txt ):
99+ logging .debug ('Script %s requires user input' % script )
112100 return False , 'waits for user input'
113101 return True , 'OK'
114102
115- def run_test ( index , testfile , timeout , ** kwargs ):
103+
104+ def run_test (index , testfile , timeout , ** kwargs ):
116105 """Run a given test
117106 """
118107 global test_status_
119108 global total_
120- pyExec = os .environ .get ( 'PYTHON_EXECUTABLE' , sys .executable )
121- cmd = Command ( [ pyExec , testfile ] )
109+ pyExec = os .environ .get ('PYTHON_EXECUTABLE' , sys .executable )
110+ cmd = Command ([ pyExec , testfile ] )
122111
123- ti = time .time ( )
124- name = os .path .basename ( testfile )
112+ ti = time .time ()
113+ name = os .path .basename (testfile )
125114 out = (name + '.' * 50 )[:40 ]
126- print ( '[TEST %3d/%d] %41s ' % (index , total_ , out ), end = '' )
127- sys .stdout .flush ( )
115+ print ('[TEST %3d/%d] %41s ' % (index , total_ , out ), end = '' )
116+ sys .stdout .flush ()
128117
129118 # Run the test.
130- status = cmd .run ( timeout = timeout )
131- t = time .time ( ) - ti
132- print ( '% 7.2f ' % t , end = '' )
133- sys .stdout .flush ( )
119+ status = cmd .run (timeout = timeout , ** kwargs )
120+ t = time .time () - ti
121+ print ('% 7.2f s ' % t , end = '' )
122+ sys .stdout .flush ()
134123
135124 # Change to directory and copy before running then test.
136- cwd = os .path .dirname ( testfile )
137- os .chdir ( cwd )
138- with open ( os .path .join ( cwd , 'matplotlibrc' ), 'w' ) as f :
139- _logger .debug ( 'Writing matplotlibrc to %s' % cwd )
140- f .write ( matplotlibrc_ )
125+ cwd = os .path .dirname (testfile )
126+ os .chdir (cwd )
127+ with open (os .path .join (cwd , 'matplotlibrc' ), 'w' ) as f :
128+ logging .debug ('Writing matplotlibrc to %s' % cwd )
129+ f .write (matplotlibrc_ )
141130
142131 if status != 0 :
143132 if status == - 15 :
144133 msg = '% 4d TIMEOUT' % status
145- test_status_ [ 'TIMED-OUT' ].append ( testfile )
134+ test_status_ ['TIMED-OUT' ].append (testfile )
146135 else :
147136 msg = '% 4d FAILED' % status
148- test_status_ [ 'FAILED' ].append ( testfile )
149- print ( msg )
137+ test_status_ ['FAILED' ].append (testfile )
138+ print (msg )
150139 else :
151- print ( '% 4d PASSED' % status )
152- test_status_ [ 'PASSED' ].append ( testfile )
140+ print ('% 4d PASSED' % status )
141+ test_status_ ['PASSED' ].append (testfile )
142+
143+ sys .stdout .flush ()
153144
154- sys .stdout .flush ( )
155145
156- def print_test_stat ( ):
146+ def print_test_stat ():
157147 global test_status_
158148 for status in test_status_ :
159- print ( 'Total %d tests %s' % (len ( test_status_ [status ] ), status ) )
149+ print ('Total %d tests %s' % (len (test_status_ [status ]), status ) )
160150
161- def test_all ( timeout , ** kwargs ):
151+
152+ def test_all (timeout , ** kwargs ):
162153 global test_dir_
163154 global total_
164- scripts = [ ]
165- for d , ds , fs in os .walk ( test_dir_ ):
155+ scripts = []
156+ for d , ds , fs in os .walk (test_dir_ ):
166157 for f in fs :
167- if not re .match ( r'.+\.py$' , f ):
158+ if not re .match (r'.+\.py$' , f ):
168159 continue
169- filepath = os .path .join ( d , f )
170- isOK , msg = suitable_for_testing ( filepath )
160+ filepath = os .path .join (d , f )
161+ isOK , msg = suitable_for_testing (filepath )
171162 if isOK :
172- scripts .append ( filepath )
163+ scripts .append (filepath )
173164 else :
174- ignored_dict_ [ msg ].append ( filepath )
165+ ignored_dict_ [msg ].append (filepath )
175166
176167 for k in ignored_dict_ :
177- _logger .debug ( '[INFO] Ignored due to %s' % k )
178- _logger .debug ( '\n \t ' .join ( ignored_dict_ [ k ] ) )
168+ logging .debug ('[INFO] Ignored due to %s' % k )
169+ logging .debug ('\n \t ' .join (ignored_dict_ [k ]) )
179170
180- _logger .info ( 'Total %d valid tests found' % len ( scripts ) )
181- total_ = len ( scripts )
182- for i , s in enumerate ( scripts ):
183- _logger .info ( 'Running test (timeout=%s) : %s' % (timeout ,s ))
184- run_test (i , s , timeout , ** kwargs )
171+ logging .info ('Total %d valid tests found' % len (scripts ) )
172+ total_ = len (scripts )
173+ for i , s in enumerate (scripts ):
174+ logging .info ('Running test (timeout=%s) : %s' % (timeout , s ))
175+ run_test (i , s , timeout , ** kwargs )
185176
186177
187- def test ( timeout = 60 , ** kwargs ):
178+ def test (timeout = 60 , ** kwargs ):
188179 """Download and run tests.
189180
190181 """
191- print ( '[INFO] Running test with timeout %d sec' % timeout )
182+ print ('[INFO] Running test with timeout %d sec' % timeout )
192183 try :
193- init_test_dir ( )
184+ init_test_dir ()
194185 except Exception as e :
195- print ( '[INFO] Failed to clone moose-examples. Error was %s' % e )
196- quit ( )
186+ print ('[INFO] Failed to clone moose-examples. Error was %s' % e )
187+ quit ()
188+
189+ test_all (timeout = timeout , ** kwargs )
190+ print_test_stat ()
197191
198- test_all ( timeout = timeout , ** kwargs )
199- print_test_stat ( )
200192
201193if __name__ == '__main__' :
202- test ( )
194+ test ()
0 commit comments