From c3fee87deea9743ebf13239017910f30d7382286 Mon Sep 17 00:00:00 2001 From: Paul Ganssle Date: Wed, 24 Apr 2019 10:34:41 -0400 Subject: [PATCH] Add custom sdist command to modify Cargo.toml This enables PEP 517 builds from an sdist, and should fix `tox` builds using `pip >= 19.0`. This does not fix pip installing the directory; pinning to `pip < 19.0` is still the best strategy for that. --- examples/rustapi_module/MANIFEST.in | 2 ++ examples/rustapi_module/pyproject.toml | 2 +- examples/rustapi_module/setup.py | 42 +++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 examples/rustapi_module/MANIFEST.in diff --git a/examples/rustapi_module/MANIFEST.in b/examples/rustapi_module/MANIFEST.in new file mode 100644 index 00000000..becccf7b --- /dev/null +++ b/examples/rustapi_module/MANIFEST.in @@ -0,0 +1,2 @@ +include pyproject.toml Cargo.toml +recursive-include src * diff --git a/examples/rustapi_module/pyproject.toml b/examples/rustapi_module/pyproject.toml index 0f58585a..2176db0b 100644 --- a/examples/rustapi_module/pyproject.toml +++ b/examples/rustapi_module/pyproject.toml @@ -1,2 +1,2 @@ [build-system] -requires = ["setuptools", "wheel", "setuptools_rust>=0.10.2"] +requires = ["setuptools", "wheel", "setuptools_rust>=0.10.2", "toml"] diff --git a/examples/rustapi_module/setup.py b/examples/rustapi_module/setup.py index 278ab4a3..2dbaebb0 100644 --- a/examples/rustapi_module/setup.py +++ b/examples/rustapi_module/setup.py @@ -1,8 +1,10 @@ +import os import sys import platform from setuptools import setup from setuptools.command.test import test as TestCommand +from setuptools.command.sdist import sdist as SdistCommand from setuptools_rust import RustExtension @@ -18,6 +20,41 @@ class PyTest(TestCommand): raise SystemExit(errno) +class CargoModifiedSdist(SdistCommand): + """Modifies Cargo.toml to use an absolute rather than a relative path + + The current implementation of PEP 517 in pip always does builds in an + isolated temporary directory. This causes problems with the build, because + Cargo.toml necessarily refers to the current version of pyo3 by a relative + path. + + Since these sdists are never meant to be used for anything other than + tox / pip installs, at sdist build time, we will modify the Cargo.toml + in the sdist archive to include an *absolute* path to pyo3. + """ + + def make_release_tree(self, base_dir, files): + """Stages the files to be included in archives""" + super().make_release_tree(base_dir, files) + + import toml + # Cargo.toml is now staged and ready to be modified + cargo_loc = os.path.join(base_dir, 'Cargo.toml') + assert os.path.exists(cargo_loc) + + with open(cargo_loc, 'r') as f: + cargo_toml = toml.load(f) + + rel_pyo3_path = cargo_toml['dependencies']['pyo3']['path'] + base_path = os.path.dirname(__file__) + abs_pyo3_path = os.path.abspath(os.path.join(base_path, rel_pyo3_path)) + + cargo_toml['dependencies']['pyo3']['path'] = abs_pyo3_path + + with open(cargo_loc, 'w') as f: + toml.dump(cargo_toml, f) + + def get_py_version_cfgs(): # For now each Cfg Py_3_X flag is interpreted as "at least 3.X" version = sys.version_info[0:2] @@ -70,5 +107,8 @@ setup( tests_require=tests_require, include_package_data=True, zip_safe=False, - cmdclass=dict(test=PyTest), + cmdclass={ + 'test': PyTest, + 'sdist': CargoModifiedSdist, + }, )