4242
4343if sys .version_info [0 ] == 2 :
4444 from urllib import urlopen
45+ string_types = basestring
4546else :
4647 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" )
5165 result_dir = tempfile .mkdtemp ()
5266 filename = os .path .join (result_dir , 'downloaded' )
5367 with open (filename , 'wb' ) as tmpfile :
54- tmpfile .write (u . read () )
68+ tmpfile .write (content )
5569 return filename
5670
5771
@@ -60,9 +74,13 @@ def pytest_addoption(parser):
6074 group .addoption ('--mpl' , action = 'store_true' ,
6175 help = "Enable comparison of matplotlib figures to reference files" )
6276 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' )
6479 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' )
6684
6785 results_path_help = "directory for test results, relative to location where py.test is run"
6886 group .addoption ('--mpl-results-path' , help = results_path_help , action = 'store' )
@@ -157,12 +175,12 @@ def item_function_wrapper(*args, **kwargs):
157175 baseline_dir = os .path .join (os .path .dirname (item .fspath .strpath ), 'baseline' )
158176 else :
159177 baseline_dir = self .baseline_dir
178+ baseline_remote = False
160179 else :
161- if not baseline_dir .startswith (('http://' , 'https://' )):
180+ baseline_remote = baseline_dir .startswith (('http://' , 'https://' ))
181+ if not baseline_remote :
162182 baseline_dir = os .path .join (os .path .dirname (item .fspath .strpath ), baseline_dir )
163183
164- baseline_remote = baseline_dir .startswith ('http' )
165-
166184 with plt .style .context (style ), switch_backend (backend ):
167185
168186 # Run test and get figure object
@@ -201,7 +219,7 @@ def item_function_wrapper(*args, **kwargs):
201219
202220 # Find path to baseline image
203221 if baseline_remote :
204- baseline_image_ref = _download_file (baseline_dir + filename )
222+ baseline_image_ref = _download_file (baseline_dir , filename )
205223 else :
206224 baseline_image_ref = os .path .abspath (os .path .join (os .path .dirname (item .fspath .strpath ), baseline_dir , filename ))
207225
0 commit comments