42
42
43
43
if sys .version_info [0 ] == 2 :
44
44
from urllib import urlopen
45
+ string_types = basestring
45
46
else :
46
47
from urllib .request import urlopen
47
-
48
-
49
- def _download_file (url ):
50
- u = urlopen (url )
48
+ string_types = str
49
+
50
+
51
+ def _download_file (baseline , filename ):
52
+ # Note that baseline can be a comma-separated list of URLs that we can
53
+ # then treat as mirrors
54
+ for base_url in baseline .split (',' ):
55
+ try :
56
+ u = urlopen (base_url + filename )
57
+ content = u .read ()
58
+ except Exception as exc :
59
+ warnings .warn ('Downloading {0} failed' .format (base_url + filename ))
60
+ else :
61
+ break
62
+ else :
63
+ raise Exception ("Could not download baseline image from any of the "
64
+ "available URLs" )
51
65
result_dir = tempfile .mkdtemp ()
52
66
filename = os .path .join (result_dir , 'downloaded' )
53
67
with open (filename , 'wb' ) as tmpfile :
54
- tmpfile .write (u . read () )
68
+ tmpfile .write (content )
55
69
return filename
56
70
57
71
@@ -60,9 +74,13 @@ def pytest_addoption(parser):
60
74
group .addoption ('--mpl' , action = 'store_true' ,
61
75
help = "Enable comparison of matplotlib figures to reference files" )
62
76
group .addoption ('--mpl-generate-path' ,
63
- help = "directory to generate reference images in, relative to location where py.test is run" , action = 'store' )
77
+ help = "directory to generate reference images in, relative "
78
+ "to location where py.test is run" , action = 'store' )
64
79
group .addoption ('--mpl-baseline-path' ,
65
- help = "directory containing baseline images, relative to location where py.test is run" , action = 'store' )
80
+ help = "directory containing baseline images, relative to "
81
+ "location where py.test is run. This can also be a URL or a "
82
+ "set of comma-separated URLs (in case mirrors are "
83
+ "specified)" , action = 'store' )
66
84
67
85
results_path_help = "directory for test results, relative to location where py.test is run"
68
86
group .addoption ('--mpl-results-path' , help = results_path_help , action = 'store' )
@@ -157,12 +175,12 @@ def item_function_wrapper(*args, **kwargs):
157
175
baseline_dir = os .path .join (os .path .dirname (item .fspath .strpath ), 'baseline' )
158
176
else :
159
177
baseline_dir = self .baseline_dir
178
+ baseline_remote = False
160
179
else :
161
- if not baseline_dir .startswith (('http://' , 'https://' )):
180
+ baseline_remote = baseline_dir .startswith (('http://' , 'https://' ))
181
+ if not baseline_remote :
162
182
baseline_dir = os .path .join (os .path .dirname (item .fspath .strpath ), baseline_dir )
163
183
164
- baseline_remote = baseline_dir .startswith ('http' )
165
-
166
184
with plt .style .context (style ), switch_backend (backend ):
167
185
168
186
# Run test and get figure object
@@ -201,7 +219,7 @@ def item_function_wrapper(*args, **kwargs):
201
219
202
220
# Find path to baseline image
203
221
if baseline_remote :
204
- baseline_image_ref = _download_file (baseline_dir + filename )
222
+ baseline_image_ref = _download_file (baseline_dir , filename )
205
223
else :
206
224
baseline_image_ref = os .path .abspath (os .path .join (os .path .dirname (item .fspath .strpath ), baseline_dir , filename ))
207
225
0 commit comments