1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.sdist import sdist as _sdist
from distutils import log
import os
import sys
import pkg_resources
from io import open
import re
argv = sys.argv[1:]
needs_wheel = {"bdist_wheel"}.intersection(argv)
wheel = ["wheel"] if needs_wheel else []
# check if minimum required Cython is available
cython_version_re = re.compile(r'\s*"cython\s*>=\s*([0-9][0-9\w\.]*)\s*"')
with open("pyproject.toml", "r", encoding="utf-8") as fp:
for line in fp:
m = cython_version_re.match(line)
if m:
cython_min_version = m.group(1)
break
else:
sys.exit("error: could not parse cython version from pyproject.toml")
try:
required_cython = "cython >= %s" % cython_min_version
pkg_resources.require(required_cython)
except pkg_resources.ResolutionError:
with_cython = False
else:
with_cython = True
class cython_build_ext(_build_ext):
"""Compile *.pyx source files to *.c using cythonize if Cython is
installed, else use the pre-generated *.c sources.
"""
def finalize_options(self):
if with_cython:
from Cython.Build import cythonize
# optionally enable line tracing for test coverage support
linetrace = os.environ.get("CYTHON_TRACE") == "1"
self.distribution.ext_modules[:] = cythonize(
self.distribution.ext_modules,
force=linetrace or self.force,
annotate=os.environ.get("CYTHON_ANNOTATE") == "1",
quiet=not self.verbose,
compiler_directives={
"linetrace": linetrace,
"language_level": 3,
"embedsignature": True,
},
include_path=["src"],
)
else:
log.warn(
"%s not installed; using pre-generated *.c sources" % required_cython
)
for ext in self.distribution.ext_modules:
ext.sources = [re.sub(r"\.pyx$", ".c", n) for n in ext.sources]
_build_ext.finalize_options(self)
class cython_sdist(_sdist):
"""Run 'cythonize' on *.pyx sources to ensure the *.c files included
in the source distribution are up-to-date.
"""
def run(self):
if not with_cython:
from distutils.errors import DistutilsSetupError
raise DistutilsSetupError(
"Cython >= %s is required to make sdist" % cython_min_version
)
from Cython.Build import cythonize
cythonize(
self.distribution.ext_modules,
force=True,
quiet=not self.verbose,
compiler_directives={"language_level": 3, "embedsignature": True},
include_path=["src"],
)
_sdist.run(self)
# need to include this for Visual Studio 2008 doesn't have stdint.h
include_dirs = (
[os.path.join(os.path.dirname(__file__), "vendor", "msinttypes")]
if os.name == "nt" and sys.version_info < (3,)
else []
)
cython_modules = ["parser", "util", "writer", "_test"]
extensions = [
Extension(
"openstep_plist." + mod,
sources=["src/openstep_plist/%s.pyx" % mod],
include_dirs=include_dirs,
language="c++",
extra_compile_args=["-std=c++11"] if sys.platform != "win32" else [],
)
for mod in cython_modules
]
with open("README.md", "r") as f:
long_description = f.read()
version_file = os.path.join("src", "openstep_plist", "_version.py")
setup_args = dict(
name="openstep_plist",
use_scm_version={"write_to": version_file},
description="ASCII plist parser written in Cython",
author="Cosimo Lupo",
author_email="cosimo@anthrotype.com",
url="https://github.com/fonttools/openstep-plist",
license="MIT",
long_description=long_description,
long_description_content_type="text/markdown",
package_dir={"": "src"},
packages=find_packages("src"),
include_package_data=True,
exclude_package_data={"": ["*.cpp"]},
ext_modules=extensions,
setup_requires=["setuptools_scm"] + wheel,
python_requires=">=3.8",
cmdclass={"build_ext": cython_build_ext, "sdist": cython_sdist},
zip_safe=False,
)
if __name__ == "__main__":
setup(**setup_args)
|