Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring setup.py to enable dry pip install #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import os.path
import lxml
import sys

from setuptools import setup
from numpy import get_include
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext


def find_libxml2_include():
Expand All @@ -39,28 +35,38 @@ def find_libxml2_include():
include_dirs.append(d)
return include_dirs


class CustomBuildExt(build_ext):
def finalize_options(self):
build_ext.finalize_options(self)

__builtins__.__NUMPY_SETUP__ = False

import Cython
import lxml
import numpy

include_dirs = [numpy.get_include()] + lxml.get_include() + find_libxml2_include()

self.include_dirs.extend(include_dirs)

ext_modules = [
Extension('dragnet.lcs',
sources=["dragnet/lcs.pyx"],
include_dirs=[get_include()],
sources=["dragnet/lcs.cpp"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work without checking in the generated C++ files?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you mean. Writing this is in fact unrolling what the cythonize function does under the hood with source names etc.

Copy link
Contributor

@b4hand b4hand Apr 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but I didn't see any call to cythonize so I wasn't sure if it was still generating those files or if those files needed to be checked in to be found.

I've seen other projects just check in the generated files to avoid using cythonize during setup.py. The downside is you have to remember to update those before checking in and it makes the diffs larger.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I see now. I did not understand "checking" this way, hence the confusion.

Copy link
Contributor

@b4hand b4hand Apr 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run on a completely clean virtualenv:

pip install git+https://github.com/Yomguithereal/dragnet.git@setup-build-enhancements

The command fails, and I see the following error:

  clang: error: no such file or directory: 'dragnet/lcs.cpp'
  clang: error: no input files
  error: command 'clang' failed with exit status 1

This means that the .cpp files need to be generated before the Extension commands can be called. I'm not opposed to pregenerating the .cpp files, but then we need to ensure they are regenerated after any changes to the .pyx files.

language="c++"),
Extension('dragnet.blocks',
sources=["dragnet/blocks.pyx"],
include_dirs=(lxml.get_include() + find_libxml2_include()),
sources=["dragnet/blocks.cpp"],
language="c++",
libraries=['xml2']),
Extension('dragnet.features._readability',
sources=["dragnet/features/_readability.pyx"],
include_dirs=[get_include()],
sources=["dragnet/features/_readability.cpp"],
extra_compile_args=['-std=c++11'] + (['-mmacosx-version-min=10.9'] if sys.platform.startswith("darwin") else []),
language="c++"),
Extension('dragnet.features._kohlschuetter',
sources=["dragnet/features/_kohlschuetter.pyx"],
include_dirs=[get_include()],
sources=["dragnet/features/_kohlschuetter.cpp"],
language="c++"),
Extension('dragnet.features._weninger',
sources=["dragnet/features/_weninger.pyx"],
include_dirs=[get_include()],
sources=["dragnet/features/_weninger.cpp"],
language="c++"),
]

Expand Down Expand Up @@ -93,8 +99,13 @@ def find_libxml2_include():
packages=['dragnet', 'dragnet.features'],
package_dir={'dragnet': 'dragnet', 'dragnet.features': 'dragnet/features'},
package_data={'dragnet': ['pickled_models/*/*']},
cmdclass={'build_ext': build_ext},
ext_modules=cythonize(ext_modules),
cmdclass={'build_ext': CustomBuildExt},
ext_modules=ext_modules,
setup_requires=[
'Cython>=0.21.1',
'lxml',
'numpy'
],
install_requires=[
'Cython>=0.21.1',
'lxml',
Expand Down