Skip to content

Commit 83c2ca8

Browse files
committed
docs: renders licenses of deps in tables
Also generates tables for the dependencies of the bindings using the `cargo tree` command.
1 parent 23c30d9 commit 83c2ca8

File tree

1 file changed

+56
-31
lines changed

1 file changed

+56
-31
lines changed

docs/license_gen.py

+56-31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import mkdocs_gen_files
23
from subprocess import run
34

@@ -10,61 +11,85 @@
1011
[MPL-2.0]: https://choosealicense.com/licenses/mpl-2.0
1112
"""
1213

13-
OPTIONAL_DEPS = """## Optional dependencies
14+
TABLE_HEADER = "| Dependency | License |\n|:------------|:-------|\n"
15+
16+
OPTIONAL_DEPS = f"""## Optional dependencies
1417
1518
The following are conditionally included in binaries (using the `openssl-vendored`
1619
feature on a case-by-case basis) because it is a dependency of
1720
[git2](https://crates.io/crates/git2):
1821
19-
- [openssl](https://crates.io/crates/openssl): Licensed under [Apache-2.0].
20-
- [openssl-probe](https://crates.io/crates/openssl-probe):
21-
Dual-licensed under [Apache-2.0] or [MIT].
22+
{TABLE_HEADER}\
23+
| [openssl](https://crates.io/crates/openssl) | [Apache-2.0] |
24+
| [openssl-probe](https://crates.io/crates/openssl-probe) | [MIT] OR [Apache-2.0] |
2225
"""
2326

24-
BINDING_DEPS = """## Bindings' dependencies
27+
PY_BINDING_HEADER = f"""## Bindings' dependencies
2528
26-
The python binding uses
29+
### Python binding
2730
28-
- [pyo3](https://crates.io/crates/pyo3):
29-
Dual-licensed under [Apache-2.0] or [MIT].
31+
{TABLE_HEADER}"""
3032

31-
The node binding uses
33+
JS_BINDING_HEADER = f"""### Node.js binding
3234
33-
- [napi](https://crates.io/crates/napi): Licensed under [MIT]
34-
- [napi-derive](https://crates.io/crates/napi-derive): Licensed under [MIT]
35-
"""
35+
{TABLE_HEADER}"""
3636

37-
with mkdocs_gen_files.open(FILENAME, "w") as io_doc:
38-
print(INTRO, file=io_doc)
39-
output = run(
40-
[
37+
SELF_DEP = re.compile(r"(\| \[cpp-linter v[0-9.]+[^\s]*)[^\]]+(\]\(.*)$")
38+
39+
40+
class TreeGetter:
41+
def __init__(self):
42+
self.args = [
4143
"cargo",
4244
"tree",
4345
"-f",
44-
r"[{p}]({r}): Licensed under {l}",
46+
r"| [{p}]({r}) | {l} |",
4547
"-e",
4648
"normal",
4749
"-p",
4850
"cpp-linter",
4951
"--depth",
5052
"1",
51-
],
52-
capture_output=True,
53-
check=True,
54-
)
55-
doc = "\n".join(
56-
[
57-
"- "
58-
+ line[3:]
59-
.replace(" MIT", " [MIT]")
60-
.replace(" Apache-2.0", " [Apache-2.0]")
61-
.replace(" MPL-2.0", " [MPL-2.0]")
62-
for line in output.stdout.decode(encoding="utf-8").splitlines()[1:]
6353
]
64-
)
54+
55+
def package(self, value: str) -> None:
56+
self.args[7] = value
57+
58+
def get_output(self) -> str:
59+
output = run(
60+
self.args,
61+
capture_output=True,
62+
check=True,
63+
)
64+
result = []
65+
for line in output.stdout.decode(encoding="utf-8").splitlines()[1:]:
66+
dep = (
67+
line[3:]
68+
.replace(" MIT", " [MIT]")
69+
.replace(" Apache-2.0", " [Apache-2.0]")
70+
.replace(" MPL-2.0", " [MPL-2.0]")
71+
.strip()
72+
)
73+
self_match = SELF_DEP.match(dep)
74+
if self_match is not None:
75+
dep = SELF_DEP.sub(r"\1\2", dep)
76+
result.append(dep)
77+
return "\n".join(result)
78+
79+
80+
with mkdocs_gen_files.open(FILENAME, "w") as io_doc:
81+
tg = TreeGetter()
82+
print(INTRO, file=io_doc)
83+
doc = TABLE_HEADER
84+
doc += tg.get_output()
6585
# print(doc)
6686
print(doc, file=io_doc)
6787
print(f"\n{OPTIONAL_DEPS}\n", file=io_doc)
68-
print(f"\n{BINDING_DEPS}\n", file=io_doc)
88+
tg.package("cpp-linter-py")
89+
doc = tg.get_output()
90+
print(f"\n{PY_BINDING_HEADER}{doc}", file=io_doc)
91+
tg.package("cpp-linter-js")
92+
doc = tg.get_output()
93+
print(f"\n{JS_BINDING_HEADER}{doc}", file=io_doc)
6994

7095
mkdocs_gen_files.set_edit_path(FILENAME, "license-gen.py")

0 commit comments

Comments
 (0)