Last active
December 23, 2015 17:39
-
-
Save mdboom/6670408 to your computer and use it in GitHub Desktop.
Dead simple setup.py to experiment with setuptools' setup_requires feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "numpy/arrayobject.h" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distribute_setup import use_setuptools | |
use_setuptools() | |
from setuptools import setup | |
from setuptools.extension import Extension | |
class DelayedExtension(Extension, object): | |
""" | |
A distutils Extension subclass where some of its members | |
may have delayed computation until reaching the build phase. | |
This is so we can, for example, get the Numpy include dirs | |
after pip has installed Numpy for us if it wasn't already | |
on the system. | |
""" | |
def __init__(self, *args, **kwargs): | |
super(DelayedExtension, self).__init__(*args, **kwargs) | |
self._finalized = False | |
self._hooks = {} | |
def add_hook(self, member, func): | |
""" | |
Add a hook to dynamically compute a member. | |
Parameters | |
---------- | |
member : string | |
The name of the member | |
func : callable | |
The function to call to get dynamically-computed values | |
for the member. | |
""" | |
self._hooks[member] = func | |
def finalize(self): | |
self._finalized = True | |
class DelayedMember(property): | |
def __init__(self, name): | |
self._name = name | |
def __get__(self, obj, objtype=None): | |
result = getattr(obj, '_' + self._name, []) | |
if obj._finalized: | |
if self._name in obj._hooks: | |
result = obj._hooks[self._name]() + result | |
return result | |
def __set__(self, obj, value): | |
setattr(obj, '_' + self._name, value) | |
include_dirs = DelayedMember('include_dirs') | |
ext = DelayedExtension("foo", ["foo.c"]) | |
distrib = setup( | |
name="dependencies", | |
ext_modules=[ext], | |
setup_requires=["numpy"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment