From 2a3c65a5def12ae6fdac980e33f50c6d18c02771 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 28 Feb 2021 17:06:29 +0100 Subject: [PATCH] Remove matplotlibrc.template. --- .gitignore | 2 - .../matplotlib/mpl-data/matplotlibrc | 0 lib/matplotlib/tests/test_rcparams.py | 6 +- setup.py | 59 ++++++++++++------- tutorials/introductory/customizing.py | 2 +- 5 files changed, 43 insertions(+), 26 deletions(-) rename matplotlibrc.template => lib/matplotlib/mpl-data/matplotlibrc (100%) diff --git a/.gitignore b/.gitignore index e2b3a00f34ee..87583e5ef767 100644 --- a/.gitignore +++ b/.gitignore @@ -56,8 +56,6 @@ Thumbs.db # Things specific to this project # ################################### -lib/matplotlib/mpl-data/matplotlib.conf -lib/matplotlib/mpl-data/matplotlibrc tutorials/intermediate/CL01.png tutorials/intermediate/CL02.png diff --git a/matplotlibrc.template b/lib/matplotlib/mpl-data/matplotlibrc similarity index 100% rename from matplotlibrc.template rename to lib/matplotlib/mpl-data/matplotlibrc diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 4705b975d60a..3b8077a580c1 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -496,7 +496,11 @@ def test_backend_fallback_headful(tmpdir): env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmpdir)} backend = subprocess.check_output( [sys.executable, "-c", - "import matplotlib.pyplot; print(matplotlib.get_backend())"], + "import matplotlib as mpl; " + "assert dict.__getitem__(mpl.rcParams, 'backend') == " + "mpl.rcsetup._auto_backend_sentinel; " + "import matplotlib.pyplot; " + "print(matplotlib.get_backend())"], env=env, universal_newlines=True) # The actual backend will depend on what's installed, but at least tkagg is # present. diff --git a/setup.py b/setup.py index 0879aa37b9a2..120ba41468d3 100644 --- a/setup.py +++ b/setup.py @@ -30,18 +30,15 @@ import subprocess from setuptools import setup, find_packages, Extension -from setuptools.command.build_ext import build_ext as BuildExtCommand -from setuptools.command.test import test as TestCommand +import setuptools.command.build_ext +import setuptools.command.build_py +import setuptools.command.test +import setuptools.command.sdist # The setuptools version of sdist adds a setup.cfg file to the tree. # We don't want that, so we simply remove it, and it will fall back to # vanilla distutils. -try: - from setuptools.command import sdist -except ImportError: - pass -else: - del sdist.sdist.make_release_tree +del setuptools.command.sdist.sdist.make_release_tree from distutils.errors import CompileError from distutils.dist import Distribution @@ -75,13 +72,13 @@ def has_flag(self, flagname): return True -class NoopTestCommand(TestCommand): +class NoopTestCommand(setuptools.command.test.test): def __init__(self, dist): print("Matplotlib does not support running tests with " "'python setup.py test'. Please run 'pytest'.") -class BuildExtraLibraries(BuildExtCommand): +class BuildExtraLibraries(setuptools.command.build_ext.build_ext): def finalize_options(self): self.distribution.ext_modules[:] = [ ext @@ -196,6 +193,34 @@ def build_extensions(self): return super().build_extensions() +def update_matplotlibrc(path): + # Update the matplotlibrc file if packagers want to change the default + # backend. + template_lines = path.read_text().splitlines(True) + backend_line_idx, = [ # Also asserts that there is a single such line. + idx for idx, line in enumerate(template_lines) + if line.startswith("#backend:")] + template_lines[backend_line_idx] = ( + "#backend: {}".format(setupext.options["backend"]) + if setupext.options["backend"] + else "#backend:") + path.write_text("".join(template_lines)) + + +class BuildPy(setuptools.command.build_py.build_py): + def run(self): + super().run() + update_matplotlibrc( + Path(self.build_lib, "matplotlib/mpl-data/matplotlibrc")) + + +class Sdist(setuptools.command.sdist.sdist): + def make_release_tree(self, base_dir, files): + super().make_release_tree(base_dir, files) + update_matplotlibrc( + Path(base_dir, "lib/matplotlib/mpl-data/matplotlibrc")) + + package_data = {} # Will be filled below by the various components. # If the user just queries for information, don't bother figuring out which @@ -234,18 +259,6 @@ def build_extensions(self): package_data.setdefault(key, []) package_data[key] = list(set(val + package_data[key])) - # Write the default matplotlibrc file - with open('matplotlibrc.template') as fd: - template_lines = fd.read().splitlines(True) - backend_line_idx, = [ # Also asserts that there is a single such line. - idx for idx, line in enumerate(template_lines) - if line.startswith('#backend:')] - if setupext.options['backend']: - template_lines[backend_line_idx] = ( - 'backend: {}'.format(setupext.options['backend'])) - with open('lib/matplotlib/mpl-data/matplotlibrc', 'w') as fd: - fd.write(''.join(template_lines)) - setup( # Finally, pass this all along to distutils to do the heavy lifting. name="matplotlib", description="Python plotting package", @@ -316,5 +329,7 @@ def build_extensions(self): cmdclass={ "test": NoopTestCommand, "build_ext": BuildExtraLibraries, + "build_py": BuildPy, + "sdist": Sdist, }, ) diff --git a/tutorials/introductory/customizing.py b/tutorials/introductory/customizing.py index 993692bc8dd0..a05e5f023e73 100644 --- a/tutorials/introductory/customizing.py +++ b/tutorials/introductory/customizing.py @@ -200,7 +200,7 @@ # A sample matplotlibrc file # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # -# .. literalinclude:: ../../../matplotlibrc.template +# .. literalinclude:: ../../../lib/matplotlib/mpl-data/matplotlibrc # # # .. _ggplot: https://ggplot2.tidyverse.org/