Skip to content

Instantly share code, notes, and snippets.

@Natureshadow
Last active March 24, 2017 18:13
Show Gist options
  • Save Natureshadow/92f30ce650682a638b0bc75e6d54e8b4 to your computer and use it in GitHub Desktop.
Save Natureshadow/92f30ce650682a638b0bc75e6d54e8b4 to your computer and use it in GitHub Desktop.
setuptools_requires_alternative.py
# © 2017 Dominik George <nik@naturalnet.de>
# Choose any OSI licence you like.
from pkg_resources import get_distribution
def requires_alternative(*alternatives):
""" Allows specifying alternative requirements.
This function takes arbitrarily many distribution specifications as arguments.
It then checks for availability of each. If a distribution is found, the original
specification for it is returned. If none of the alternatives is available,
the first specified one is returned, causing setuptools to install it.
"""
# Allow calling with both a list and several arguments
if not isinstance(alternatives[0], str):
alternatives = alternatives[0]
# Iterate over all listed alternatives
for alternative in alternatives:
# Try to find a distribution with this specification
try:
get_distribution(alternative)
# If we got here, return this specification as requirement,
# it was the first we found
return alternative
except:
# Not found, go on
continue
# If we got here, no alternative is installed yet, so return the first
return alternatives[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment