|
| 1 | +from __future__ import print_function, absolute_import |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +from distutils.cmd import Command |
| 6 | +from distutils.errors import ( |
| 7 | + CompileError, DistutilsFileError, DistutilsExecError) |
| 8 | + |
| 9 | +import semantic_version |
| 10 | + |
| 11 | +from .extension import RustExtension |
| 12 | +from .utils import cpython_feature, get_rust_version |
| 13 | + |
| 14 | +MIN_VERSION = semantic_version.Spec('>=1.15') |
| 15 | + |
| 16 | + |
| 17 | +class test_rust(Command): |
| 18 | + """ Run cargo test""" |
| 19 | + |
| 20 | + description = "test rust extensions" |
| 21 | + |
| 22 | + user_options = [] |
| 23 | + |
| 24 | + def initialize_options(self): |
| 25 | + self.extensions = () |
| 26 | + |
| 27 | + def finalize_options(self): |
| 28 | + self.extensions = [ext for ext in self.distribution.rust_extensions |
| 29 | + if isinstance(ext, RustExtension)] |
| 30 | + |
| 31 | + def run(self): |
| 32 | + if not self.extensions: |
| 33 | + return |
| 34 | + |
| 35 | + version = get_rust_version() |
| 36 | + if version not in MIN_VERSION: |
| 37 | + print('Rust version mismatch: required rust%s got rust%s' % ( |
| 38 | + MIN_VERSION, version)) |
| 39 | + return |
| 40 | + |
| 41 | + # Make sure that if pythonXX-sys is used, it builds against the current |
| 42 | + # executing python interpreter. |
| 43 | + bindir = os.path.dirname(sys.executable) |
| 44 | + |
| 45 | + env = os.environ.copy() |
| 46 | + env.update({ |
| 47 | + # disables rust's pkg-config seeking for specified packages, |
| 48 | + # which causes pythonXX-sys to fall back to detecting the |
| 49 | + # interpreter from the path. |
| 50 | + "PYTHON_2.7_NO_PKG_CONFIG": "1", |
| 51 | + "PATH": bindir + os.pathsep + os.environ.get("PATH", "") |
| 52 | + }) |
| 53 | + |
| 54 | + for ext in self.extensions: |
| 55 | + if not os.path.exists(ext.path): |
| 56 | + raise DistutilsFileError( |
| 57 | + "Can not file rust extension project file: %s" % ext.path) |
| 58 | + |
| 59 | + features = set(ext.features) |
| 60 | + features.update(cpython_feature(ext=False)) |
| 61 | + |
| 62 | + # build cargo command |
| 63 | + args = (["cargo", "test", "--manifest-path", ext.path, |
| 64 | + "--features", " ".join(features)] |
| 65 | + + list(ext.args or [])) |
| 66 | + |
| 67 | + # Execute cargo command |
| 68 | + print(' '.join(args)) |
| 69 | + try: |
| 70 | + subprocess.check_output(args) |
| 71 | + except subprocess.CalledProcessError as e: |
| 72 | + raise CompileError( |
| 73 | + "cargo failed with code: %d\n%s" % ( |
| 74 | + e.returncode, e.output.decode("utf-8"))) |
| 75 | + except OSError: |
| 76 | + raise DistutilsExecError( |
| 77 | + "Unable to execute 'cargo' - this package " |
| 78 | + "requires rust to be installed and " |
| 79 | + "cargo to be on the PATH") |
| 80 | + else: |
| 81 | + print("Test has been completed for '%s' extension" % ext.name) |
0 commit comments