|
1 | 1 | import importlib.metadata
|
| 2 | +import runpy |
2 | 3 | import tomllib
|
3 | 4 | from typing import TypedDict
|
4 | 5 |
|
5 |
| -PATH_OS_NOTICE_DOC = "docs/legal/os_notices.md" |
6 | 6 | PATH_OS_NOTICE_DATA = "os_notices.toml"
|
7 | 7 |
|
8 |
| -TEMPLATE_OS_NOTICE_DOC = """<!-- Generated by scripts/generate_os_notice.py, DO NOT DIRECTLY EDIT THIS FILE. --> |
9 |
| -<!-- markdownlint-disable --> |
10 |
| -<!-- @formatter:off --> |
| 8 | +Notice = TypedDict( |
| 9 | + "Notice", |
| 10 | + { |
| 11 | + "name": str, |
| 12 | + "dependency": str, |
| 13 | + "repository": str, |
| 14 | + "license": str, |
| 15 | + "license-url": str, |
| 16 | + "copyrights": list[str], |
| 17 | + }, |
| 18 | +) |
11 | 19 |
|
12 |
| -# 开源声明 |
13 |
| -
|
14 |
| -| 开源项目 | 许可证 | 版权声明 | |
15 |
| -| -------- | ------ | -------- | |
16 |
| -{notices} |
17 |
| -""" |
18 |
| -TEMPLATE_OS_NOTICE_DOC_NOTICE = """| {name} | {license} | {copyrights} |""" |
19 |
| -TEMPLATE_OS_NOTICE_DOC_NOTICE_LICENSE = """[`{license}`]({license_url})""" |
20 |
| - |
21 |
| -Notice = TypedDict("Notice", { |
22 |
| - "name": str, |
23 |
| - "dependency": str, |
24 |
| - "repository": str, |
25 |
| - "license": str, |
26 |
| - "license-url": str, |
27 |
| - "copyrights": list[str] |
28 |
| -}) |
| 20 | +NoticesConfig = TypedDict( |
| 21 | + "NoticesConfig", {"template": str, "output": str, "notices": list[Notice]} |
| 22 | +) |
29 | 23 |
|
30 | 24 |
|
31 | 25 | def format_url(url: str, notice: Notice) -> str:
|
32 | 26 | if "dependency" not in notice:
|
33 | 27 | return url
|
34 |
| - return url.format( |
35 |
| - version=importlib.metadata.version(notice["dependency"]) |
36 |
| - ) |
37 |
| - |
38 |
| - |
39 |
| -def generate_os_notice(notices: list[Notice]) -> str: |
40 |
| - return TEMPLATE_OS_NOTICE_DOC.format( |
41 |
| - notices="\n".join([ |
42 |
| - TEMPLATE_OS_NOTICE_DOC_NOTICE.format( |
43 |
| - name=f"[{notice["name"]}]({notice["repository"]})" if "repository" in notice else notice["name"], |
44 |
| - license=TEMPLATE_OS_NOTICE_DOC_NOTICE_LICENSE.format( |
45 |
| - license=notice["license"], |
46 |
| - license_url=format_url(notice["license-url"], notice) |
47 |
| - ), |
48 |
| - copyrights="<br />".join(notice["copyrights"]) |
49 |
| - ) |
50 |
| - for notice in notices |
51 |
| - ]) |
52 |
| - ) |
| 28 | + return url.format(version=importlib.metadata.version(notice["dependency"])) |
| 29 | + |
| 30 | + |
| 31 | +def process_notices(notices: list[Notice]): |
| 32 | + return [ |
| 33 | + {**notice, "license-url": format_url(notice["license-url"], notice)} |
| 34 | + for notice in notices |
| 35 | + ] |
53 | 36 |
|
54 | 37 |
|
55 | 38 | def main() -> int:
|
56 | 39 | with open(PATH_OS_NOTICE_DATA, "rb") as f:
|
57 |
| - data = tomllib.load(f) |
58 |
| - with open(PATH_OS_NOTICE_DOC, "w", encoding="utf-8") as f: |
59 |
| - f.write(generate_os_notice(notices=data["notices"])) |
| 40 | + config = NoticesConfig(**tomllib.load(f)) |
| 41 | + |
| 42 | + with open(config["output"], "w", encoding="utf-8") as f: |
| 43 | + output = runpy.run_path( |
| 44 | + config["template"], |
| 45 | + init_globals={"notices": process_notices(notices=config["notices"])}, |
| 46 | + )["output"] |
| 47 | + f.write(output) |
60 | 48 | return 0
|
0 commit comments