|
| 1 | +# coding: utf-8 |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import glob |
| 5 | +import os |
| 6 | +import string |
| 7 | + |
| 8 | +try: |
| 9 | + import configparser |
| 10 | +except ImportError: |
| 11 | + import ConfigParser as configparser |
| 12 | + |
| 13 | +import setuptools |
| 14 | + |
| 15 | +from distutils import log |
| 16 | +from distutils.command.build import build |
| 17 | + |
| 18 | +from .extension import RustExtension |
| 19 | + |
| 20 | + |
| 21 | +__all__ = ["tomlgen"] |
| 22 | + |
| 23 | + |
| 24 | +class tomlgen_rust(setuptools.Command): |
| 25 | + |
| 26 | + user_options = [ |
| 27 | + |
| 28 | + ] |
| 29 | + |
| 30 | + def initialize_options(self): |
| 31 | + |
| 32 | + self.dependencies = None |
| 33 | + self.authors = None |
| 34 | + self.create_workspace = False |
| 35 | + |
| 36 | + # command to find build directories |
| 37 | + self.build = build(self.distribution) |
| 38 | + |
| 39 | + # parse config files |
| 40 | + self.cfg = configparser.ConfigParser() |
| 41 | + self.cfg.read(self.distribution.find_config_files()) |
| 42 | + |
| 43 | + def finalize_options(self): |
| 44 | + |
| 45 | + # Finalize previous commands |
| 46 | + self.distribution.finalize_options() |
| 47 | + self.build.ensure_finalized() |
| 48 | + |
| 49 | + # Shortcuts |
| 50 | + self.extensions = self.distribution.rust_extensions |
| 51 | + self.workspace = os.path.dirname(self.distribution.script_name) |
| 52 | + |
| 53 | + # Build list of authors |
| 54 | + if self.authors is not None: |
| 55 | + self.authors = "[{}]".format( |
| 56 | + ", ".join(author.strip() for author in self.authors.split('\n'))) |
| 57 | + else: |
| 58 | + self.authors = '["{} <{}>"]'.format( |
| 59 | + self.distribution.get_author(), |
| 60 | + self.distribution.get_author_email()) |
| 61 | + |
| 62 | + def run(self): |
| 63 | + for ext in self.extensions: |
| 64 | + log.info("creating 'Cargo.toml' for '%s'", ext.name) |
| 65 | + toml = self.build_cargo_toml(ext) |
| 66 | + with open(ext.path, 'w') as manifest: |
| 67 | + toml.write(manifest) |
| 68 | + |
| 69 | + if self.create_workspace and self.extensions: |
| 70 | + log.info("creating 'Cargo.toml' for workspace") |
| 71 | + toml = self.build_workspace_toml() |
| 72 | + with open(os.path.join(self.workspace, "Cargo.toml"), 'w') as manifest: |
| 73 | + toml.write(manifest) |
| 74 | + |
| 75 | + def build_cargo_toml(self, ext): |
| 76 | + |
| 77 | + quote = '"{}"'.format |
| 78 | + dist = self.distribution |
| 79 | + toml = configparser.ConfigParser() |
| 80 | + |
| 81 | + toml.add_section("package") |
| 82 | + toml.set("package", "name", quote(ext.name)) |
| 83 | + toml.set("package", "version", quote(dist.get_version())) |
| 84 | + toml.set("package", "authors", self.authors) |
| 85 | + toml.set("package", "publish", "false") |
| 86 | + |
| 87 | + if self.create_workspace: |
| 88 | + toml.set("package", "workspace", quote(os.path.abspath(self.workspace))) |
| 89 | + |
| 90 | + toml.add_section("lib") |
| 91 | + toml.set("lib", "crate-type", '["cdylib"]') |
| 92 | + toml.set("lib", "name", quote(ext.basename)) |
| 93 | + toml.set("lib", "path", quote(ext.libfile)) |
| 94 | + |
| 95 | + toml.add_section("dependencies") |
| 96 | + for dep, options in self.iter_dependencies(ext): |
| 97 | + toml.set("dependencies", dep, options) |
| 98 | + |
| 99 | + return toml |
| 100 | + |
| 101 | + def build_workspace_toml(self): |
| 102 | + |
| 103 | + members = [os.path.dirname(os.path.relpath(ext.path)) for ext in self.extensions] |
| 104 | + members = ['"{}"'.format(m) for m in members] |
| 105 | + |
| 106 | + toml = configparser.ConfigParser() |
| 107 | + toml.add_section('workspace') |
| 108 | + toml.set('workspace', 'members', '[{}]'.format(', '.join(members))) |
| 109 | + |
| 110 | + return toml |
| 111 | + |
| 112 | + def iter_dependencies(self, ext=None): |
| 113 | + |
| 114 | + command = self.get_command_name() |
| 115 | + sections = ['{}.dependencies'.format(command)] |
| 116 | + |
| 117 | + if ext is not None: |
| 118 | + sections.append('{}.dependencies.{}'.format(command, ext.name)) |
| 119 | + |
| 120 | + for section in sections: |
| 121 | + if self.cfg.has_section(section): |
| 122 | + for dep, options in self.cfg.items(section): |
| 123 | + yield dep, options |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +def _slugify(name): |
| 128 | + allowed = set(string.ascii_letters + string.digits + '_') |
| 129 | + slug = [char if char in allowed else '_' for char in name] |
| 130 | + return ''.join(slug) |
| 131 | + |
| 132 | +def find_rust_extensions(*directories, libfile="lib.rs", **kwargs): |
| 133 | + |
| 134 | + directories = directories or [os.getcwd()] |
| 135 | + extensions = [] |
| 136 | + |
| 137 | + for directory in directories: |
| 138 | + for base, dirs, files in os.walk(directory): |
| 139 | + if libfile in files: |
| 140 | + dotpath = os.path.relpath(base).replace(os.path.sep, '.') |
| 141 | + tomlpath = os.path.join(base, "Cargo.toml") |
| 142 | + ext = RustExtension(dotpath, tomlpath, **kwargs) |
| 143 | + ext.libfile = os.path.join(base, libfile) |
| 144 | + ext.basename = os.path.basename(base) |
| 145 | + extensions.append(ext) |
| 146 | + |
| 147 | + return extensions |
0 commit comments