66from pathlib import Path
77import shutil
88import string
9+ import subprocess
910import sys
1011import unittest
1112import warnings
@@ -486,15 +487,14 @@ def _image_directories(func):
486487 try :
487488 import matplotlib_baseline_images
488489 except :
489- raise ImportError ( "Not able to import matplotlib_baseline_images" )
490+ generate_matplotlib_baseline_images_from_previous_commit ( )
490491 baseline_dir = (Path (matplotlib_baseline_images .__file__ ).parent /
491492 module_path .stem )
492493 elif func .__module__ .startswith ("mpl_toolkits." ):
493494 try :
494495 import mpl_toolkits_baseline_images
495496 except :
496- raise ImportError ("Not able to import "
497- "mpl_toolkits_baseline_images" )
497+ generate_matplotlib_baseline_images_from_previous_commit ()
498498 baseline_dir = (Path (mpl_toolkits_baseline_images .__file__ ).parent /
499499 module_path .stem )
500500 else :
@@ -504,3 +504,62 @@ def _image_directories(func):
504504 result_dir = Path ().resolve () / "result_images" / module_path .stem
505505 result_dir .mkdir (parents = True , exist_ok = True )
506506 return baseline_dir , result_dir
507+
508+
509+ def generate_matplotlib_baseline_images_from_previous_commit ():
510+ mplRepoPath = Path (mpl .__file__ ).parent
511+ # Check that git is installed.
512+ subprocess .run (shutil .which ('git' ),
513+ stderr = subprocess .STDOUT ,
514+ stdout = open (os .devnull , 'w' ),
515+ universal_newlines = True ) == None
516+ # Check if current repository is a git repository
517+ subprocess .run (['git' , 'status' ],
518+ cwd = mplRepoPath ,
519+ stderr = subprocess .STDOUT ,
520+ stdout = open (os .devnull , 'w' ),
521+ universal_newlines = True ) == None
522+ # Create virtual env in tmpdir.
523+ # if not (mplRepoPath / 'tmp/testenv').exists():
524+ # modify_existing_baseline_images()
525+ # else
526+ subprocess .run (["python" , "-mvenv" , "tmp/testenv" ],
527+ cwd = mplRepoPath ,
528+ stderr = subprocess .STDOUT ,
529+ stdout = subprocess .DEVNULL ),
530+ universal_newlines = True )
531+ # Clone mpl repo to tmpvenv and run pytests from new mpl repo created
532+ subprocess .run (["git" , "clone" , str (mplRepoPath ), "tmp/testenv" ],
533+ cwd = mplRepoPath ,
534+ stderr = subprocess .STDOUT ,
535+ stdout = subprocess .DEVNULL ,
536+ universal_newlines = True )
537+ subprocess .run (["tmp/testenv/bin/python" ],
538+ cwd = mplRepoPath ,
539+ stderr = subprocess .STDOUT ,
540+ stdout = subprocess .DEVNULL ,
541+ universal_newlines = True )
542+ subprocess .run (["git" , "checkout" , "HEAD~" ],
543+ cwd = mplRepoPath ,
544+ stderr = subprocess .STDOUT ,
545+ stdout = subprocess .DEVNULL ,
546+ universal_newlines = True )
547+ subprocess .run (["python" , "-mpip" , "install" , "-e" , mplRepoPath ],
548+ cwd = mplRepoPath ,
549+ stderr = subprocess .STDOUT ,
550+ stdout = subprocess .DEVNULL ,
551+ universal_newlines = True )
552+ subprocess .run (["python" , "-mpytest" ],
553+ cwd = mplRepoPath ,
554+ stderr = subprocess .STDOUT ,
555+ stdout = subprocess .DEVNULL ,
556+ universal_newlines = True )
557+ # Copy result_images in the temporary directory and cache it
558+ subprocess .run (shutil .copytree ("result_images" ,
559+ "tempenv/.pytest_cache" ),
560+ cwd = mplRepoPath ,
561+ stderr = subprocess .STDOUT ,
562+ stdout = subprocess .DEVNUL ,
563+ universal_newlines = True )
564+
565+ # def modify_existing_baseline_images():
0 commit comments