|
5 | 5 |
|
6 | 6 | from setuptools.command.build_ext import build_ext
|
7 | 7 | from setuptools.command.install import install
|
| 8 | +from distutils.command.sdist import sdist |
| 9 | +import subprocess |
8 | 10 |
|
9 | 11 | try:
|
10 | 12 | from wheel.bdist_wheel import bdist_wheel
|
|
13 | 15 |
|
14 | 16 |
|
15 | 17 | def add_rust_extension(dist):
|
| 18 | + sdist_base_class = dist.cmdclass.get("sdist", sdist) |
| 19 | + sdist_options = sdist_base_class.user_options.copy() |
| 20 | + sdist_boolean_options = sdist_base_class.boolean_options.copy() |
| 21 | + sdist_negative_opt = sdist_base_class.negative_opt.copy() |
| 22 | + sdist_options.extend([ |
| 23 | + ('vendor-crates', None, |
| 24 | + "vendor Rust crates"), |
| 25 | + ('no-vendor-crates', None, |
| 26 | + "don't vendor Rust crates." |
| 27 | + "[default; enable with --vendor-crates]"), |
| 28 | + ]) |
| 29 | + sdist_boolean_options.append('vendor-crates') |
| 30 | + sdist_negative_opt['no-vendor-crates'] = 'vendor-crates' |
| 31 | + |
| 32 | + class sdist_rust_extension(sdist_base_class): |
| 33 | + user_options = sdist_options |
| 34 | + boolean_options = sdist_boolean_options |
| 35 | + negative_opt = sdist_negative_opt |
| 36 | + |
| 37 | + def initialize_options(self): |
| 38 | + super().initialize_options() |
| 39 | + self.vendor_crates = 0 |
| 40 | + |
| 41 | + def get_file_list(self): |
| 42 | + if self.vendor_crates: |
| 43 | + base_dir = self.distribution.get_fullname() |
| 44 | + vendor_path = os.path.join(base_dir, "vendored-crates") |
| 45 | + cargo_config = subprocess.check_output(["cargo", "vendor", vendor_path]) |
| 46 | + cargo_config = cargo_config.replace(base_dir.encode() + b'/', b'') |
| 47 | + dot_cargo_path = os.path.join(base_dir, ".cargo") |
| 48 | + self.mkpath(dot_cargo_path) |
| 49 | + cargo_config_path = os.path.join(dot_cargo_path, "config.toml") |
| 50 | + with open(cargo_config_path, "wb") as f: |
| 51 | + f.write(cargo_config) |
| 52 | + self.filelist.append(vendor_path) |
| 53 | + self.filelist.append(cargo_config_path) |
| 54 | + super().get_file_list() |
| 55 | + dist.cmdclass["sdist"] = sdist_rust_extension |
| 56 | + |
16 | 57 | build_ext_base_class = dist.cmdclass.get('build_ext', build_ext)
|
17 | 58 |
|
18 | 59 | class build_ext_rust_extension(build_ext_base_class):
|
|
0 commit comments