Skip to content

Commit e4f37fe

Browse files
authored
Merge pull request #223 from davidhewitt/fix-vendor
ci: fix sdist vendor test
2 parents 4b2c877 + 1d0d958 commit e4f37fe

File tree

6 files changed

+57
-45
lines changed

6 files changed

+57
-45
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ jobs:
3434
with:
3535
python-version: "3.x"
3636

37-
- run: pip install tox
37+
- run: pip install nox
3838

39-
- run: tox -e mypy
39+
- run: nox -s mypy
4040

4141
pytest:
4242
runs-on: "ubuntu-latest"
@@ -48,9 +48,9 @@ jobs:
4848
with:
4949
python-version: "3.x"
5050

51-
- run: pip install tox
51+
- run: pip install nox
5252

53-
- run: tox -e pytest
53+
- run: nox -s test
5454

5555
build:
5656
name: ${{ matrix.python-version }} ${{ matrix.platform.os }}-${{ matrix.platform.python-architecture }}
@@ -127,13 +127,9 @@ jobs:
127127
128128
- name: Test sdist vendor Rust crates
129129
shell: bash
130-
run: |
131-
cd examples/namespace_package
132-
python setup.py sdist --vendor-crates
133-
cd dist
134-
tar -zxf namespace_package-0.1.0.tar.gz
135-
cd namespace_package-0.1.0
136-
cargo build --offline --target ${{ matrix.platform.rust-target }}
130+
run: nox -s test-sdist-vendor
131+
env:
132+
CARGO_BUILD_TARGET: ${{ matrix.platform.rust-target }}
137133

138134
test-abi3:
139135
runs-on: ${{ matrix.os }}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixed
6+
- Fix building sdist with vendored dependencies on Windows. [#223](https://github.com/PyO3/setuptools-rust/pull/223)
7+
38
## 1.2.0 (2022-03-22)
49
### Packaging
510
- Drop support for Python 3.6. [#209](https://github.com/PyO3/setuptools-rust/pull/209)

examples/namespace_package/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn rust_func() -> usize {
88

99
/// A Python module implemented in Rust.
1010
#[pymodule]
11-
fn rust(py: Python, m: &PyModule) -> PyResult<()> {
11+
fn rust(_py: Python, m: &PyModule) -> PyResult<()> {
1212
m.add_wrapped(wrap_pyfunction!(rust_func))?;
1313

1414
Ok(())

noxfile.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import os
2+
import tarfile
13
from glob import glob
4+
from pathlib import Path
25

36
import nox
47

@@ -7,3 +10,28 @@
710
def test_examples(session: nox.Session):
811
for example in glob("examples/*/noxfile.py"):
912
session.run("nox", "-f", example, external=True)
13+
14+
15+
@nox.session(name="test-sdist-vendor")
16+
def test_sdist_vendor(session: nox.Session):
17+
session.install(".")
18+
namespace_package = Path(__file__).parent / "examples" / "namespace_package"
19+
os.chdir(namespace_package)
20+
session.run("python", "setup.py", "sdist", "--vendor-crates", external=True)
21+
dist = namespace_package / "dist"
22+
with tarfile.open(str(dist / "namespace_package-0.1.0.tar.gz")) as tf:
23+
tf.extractall(str(dist))
24+
os.chdir(dist / "namespace_package-0.1.0")
25+
session.run("cargo", "build", "--offline", external=True)
26+
27+
28+
@nox.session()
29+
def mypy(session: nox.Session):
30+
session.install("mypy", "fat_macho", "types-setuptools", ".")
31+
session.run("mypy", "setuptools_rust", *session.posargs)
32+
33+
34+
@nox.session()
35+
def test(session: nox.Session):
36+
session.install("pytest", ".")
37+
session.run("pytest", "setuptools_rust", *session.posargs)

setuptools_rust/setuptools_ext.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
from distutils import log
55
from distutils.command.clean import clean
6+
from pathlib import Path
67
from typing import List, Tuple, Type, cast
78

89
from setuptools.command.build_ext import build_ext
@@ -68,18 +69,9 @@ def make_distribution(self) -> None:
6869
# set --manifest-path before vendor_path and after --sync to workaround that
6970
# See https://docs.rs/clap/latest/clap/struct.Arg.html#method.multiple for detail
7071
command.extend(["--manifest-path", manifest_paths[0], vendor_path])
71-
cargo_config = subprocess.check_output(command)
72-
base_dir_bytes = (
73-
base_dir.encode(sys.getfilesystemencoding()) + os.sep.encode()
74-
)
75-
if os.sep == "\\":
76-
# TOML escapes backslash \
77-
base_dir_bytes += os.sep.encode()
78-
cargo_config = cargo_config.replace(base_dir_bytes, b"")
79-
if os.altsep:
80-
cargo_config = cargo_config.replace(
81-
base_dir_bytes + os.altsep.encode(), b""
82-
)
72+
subprocess.run(command, check=True)
73+
74+
cargo_config = _CARGO_VENDOR_CONFIG
8375

8476
# Check whether `.cargo/config`/`.cargo/config.toml` already exists
8577
existing_cargo_config = None
@@ -90,18 +82,18 @@ def make_distribution(self) -> None:
9082
if filename in self.filelist.files:
9183
existing_cargo_config = filename
9284
break
85+
9386
if existing_cargo_config:
9487
cargo_config_path = os.path.join(
9588
base_dir, existing_cargo_config
9689
)
9790
# Append vendor config to original cargo config
9891
with open(existing_cargo_config, "rb") as f:
99-
cargo_config = f.read() + b"\n" + cargo_config
92+
cargo_config += f.read() + b"\n"
10093

10194
with open(cargo_config_path, "wb") as f:
10295
f.write(cargo_config)
103-
self.filelist.append(vendor_path)
104-
self.filelist.append(cargo_config_path)
96+
10597
super().make_distribution()
10698

10799
dist.cmdclass["sdist"] = sdist_rust_extension
@@ -287,3 +279,12 @@ def rust_extensions(
287279
if has_rust_extensions:
288280
patch_distutils_build()
289281
add_rust_extension(dist)
282+
283+
284+
_CARGO_VENDOR_CONFIG = b"""
285+
[source.crates-io]
286+
replace-with = "vendored-sources"
287+
288+
[source.vendored-sources]
289+
directory = ".cargo/vendor"
290+
"""

tox.ini

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)