/
project_inventory.py
267 lines (222 loc) · 7.86 KB
/
project_inventory.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Helper tools for writing tables."""
import argparse
import json
import re
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Callable, Dict, Iterable, List, Optional, Sequence, Union
import yaml
from pydantic import BaseModel, root_validator
from pytablewriter import HtmlTableWriter
from .repo import Repo, get_repo
def load_yaml(path: Union[Path, str]) -> dict:
with open(path) as stream:
return yaml.load(stream, Loader=yaml.SafeLoader)
def to_html_table(
inventory: "ProjectInventory",
selected_languages: List[str],
*,
fetch: bool = False,
min_percentage: float = 2.5,
hide_columns: Optional[Iterable[str]] = None,
) -> str:
header_to_formatters: Dict[str, Callable[[Project], str]] = {
"Project": _create_project_entry,
"Collaboration": partial(_format_collaboration, inventory=inventory),
"Since": _fetch_first_commit_year if fetch else lambda _: "",
"Latest commit": _fetch_latest_commit_date if fetch else lambda _: "",
}
for language in selected_languages:
header_to_formatters[language] = partial(
_checkmark_language,
language=language,
fetch=fetch,
min_percentage=min_percentage,
)
if hide_columns is not None:
for hide in hide_columns:
del header_to_formatters[hide]
writer = HtmlTableWriter(
headers=list(header_to_formatters),
value_matrix=[
tuple(formatter(project) for formatter in header_to_formatters.values())
for project in inventory.projects
],
)
return writer.dumps()
def fix_html_alignment(src: str) -> str:
left_align_style = 'style="text-align:left; vertical-align:top"'
center_align_style = 'style="text-align:center; vertical-align:top"'
src = src.replace(
'<td align="left">✓</td>',
f"<td {center_align_style}>✓</td>",
)
src = re.sub(
r'<td align="right">([0-9]{4})</td>',
rf"<td {center_align_style}>\1</td>",
src,
)
src = src.replace('align="left"', left_align_style)
return src.replace("<th>", f"<th {left_align_style}>")
class SubProject(BaseModel):
name: str
url: str
class Project(BaseModel):
name: str
url: str
collaboration: Optional[Union[List[str], str]] = None
languages: List[str] = []
sub_projects: Optional[List[SubProject]] = None
since: int = 0
class ProjectInventory(BaseModel):
projects: List[Project]
collaborations: Dict[str, str] = {}
@root_validator(skip_on_failure=True)
def _check_collaboration_exists(cls, values: dict) -> dict: # noqa: N805
defined_collaborations = set(values["collaborations"])
project: Project
for project in values["projects"]:
if project.collaboration is None:
continue
if isinstance(project.collaboration, str):
collaborations = [project.collaboration]
else:
collaborations = project.collaboration
for col in collaborations:
if col not in defined_collaborations:
msg = f"No collaboration defined for {col}"
raise ValueError(msg)
return values
def _create_project_entry(project: Project) -> str:
html = _form_html_link(name=project.name, url=project.url)
if project.sub_projects is not None:
sub_project_links = [
_form_html_link(name=s.name, url=s.url) for s in project.sub_projects
]
enumerated_projects = _enumerate_html_links(sub_project_links)
html += enumerated_projects
return html
def _checkmark_language(
project: Project,
language: str,
*,
fetch: bool = False,
min_percentage: float,
) -> str:
languages = project.languages
if not languages and fetch:
languages = _fetch_languages(project, min_percentage)
if project.sub_projects is not None:
for sub_project in project.sub_projects:
sub_languages = _fetch_languages(sub_project, min_percentage)
languages.extend(sub_languages)
normalized_languages = (__replace_language(s).lower() for s in languages)
if language.lower() in normalized_languages:
return "✓"
return ""
def __replace_language(language: str) -> str:
replacements = {
"C": "C++",
}
for old, new in replacements.items():
if old.lower() == language.lower():
return new
return language
def _fetch_languages(
project: Union[Project, SubProject], min_percentage: float
) -> List[str]:
repo = get_repo(project.url)
if repo is None:
return []
return repo.filter_languages(min_percentage)
def _fetch_latest_commit_date(project: Project) -> str:
return _get_date(
project,
date_getter=lambda p: p.latest_commit,
min_or_max=max,
date_format="%m/%Y",
)
def _fetch_first_commit_year(project: Project) -> str:
if project.since != 0:
return str(project.since)
return _get_date(
project,
date_getter=lambda p: p.first_commit,
min_or_max=min,
date_format="%Y",
)
def _get_date(
project: Project,
date_getter: Callable[[Repo], datetime],
date_format: str,
min_or_max: Callable[[Iterable[datetime]], datetime],
) -> str:
repo = get_repo(project.url)
time_stamps = []
if repo is not None:
main_timestamp = date_getter(repo)
time_stamps.append(main_timestamp)
sub_time_stamps = _get_subproject_timestamps(project, date_getter)
time_stamps.extend(sub_time_stamps)
if time_stamps:
return min_or_max(time_stamps).strftime(date_format)
return ""
def _get_subproject_timestamps(
project: Project, date_getter: Callable[[Repo], datetime]
) -> List[datetime]:
if project.sub_projects is None:
return []
timestamps = []
for sub_project in project.sub_projects:
repo = get_repo(sub_project.url)
if repo is None:
continue
date = date_getter(repo)
timestamps.append(date)
return timestamps
def _format_collaboration(project: Project, inventory: "ProjectInventory") -> str:
collaborations = project.collaboration
if collaborations is None:
return ""
if isinstance(collaborations, str):
collaborations = [collaborations]
collaborations_links = (
_form_collaboration_link(inventory, c) for c in collaborations
)
return " / ".join(collaborations_links)
def _form_collaboration_link(inventory: ProjectInventory, name: str) -> str:
collaboration_url = inventory.collaborations.get(name)
if collaboration_url is None:
msg = f'Collaboration entry "{name}" not found'
raise KeyError(msg)
return _form_html_link(name=name, url=collaboration_url)
def _form_html_link(url: str, name: str) -> str:
return f'<a href="{url}">{name}</a>'
def _enumerate_html_links(list_of_entries: Sequence[str]) -> str:
if not list_of_entries:
return ""
if len(list_of_entries) == 1:
return list_of_entries[0]
html = "<li>".join(list_of_entries)
return "<li>" + html
def export_json_schema(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser(
"Create a JSON validation schema for a software project inventory file\n"
)
parser.add_argument(
"path",
type=str,
nargs="?",
default="docs/software/project-inventory-schema.json",
help="Output path to write the JSON schema to",
)
args = parser.parse_args(argv)
schema = ProjectInventory.model_json_schema()
json_schema = json.dumps(schema, indent=2, sort_keys=False)
json_schema += "\n"
with open(args.path, "w") as stream:
stream.write(json_schema)
return 0
if __name__ == "__main__":
raise SystemExit(export_json_schema())