Skip to content

Commit ac5c929

Browse files
author
Martin Larralde
committed
Add README.rst to example_tomlgen and improve tomlgen_rust documentation
Signed-off-by: Martin Larralde <[email protected]>
1 parent 1fc309a commit ac5c929

File tree

4 files changed

+136
-5
lines changed

4 files changed

+136
-5
lines changed

example_tomlgen/README.rst

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,116 @@
22
=================
33

44
An example extensions with automatically generated ``Cargo.toml`` manifest
5-
files.
5+
files. Simply run ``python setup.py tomlgen_rust`` to generate the following
6+
files:
7+
8+
* ``Cargo.toml`` for ``hello.english``
9+
10+
.. code-block:: toml
11+
12+
[package]
13+
name = "hello.english"
14+
version = "0.1.0"
15+
authors = ["Martin Larralde <[email protected]>"]
16+
publish = false
17+
18+
[lib]
19+
crate-type = ["cdylib"]
20+
name = "hello_english"
21+
path = "lib.rs"
22+
23+
[dependencies]
24+
pyo3 = { version = "*", features = ["extension-module"] }
25+
english-lint = "*"
26+
27+
28+
* ``Cargo.toml`` for ``hello.french``
29+
30+
.. code-block:: toml
31+
32+
[package]
33+
name = "hello.french"
34+
version = "0.1.0"
35+
authors = ["Martin Larralde <[email protected]>"]
36+
publish = false
37+
38+
[lib]
39+
crate-type = ["cdylib"]
40+
name = "hello_french"
41+
path = "lib.rs"
42+
43+
[dependencies]
44+
pyo3 = { version = "*", features = ["extension-module"] }
45+
46+
47+
Metadata
48+
--------
49+
50+
The package name will be generated from the position of the extension within
51+
the Python package. The same version is used as the one declared in ``setup.py``
52+
or ``setup.cfg``.
53+
54+
The authors list is generated after the ``author`` and ``author_email`` options
55+
from ``setup.py`` / ``setup.cfg``, but can also be overriden using the
56+
``authors`` key in the ``[tomlgen_rust]`` section of ``setup.cfg``:
57+
58+
.. code-block:: toml
59+
60+
[tomlgen_rust]
61+
authors =
62+
Jane Doe <[email protected]>
63+
John Doe <[email protected]>
64+
65+
The library name is a slugified variant of the extension package name, to
66+
avoid name collisions within the build directory.
67+
68+
As a safety, ``publish = false`` is added to the ``[package]`` section
69+
(you wouldn't publish an automatically generated package, *would you ?!*).
70+
71+
72+
Options
73+
-------
74+
75+
Use ``--force`` (or add ``force = true`` to the ``[tomlgen_rust]`` section of
76+
``setup.cfg``) to force generating a manifest even when one already exists.
77+
78+
Use ``--create-workspace`` to create a virtual manifest at the root of your
79+
project (next to the ``setup.py`` file) which registers all of the extensions.
80+
This way, generic ``cargo`` commands can be run without leaving the root of
81+
the project.
82+
83+
84+
Dependencies
85+
------------
86+
87+
To specify dependencies for all extensions, add them to the
88+
``[tomlgen_rust.dependencies]`` section of your setuptools configuration file
89+
(``setup.cfg``), as you would normally in your ``Cargo.toml`` file. Here is
90+
probably a good place to add ``pyo3`` as a dependency.
91+
92+
To specify per-extension dependency, create a section for each extension
93+
(``[tomlgen_rust.dependencies.<DOTTEDPATH>]``, where ``<DOTTEDPATH>`` is the
94+
complete Python path to the extension (e.g. ``hello.english``). Extension
95+
specific dependencies are added *after* global dependencies.
96+
97+
*Note that, since all projects are built in the same directory, you can also
98+
declare all dependencies in the ``[tomlgen_rust.dependencies]``, as they will
99+
be built only once anyway*.
100+
101+
102+
Automatic generation at each build
103+
----------------------------------
104+
105+
If you intend to regenerate manifests everytime the library is built, you can
106+
add ``Cargo.toml`` and ``Cargo.lock`` to your ``.gitignore`` file.
107+
108+
Then, make sure ``tomlgen_rust`` is run before ``build_rust`` everytime by
109+
adding aliases to your ``setup.cfg`` file:
110+
111+
.. code-block:: toml
112+
113+
[aliases]
114+
build_rust = tomlgen_rust -f build_rust
115+
clean_rust = tomlgen_rust -f clean_rust
116+
build = tomlgen_rust -f build
117+
clean = clean_rust -f clean

example_tomlgen/setup.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
[metadata]
22
author_email = "[email protected]"
3+
description = Example Python/Rust project with automatic Cargo.toml generation
4+
long-description = file: README.rst
5+
6+
[options]
7+
zip_safe = false
38

49
[tomlgen_rust]
510
create_workspace = true
@@ -12,3 +17,6 @@ english-lint = "*"
1217

1318
[aliases]
1419
build_rust = tomlgen_rust build_rust
20+
clean_rust = tomlgen_rust clean_rust
21+
build = tomlgen_rust build
22+
clean = clean_rust clean

example_tomlgen/setup.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
14
import setuptools
25
import setuptools_rust as rust
36

@@ -13,6 +16,12 @@
1316
strip=rust.Strip.Debug,
1417
),
1518

19+
# specify setup dependencies
20+
setup_requires=[
21+
'setuptools',
22+
'setuptools_rust',
23+
],
24+
1625
# rust extensions are not zip safe, just like C-extensions.
1726
zip_safe=False
1827
)

setuptools_rust/tomlgen.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ def finalize_options(self):
5656

5757
# Build list of authors
5858
if self.authors is not None:
59+
sep = '\n' if '\n' in self.authors.strip() else ','
5960
self.authors = "[{}]".format(
60-
", ".join(author.strip() for author in self.authors.split('\n')))
61+
", ".join(author.strip() for author in self.authors.split(sep)))
6162
else:
6263
self.authors = '["{} <{}>"]'.format(
6364
self.distribution.get_author(),
@@ -139,8 +140,11 @@ def build_workspace_toml(self):
139140
def iter_dependencies(self, ext=None):
140141

141142
command = self.get_command_name()
143+
144+
# global dependencies
142145
sections = ['{}.dependencies'.format(command)]
143146

147+
# extension-specific dependencies
144148
if ext is not None:
145149
sections.append('{}.dependencies.{}'.format(command, ext.name))
146150

@@ -194,9 +198,7 @@ def find_rust_extensions(*directories, libfile="lib.rs", **kwargs):
194198
└ Cargo.toml
195199
setup.py
196200
197-
The only extension can be found in the ``lib`` module:
198-
199-
.. code-block:: python
201+
The only extension can be found in the ``lib`` module::
200202
201203
>>> import setuptools_rust as rust
202204
>>> for ext in rust.find_rust_extensions("lib"):

0 commit comments

Comments
 (0)