Skip to content

Commit a57c9a1

Browse files
authored
Merge pull request #16177 from anntzer/simplesetup
Further simplify setupext.
2 parents f6e94f8 + f56520e commit a57c9a1

File tree

3 files changed

+27
-56
lines changed

3 files changed

+27
-56
lines changed

setup.cfg.template

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,11 @@
1818

1919
[gui_support]
2020
# Matplotlib supports multiple GUI toolkits, known as backends.
21-
# The Mac OSX backend requires the Cocoa headers included with XCode.
21+
# The MacOSX backend requires the Cocoa headers included with XCode.
2222
# You can select whether to build it by uncommenting the following line.
23-
# Acceptable values are:
23+
# It is never built on Linux or Windows, regardless of the config value.
2424
#
25-
# True: build the extension. Exits with a warning if the
26-
# required dependencies are not available
27-
# False: do not build the extension
28-
# auto: build if the required dependencies are available,
29-
# otherwise skip silently. This is the default
30-
# behavior
31-
#
32-
#macosx = auto
25+
#macosx = True
3326

3427
[rc_options]
3528
# User-configurable options

setup.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,13 @@ def run(self):
189189
good_packages = []
190190
for package in mpl_packages:
191191
try:
192-
result = package.check()
193-
if result is not None:
194-
print_status(package.name, 'yes [%s]' % result)
195-
except setupext.CheckFailed as e:
196-
print_status(package.name, 'no [%s]' % str(e))
197-
if not package.optional:
198-
sys.exit("Failed to build %s" % package.name)
199-
else:
200-
good_packages.append(package)
192+
message = package.check()
193+
except setupext.Skipped as e:
194+
print_status(package.name, f"no [{e}]")
195+
continue
196+
if message is not None:
197+
print_status(package.name, f"yes [{message}]")
198+
good_packages.append(package)
201199

202200
print_raw()
203201

setupext.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -228,38 +228,38 @@ def pkg_config_setup_extension(
228228
ext.libraries.extend(default_libraries)
229229

230230

231-
class CheckFailed(Exception):
231+
class Skipped(Exception):
232232
"""
233-
Exception thrown when a `SetupPackage.check` method fails.
233+
Exception thrown by `SetupPackage.check` to indicate that a package should
234+
be skipped.
234235
"""
235-
pass
236236

237237

238238
class SetupPackage:
239-
optional = False
240239

241240
def check(self):
242241
"""
243-
Checks whether the build dependencies are met. Should raise a
244-
`CheckFailed` exception if the dependency could not be met, otherwise
245-
return a string indicating a version number or some other message
246-
indicating what was found.
242+
If the package should be installed, return an informative string, or
243+
None if no information should be displayed at all.
244+
245+
If the package should be skipped, raise a `Skipped` exception.
246+
247+
If a missing build dependency is fatal, call `sys.exit`.
247248
"""
248-
pass
249249

250250
def get_package_data(self):
251251
"""
252252
Get a package data dictionary to add to the configuration.
253-
These are merged into to the `package_data` list passed to
254-
`distutils.setup`.
253+
These are merged into to the *package_data* list passed to
254+
`setuptools.setup`.
255255
"""
256256
return {}
257257

258258
def get_extension(self):
259259
"""
260260
Get a list of C extensions (`distutils.core.Extension`
261261
objects) to add to the configuration. These are added to the
262-
`extensions` list passed to `distutils.setup`.
262+
*extensions* list passed to `setuptools.setup`.
263263
"""
264264
return None
265265

@@ -269,43 +269,23 @@ def do_custom_build(self):
269269
third-party library, before building an extension, it should
270270
override this method.
271271
"""
272-
pass
273272

274273

275274
class OptionalPackage(SetupPackage):
276-
optional = True
277275
config_category = "packages"
278-
default_config = "auto"
279-
280-
@classmethod
281-
def get_config(cls):
282-
"""
283-
Look at `setup.cfg` and return one of ["auto", True, False] indicating
284-
if the package is at default state ("auto"), forced by the user (case
285-
insensitively defined as 1, true, yes, on for True) or opted-out (case
286-
insensitively defined as 0, false, no, off for False).
287-
"""
288-
conf = cls.default_config
289-
if config.has_option(cls.config_category, cls.name):
290-
try:
291-
conf = config.getboolean(cls.config_category, cls.name)
292-
except ValueError:
293-
conf = config.get(cls.config_category, cls.name)
294-
return conf
276+
default_config = True
295277

296278
def check(self):
297279
"""
298280
Check whether ``setup.cfg`` requests this package to be installed.
299281
300282
May be overridden by subclasses for additional checks.
301283
"""
302-
conf = self.get_config() # Check configuration file
303-
if conf in [True, 'auto']: # Default "auto", or install forced by user
304-
if conf is True: # Set non-optional if user sets `True` in config
305-
self.optional = False
284+
if config.getboolean(self.config_category, self.name,
285+
fallback=self.default_config):
306286
return "installing"
307287
else: # Configuration opt-out by user
308-
raise CheckFailed("skipping due to configuration")
288+
raise Skipped("skipping due to configuration")
309289

310290

311291
class Platform(SetupPackage):
@@ -722,7 +702,7 @@ class BackendMacOSX(OptionalPackage):
722702

723703
def check(self):
724704
if sys.platform != 'darwin':
725-
raise CheckFailed("Mac OS-X only")
705+
raise Skipped("Mac OS-X only")
726706
return super().check()
727707

728708
def get_extension(self):

0 commit comments

Comments
 (0)