def make_dvi(self, tex, fontsize): """ Generate a dvi file containing latex's layout of tex string. Return the file name. """ if rcParams['text.latex.preview']: return self.make_dvi_preview(tex, fontsize) basefile = self.get_basefile(tex, fontsize) dvifile = '%s.dvi' % basefile if not os.path.exists(dvifile): texfile = self.make_tex(tex, fontsize) with cbook._lock_path(texfile): self._run_checked_subprocess( ["latex", "-interaction=nonstopmode", "--halt-on-error", texfile], tex) for fname in glob.glob(basefile + '*'): if not fname.endswith(('dvi', 'tex')): try: os.remove(fname) except OSError: pass return dvifile
def make_dvi(self, tex, fontsize): """ Generate a dvi file containing latex's layout of tex string. Return the file name. """ if rcParams['text.latex.preview']: return self.make_dvi_preview(tex, fontsize) basefile = self.get_basefile(tex, fontsize) dvifile = '%s.dvi' % basefile if not os.path.exists(dvifile): texfile = self.make_tex(tex, fontsize) with cbook._lock_path(texfile): self._run_checked_subprocess([ "latex", "-interaction=nonstopmode", "--halt-on-error", texfile ], tex) for fname in glob.glob(basefile + '*'): if not fname.endswith(('dvi', 'tex')): try: os.remove(fname) except OSError: pass return dvifile
def compare(self, fig, baseline, extension, *, _lock=False): __tracebackhide__ = True if self.remove_text: remove_ticks_and_titles(fig) actual_path = (self.result_dir / baseline).with_suffix(f'.{extension}') kwargs = self.savefig_kwargs.copy() if extension == 'pdf': kwargs.setdefault('metadata', { 'Creator': None, 'Producer': None, 'CreationDate': None }) lock = (cbook._lock_path(actual_path) if _lock else contextlib.nullcontext()) with lock: try: fig.savefig(actual_path, **kwargs) finally: # Matplotlib has an autouse fixture to close figures, but this # makes things more convenient for third-party users. plt.close(fig) expected_path = self.copy_baseline(baseline, extension) _raise_on_image_difference(expected_path, actual_path, self.tol)
def _rebuild(): global fontManager fontManager = FontManager() if _fmcache: with cbook._lock_path(_fmcache): json_dump(fontManager, _fmcache) _log.debug("generated new fontManager")
def _rebuild(): global fontManager fontManager = FontManager() if _fmcache: with cbook._lock_path(_fmcache): json_dump(fontManager, _fmcache) _log.info("generated new fontManager")
def _clean_conversion_cache(): # This will actually ignore mpl_toolkits baseline images, but they're # relatively small. baseline_images_size = sum( path.stat().st_size for path in Path(mpl.__file__).parent.glob("**/baseline_images/**/*")) # 2x: one full copy of baselines, and one full copy of test results # (actually an overestimate: we don't convert png baselines and results). max_cache_size = 2 * baseline_images_size # Reduce cache until it fits. with cbook._lock_path(_get_cache_path()): cache_stat = { path: path.stat() for path in _get_cache_path().glob("*")} cache_size = sum(stat.st_size for stat in cache_stat.values()) paths_by_atime = sorted( # Oldest at the end. cache_stat, key=lambda path: cache_stat[path].st_atime, reverse=True) while cache_size > max_cache_size: path = paths_by_atime.pop() cache_size -= cache_stat[path].st_size path.unlink()
def compare(self, idx, baseline, extension, *, _lock=False): __tracebackhide__ = True fignum = plt.get_fignums()[idx] fig = plt.figure(fignum) if self.remove_text: remove_ticks_and_titles(fig) actual_path = (self.result_dir / baseline).with_suffix(f'.{extension}') kwargs = self.savefig_kwargs.copy() if extension == 'pdf': kwargs.setdefault('metadata', {'Creator': None, 'Producer': None, 'CreationDate': None}) lock = (cbook._lock_path(actual_path) if _lock else contextlib.nullcontext()) with lock: fig.savefig(actual_path, **kwargs) expected_path = self.copy_baseline(baseline, extension) _raise_on_image_difference(expected_path, actual_path, self.tol)
def json_dump(data, filename): """ Dump `FontManager` *data* as JSON to the file named *filename*. See Also -------- json_load Notes ----- File paths that are children of the Matplotlib data path (typically, fonts shipped with Matplotlib) are stored relative to that data path (to remain valid across virtualenvs). This function temporarily locks the output file to prevent multiple processes from overwriting one another's output. """ with cbook._lock_path(filename), open(filename, 'w') as fh: try: json.dump(data, fh, cls=_JSONEncoder, indent=2) except OSError as e: _log.warning('Could not save font_manager cache {}'.format(e))
def _rebuild(): global fontManager _log.info("Generating new fontManager, this may take some time...") fontManager = FontManager() with cbook._lock_path(_fmcache): json_dump(fontManager, _fmcache)