Skip to content

Commit 3d75a9b

Browse files
committed
use different semver lib; added readme info about RustExtension
1 parent d741712 commit 3d75a9b

File tree

6 files changed

+64
-33
lines changed

6 files changed

+64
-33
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ CHANGES
88

99
- Add rust version check for extension.
1010

11+
- Cleanup example project
12+
1113

1214
0.2 (2017-03-08)
1315
----------------

README.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ Or you can use commands like `bdist_wheel` or `bdist_egg`
5050
RustExtension
5151
-------------
5252

53+
You can define rust extension with `RustExtension` class.
54+
55+
.. class:: RustExtension(name, path, args=None, features=None, rust_version=None, \
56+
quiet=False, debug=False)
57+
58+
The class for creating rust extensions.
59+
60+
:param str name: the full name of the extension, including any packages -- ie.
61+
*not* a filename or pathname, but Python dotted name
62+
63+
:param str path: path to the Cargo.toml manifest file
64+
65+
:param [str] args: a list of extra argumenents to be passed to cargo.
66+
67+
:param [str] features: a list of features to also build
68+
69+
:param str rust_version: sematic version of rust compiler version -- for example
70+
*>1.14,<1.16*, default is None
71+
72+
:param bool quiet: Does not echo cargo's output. default is False
73+
74+
:param bool debug: Controls whether --debug or --release is passed to cargo.
75+
5376

5477

5578

example/extensions/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ default-features = false
99

1010
[lib]
1111
name = "helloworld"
12-
crate-type = ["dylib"]
12+
crate-type = ["cdylib"]

example/extensions/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
66

7-
py_module_initializer!(_helloworld, init_helloworld, PyInit_helloworld, |py, m| {
7+
py_module_initializer!(_helloworld, init_helloworld, PyInit__helloworld, |py, m| {
88
try!(m.add(py, "__doc__", "Module documentation string"));
99
try!(m.add(py, "run", py_fn!(py, run(*args, **kwargs))));
1010
try!(m.add(py, "val", py_fn!(py, val())));

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup
22

3-
version = '0.2'
3+
version = '0.3'
44

55

66
setup(
@@ -15,7 +15,7 @@
1515
(open('README.rst').read(), open('CHANGES.rst').read())),
1616
license='MIT',
1717
packages=['setuptools_rust'],
18-
install_requires=['semver==2.7.6'],
18+
install_requires=['semantic_version==2.6.0'],
1919
zip_safe=True,
2020
classifiers=[
2121
"Topic :: Software Development :: Version Control",

setuptools_rust/__init__.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import subprocess
77
from distutils.cmd import Command
88
from distutils.errors import (
9-
CompileError,
10-
DistutilsExecError, DistutilsFileError, DistutilsPlatformError)
9+
CompileError, DistutilsExecError, DistutilsFileError,
10+
DistutilsPlatformError, DistutilsSetupError)
1111

12-
import semver
12+
import semantic_version
1313

1414
from . import patch # noqa
1515

@@ -25,7 +25,7 @@ class RustExtension:
2525
the full name of the extension, including any packages -- ie.
2626
*not* a filename or pathname, but Python dotted name
2727
path : string
28-
path to the cargo.toml manifest
28+
path to the cargo.toml manifest file
2929
args : [string]
3030
a list of extra argumenents to be passed to cargo.
3131
features : [string]
@@ -53,26 +53,31 @@ def __init__(self, name, path,
5353

5454
self.features = [s.strip() for s in features]
5555

56-
@staticmethod
57-
def get_rust_version():
58-
env = os.environ.copy()
59-
try:
60-
output = subprocess.check_output(["rustc", "-V"], env=env)
61-
return output.split(' ')[1]
62-
except subprocess.CalledProcessError:
63-
return None
64-
except OSError:
56+
def get_rust_version(self):
57+
if self.rust_version is None:
6558
return None
59+
try:
60+
return semantic_version.Spec(self.rust_version)
61+
except:
62+
raise DistutilsSetupError(
63+
'Can not parse rust compiler version: %s', self.rust_version)
6664

6765

68-
class build_rust(Command):
69-
"""
70-
Command for building rust crates via cargo.
66+
def get_rust_version():
67+
try:
68+
output = subprocess.check_output(["rustc", "-V"])
69+
if isinstance(output, bytes):
70+
output = output.decode('latin-1')
71+
return semantic_version.Version(output.split(' ')[1], partial=True)
72+
except (subprocess.CalledProcessError, OSError):
73+
raise DistutilsPlatformError('Can not find Rust compiler')
74+
except Exception as exc:
75+
raise DistutilsPlatformError(
76+
'Can not get rustc version: %s' % str(exc))
7177

72-
Don't use this directly; use the build_rust_cmdclass
73-
factory method.
74-
"""
75-
description = "build rust crates into Python extensions"
78+
79+
class build_rust(Command):
80+
""" Command for building rust crates via cargo. """
7681

7782
user_options = [
7883
('inplace', 'i',
@@ -140,7 +145,7 @@ def build_extension(self, ext):
140145
print(output, file=sys.stderr)
141146

142147
# Find the shared library that cargo hopefully produced and copy
143-
# it into the build directory as if it were produced by build_cext.
148+
# it into the build directory as if it were produced by build_ext.
144149
if ext.debug:
145150
suffix = "debug"
146151
else:
@@ -179,15 +184,16 @@ def build_extension(self, ext):
179184
shutil.copyfile(dylib_path, ext_path)
180185

181186
def run(self):
182-
version = RustExtension.get_rust_version()
183-
if self.extensions and version is None:
184-
raise DistutilsPlatformError('Can not find Rust compiler')
187+
if not self.extensions:
188+
return
189+
190+
version = get_rust_version()
185191

186192
for ext in self.extensions:
187-
if ext.rust_version is not None:
188-
if not semver.match(version, ext.rust_version):
189-
raise DistutilsPlatformError(
190-
"Rust %s does not match extension requirenment %s" % (
191-
version, ext.rust_version))
193+
rust_version = ext.get_rust_version()
194+
if rust_version is not None and version not in rust_version:
195+
raise DistutilsPlatformError(
196+
"Rust %s does not match extension requirenment %s" % (
197+
version, ext.rust_version))
192198

193199
self.build_extension(ext)

0 commit comments

Comments
 (0)