|
30 | 30 | import subprocess
|
31 | 31 |
|
32 | 32 | from setuptools import setup, find_packages, Extension
|
33 |
| -from setuptools.command.build_ext import build_ext as BuildExtCommand |
34 |
| -from setuptools.command.test import test as TestCommand |
| 33 | +import setuptools.command.build_ext |
| 34 | +import setuptools.command.build_py |
| 35 | +import setuptools.command.test |
| 36 | +import setuptools.command.sdist |
35 | 37 |
|
36 | 38 | # The setuptools version of sdist adds a setup.cfg file to the tree.
|
37 | 39 | # We don't want that, so we simply remove it, and it will fall back to
|
38 | 40 | # vanilla distutils.
|
39 |
| -try: |
40 |
| - from setuptools.command import sdist |
41 |
| -except ImportError: |
42 |
| - pass |
43 |
| -else: |
44 |
| - del sdist.sdist.make_release_tree |
| 41 | +del setuptools.command.sdist.sdist.make_release_tree |
45 | 42 |
|
46 | 43 | from distutils.errors import CompileError
|
47 | 44 | from distutils.dist import Distribution
|
@@ -75,13 +72,13 @@ def has_flag(self, flagname):
|
75 | 72 | return True
|
76 | 73 |
|
77 | 74 |
|
78 |
| -class NoopTestCommand(TestCommand): |
| 75 | +class NoopTestCommand(setuptools.command.test.test): |
79 | 76 | def __init__(self, dist):
|
80 | 77 | print("Matplotlib does not support running tests with "
|
81 | 78 | "'python setup.py test'. Please run 'pytest'.")
|
82 | 79 |
|
83 | 80 |
|
84 |
| -class BuildExtraLibraries(BuildExtCommand): |
| 81 | +class BuildExtraLibraries(setuptools.command.build_ext.build_ext): |
85 | 82 | def finalize_options(self):
|
86 | 83 | self.distribution.ext_modules[:] = [
|
87 | 84 | ext
|
@@ -196,6 +193,34 @@ def build_extensions(self):
|
196 | 193 | return super().build_extensions()
|
197 | 194 |
|
198 | 195 |
|
| 196 | +def update_matplotlibrc(path): |
| 197 | + # Update the matplotlibrc file if packagers want to change the default |
| 198 | + # backend. |
| 199 | + template_lines = path.read_text().splitlines(True) |
| 200 | + backend_line_idx, = [ # Also asserts that there is a single such line. |
| 201 | + idx for idx, line in enumerate(template_lines) |
| 202 | + if line.startswith("#backend:")] |
| 203 | + template_lines[backend_line_idx] = ( |
| 204 | + "#backend: {}".format(setupext.options["backend"]) |
| 205 | + if setupext.options["backend"] |
| 206 | + else "#backend:") |
| 207 | + path.write_text("".join(template_lines)) |
| 208 | + |
| 209 | + |
| 210 | +class BuildPy(setuptools.command.build_py.build_py): |
| 211 | + def run(self): |
| 212 | + super().run() |
| 213 | + update_matplotlibrc( |
| 214 | + Path(self.build_lib, "matplotlib/mpl-data/matplotlibrc")) |
| 215 | + |
| 216 | + |
| 217 | +class Sdist(setuptools.command.sdist.sdist): |
| 218 | + def make_release_tree(self, base_dir, files): |
| 219 | + super().make_release_tree(base_dir, files) |
| 220 | + update_matplotlibrc( |
| 221 | + Path(base_dir, "lib/matplotlib/mpl-data/matplotlibrc")) |
| 222 | + |
| 223 | + |
199 | 224 | package_data = {} # Will be filled below by the various components.
|
200 | 225 |
|
201 | 226 | # If the user just queries for information, don't bother figuring out which
|
@@ -234,18 +259,6 @@ def build_extensions(self):
|
234 | 259 | package_data.setdefault(key, [])
|
235 | 260 | package_data[key] = list(set(val + package_data[key]))
|
236 | 261 |
|
237 |
| - # Write the default matplotlibrc file |
238 |
| - with open('matplotlibrc.template') as fd: |
239 |
| - template_lines = fd.read().splitlines(True) |
240 |
| - backend_line_idx, = [ # Also asserts that there is a single such line. |
241 |
| - idx for idx, line in enumerate(template_lines) |
242 |
| - if line.startswith('#backend:')] |
243 |
| - if setupext.options['backend']: |
244 |
| - template_lines[backend_line_idx] = ( |
245 |
| - 'backend: {}'.format(setupext.options['backend'])) |
246 |
| - with open('lib/matplotlib/mpl-data/matplotlibrc', 'w') as fd: |
247 |
| - fd.write(''.join(template_lines)) |
248 |
| - |
249 | 262 | setup( # Finally, pass this all along to distutils to do the heavy lifting.
|
250 | 263 | name="matplotlib",
|
251 | 264 | description="Python plotting package",
|
@@ -316,5 +329,7 @@ def build_extensions(self):
|
316 | 329 | cmdclass={
|
317 | 330 | "test": NoopTestCommand,
|
318 | 331 | "build_ext": BuildExtraLibraries,
|
| 332 | + "build_py": BuildPy, |
| 333 | + "sdist": Sdist, |
319 | 334 | },
|
320 | 335 | )
|
0 commit comments