-
Notifications
You must be signed in to change notification settings - Fork 18
/
generate-docs.py
executable file
·157 lines (111 loc) · 3.95 KB
/
generate-docs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [ mdutils ])"
#
# Helper script to generate the output for the documentation."""
import re
import glob
from mdutils.mdutils import MdUtils
URL = "https://search.nixos.org/packages?channel=unstable&show={tool}&type=packages&query={tool}"
def collect_files():
"""Collect all files aka categories."""
pkgs_files = []
for file in glob.glob("*.nix"):
pkgs_files.append(file)
return pkgs_files
def starts_with_four_n_spaces(eval_string):
"""Test if the string starts with 4 spaces."""
return re.search(r"^(?:\s{4})+(?!\s).*$", eval_string) is not None
def collect_tools():
"Collect all tools from the different files."
output = {}
pkgs_files = collect_files()
for pkgs_file in pkgs_files:
category = pkgs_file.split(".")[0]
with open(pkgs_file) as file_content:
tools = []
for line in file_content.readlines():
if starts_with_four_n_spaces(line):
tools.append(line.strip())
output[category] = tools
return output
def list_tools(data):
"""Add all tools to a single list."""
tools = data
tools_list = []
for _, tools in tools.items():
tools_list = tools_list + tools
return [tool for tool in tools_list if not ("#" in tool)]
def count_tools(data):
"""Count the available tools."""
print("Available tools:", len(list_tools(data)))
def full_list(data):
"""Create a page with all available packages."""
tools = list_tools(data)
tools.sort()
mdFile = MdUtils(file_name="docs/list.md", title="Full list")
mdFile.new_line("```text")
for tool in tools:
mdFile.new_line(tool)
mdFile.new_line("```")
mdFile.create_md_file()
def imports():
"""Create files list for imports."""
pkgs_files = collect_files()
pkgs_files.sort()
mdFile = MdUtils(file_name="docs/imports.md", title="Imports")
mdFile.new_line("```text")
for file in pkgs_files:
mdFile.new_line(f" ./{file}")
mdFile.new_line("```")
mdFile.create_md_file()
def full_nix(data):
"""Create a nix file which contains all tools."""
tools = list_tools(data)
tools.sort()
with open("docs/nix-security-tool-box.nix", "w") as nix_file:
nix_file.write("# Full package set from Nix Security Tool Box\n\n")
nix_file.write("{ pkgs, ... }:\n\n")
nix_file.write("{\n")
nix_file.write(" environment.systemPackages = with pkgs; [\n")
for tool in tools:
nix_file.write(f" {tool}\n")
nix_file.write(" ];\n")
nix_file.write("}\n")
def full_shell(data):
"""Create a nix file which contains all tools."""
tools = list_tools(data)
tools.sort()
with open("docs/nstb-shell.nix", "w") as nix_file:
nix_file.write("{ pkgs ? import <nixpkgs> {} }:\n\n")
nix_file.write("with pkgs;\n\n")
nix_file.write("mkShell {\n")
nix_file.write(" nativeBuildInputs = [\n")
for tool in tools:
nix_file.write(f" {tool}\n")
nix_file.write(" ];\n")
nix_file.write("}\n")
def create_overview(data):
"""Create tools overview."""
mdFile = MdUtils(file_name="docs/index.md", title="Tool overview")
for category, tools in data.items():
mdFile.new_header(level=1, title=category.replace("-", " ").capitalize())
for tool in tools:
if not tool.startswith("# "):
mdFile.new_line(
"- {}".format(
mdFile.new_inline_link(link=URL.format(tool=tool), text=tool)
)
)
mdFile.new_line()
mdFile.create_md_file()
def main():
"""Main part of the script."""
data = collect_tools()
create_overview(data)
count_tools(data)
full_list(data)
full_nix(data)
full_shell(data)
imports()
if __name__ == "__main__":
main()