|
| 1 | +import re |
1 | 2 | import mkdocs_gen_files |
2 | 3 | from subprocess import run |
3 | 4 |
|
|
10 | 11 | [MPL-2.0]: https://choosealicense.com/licenses/mpl-2.0 |
11 | 12 | """ |
12 | 13 |
|
13 | | -OPTIONAL_DEPS = """## Optional dependencies |
| 14 | +TABLE_HEADER = "| Dependency | License |\n|:------------|:-------|\n" |
| 15 | + |
| 16 | +OPTIONAL_DEPS = f"""## Optional dependencies |
14 | 17 |
|
15 | 18 | The following are conditionally included in binaries (using the `openssl-vendored` |
16 | 19 | feature on a case-by-case basis) because it is a dependency of |
17 | 20 | [git2](https://crates.io/crates/git2): |
18 | 21 |
|
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] | |
22 | 25 | """ |
23 | 26 |
|
24 | | -BINDING_DEPS = """## Bindings' dependencies |
| 27 | +PY_BINDING_HEADER = f"""## Bindings' dependencies |
25 | 28 |
|
26 | | -The python binding uses |
| 29 | +### Python binding |
27 | 30 |
|
28 | | -- [pyo3](https://crates.io/crates/pyo3): |
29 | | - Dual-licensed under [Apache-2.0] or [MIT]. |
| 31 | +{TABLE_HEADER}""" |
30 | 32 |
|
31 | | -The node binding uses |
| 33 | +JS_BINDING_HEADER = f"""### Node.js binding |
32 | 34 |
|
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}""" |
36 | 36 |
|
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 = [ |
41 | 43 | "cargo", |
42 | 44 | "tree", |
43 | 45 | "-f", |
44 | | - r"[{p}]({r}): Licensed under {l}", |
| 46 | + r"| [{p}]({r}) | {l} |", |
45 | 47 | "-e", |
46 | 48 | "normal", |
47 | 49 | "-p", |
48 | 50 | "cpp-linter", |
49 | 51 | "--depth", |
50 | 52 | "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:] |
63 | 53 | ] |
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() |
65 | 85 | # print(doc) |
66 | 86 | print(doc, file=io_doc) |
67 | 87 | 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) |
69 | 94 |
|
70 | 95 | mkdocs_gen_files.set_edit_path(FILENAME, "license-gen.py") |
0 commit comments