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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#!/usr/bin/env python
"""Run the py->rst conversion and run all examples.
Steps are:
analyze example index file for example py filenames
check for any filenames in example directory not included
do py to rst conversion, writing into build directory
run
"""
# -----------------------------------------------------------------------------
# Library imports
# -----------------------------------------------------------------------------
# Stdlib imports
import os
import os.path as op
import sys
import shutil
import io
from subprocess import check_call
from glob import glob
from time import time
# Third-party imports
# We must configure the mpl backend before making any further mpl imports
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib._pylab_helpers import Gcf
import dipy
# -----------------------------------------------------------------------------
# Function defintions
# -----------------------------------------------------------------------------
# These global variables let show() be called by the scripts in the usual
# manner, but when generating examples, we override it to write the figures to
# files with a known name (derived from the script name) plus a counter
figure_basename = None
# We must change the show command to save instead
def show():
allfm = Gcf.get_all_fig_managers()
for fcount, fm in enumerate(allfm):
fm.canvas.figure.savefig('%s_%02i.png' %
(figure_basename, fcount + 1))
_mpl_show = plt.show
plt.show = show
# -----------------------------------------------------------------------------
# Main script
# -----------------------------------------------------------------------------
# Where things are
DOC_PATH = op.abspath('..')
EG_INDEX_FNAME = op.join(DOC_PATH, 'examples_index.rst')
EG_SRC_DIR = op.join(DOC_PATH, 'examples')
# Work in examples directory
# os.chdir(op.join(DOC_PATH, 'examples_built'))
if not os.getcwd().endswith(op.join('doc', 'examples_built')):
raise OSError('This must be run from the doc directory')
# Copy the py files; check they are in the examples list and warn if not
with io.open(EG_INDEX_FNAME, 'rt', encoding="utf8") as f:
eg_index_contents = f.read()
# Here I am adding an extra step. The list of examples to be executed need
# also to be added in the following file (valid_examples.txt). This helps
# with debugging the examples and the documentation only a few examples at
# the time.
flist_name = op.join(op.dirname(os.getcwd()), 'examples',
'valid_examples.txt')
with io.open(flist_name, "r", encoding="utf8") as flist:
validated_examples = flist.readlines()
# Parse "#" in lines
validated_examples = [line.split("#", 1)[0] for line in validated_examples]
# Remove leading and trailing white space from example names
validated_examples = [line.strip() for line in validated_examples]
# Remove blank lines
validated_examples = list(filter(None, validated_examples))
for example in validated_examples:
fullpath = op.join(EG_SRC_DIR, example)
if not example.endswith(".py"):
print("%s not a python file, skipping." % example)
continue
elif not op.isfile(fullpath):
print("Cannot find file, %s, skipping." % example)
continue
shutil.copyfile(fullpath, example)
# Check that example file is included in the docs
file_root = example[:-3]
if file_root not in eg_index_contents:
msg = "Example, %s, not in index file %s."
msg = msg % (example, EG_INDEX_FNAME)
print(msg)
# Run the conversion from .py to rst file
check_call('{} ../../tools/ex2rst'.format(sys.executable) +
' --project dipy --outdir . .', shell=True)
# added the path so that scripts can import other scripts on the same directory
sys.path.insert(0, os.getcwd())
if not op.isdir('fig'):
os.mkdir('fig')
use_xvfb = os.environ.get('TEST_WITH_XVFB', False)
use_memprof = os.environ.get('TEST_WITH_MEMPROF', False)
if use_xvfb:
try:
from xvfbwrapper import Xvfb
except ImportError:
raise RuntimeError("You are trying to run a documentation build",
"with 'TEST_WITH_XVFB' set to True, but ",
"xvfbwrapper is not available. Please install",
"xvfbwrapper and try again")
display = Xvfb(width=1920, height=1080)
display.start()
if use_memprof:
try:
import memory_profiler
except ImportError:
raise RuntimeError("You are trying to run a documentation build",
"with 'TEST_WITH_MEMPROF' set to True, but ",
"memory_profiler is not available. Please install",
"memory_profiler and try again")
name = ''
def run_script():
namespace = {}
t1 = time()
with io.open(script, encoding="utf8") as f:
exec(f.read(), namespace)
t2 = time()
print("That took %.2f seconds to run" % (t2 - t1))
plt.close('all')
del namespace
# Execute each python script in the directory:
for script in validated_examples:
figure_basename = op.join('fig', op.splitext(script)[0])
if use_memprof:
print("memory profiling ", script)
memory_profiler.profile(run_script)()
else:
print('*************************************************************')
print(script)
print('*************************************************************')
run_script()
if use_xvfb:
display.stop()
# clean up stray images, pickles, npy files, etc
for globber in ('*.nii.gz', '*.dpy', '*.npy', '*.pkl', '*.mat', '*.img',
'*.hdr'):
for fname in glob(globber):
os.unlink(fname)
|