Repository navigation
Expand file tree
/
Copy pathrender_citation.py
More file actions
332 lines (306 loc) · 10.6 KB
/
Copy pathrender_citation.py
File metadata and controls
332 lines (306 loc) · 10.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""Render CITATION.cff from template and git history.
Reads contributor data from ``git log``, orders
authors by total lines added (descending), and
generates CITATION.cff from a Jinja2 template with
rich author metadata (ORCID, affiliations).
"""
import re
import subprocess
from collections import defaultdict
from datetime import date
import toml
from jinja2 import Template
# --------------------------------------------------
# CFF author metadata keyed by canonical name.
# Only authors with known ORCID or affiliations need
# an entry here. Unknown git contributors will still
# appear in the output with just their name.
# --------------------------------------------------
AUTHOR_METADATA: dict[str, dict] = {
"Andre Marquand": {
"family-names": "Marquand",
"given-names": "Andre",
"orcid": ("https://orcid.org/0000-0001-5903-203X"),
"affiliations": [
"Donders Institute for Brain, Cognition, and Behavior",
"Radboud University Medical Center",
"King's College London",
],
},
"Stijn de Boer": {
"family-names": "de Boer",
"given-names": "Stijn",
"orcid": ("https://orcid.org/0000-0002-8657-8959"),
"affiliations": [
"Donders Institute for Brain, Cognition and Behaviour",
"Radboud University Medical Center",
],
},
"Konstantinos Tsilimparis": {
"family-names": "Tsilimparis",
"given-names": "Konstantinos",
"orcid": ("https://orcid.org/0009-0008-5734-7538"),
"affiliations": [
"Donders Institute for Brain, Cognition and Behaviour",
"Radboud University Medical Center",
],
},
"Seyed Mostafa Kia": {
"family-names": "Kia",
"given-names": "Seyed Mostafa",
"orcid": ("https://orcid.org/0000-0002-7128-814X"),
"affiliations": [
"Tilburg University",
"Donders Institute for Brain, Cognition, and Behavior",
"University Medical Center Utrecht",
],
},
"Saige Rutherford": {
"family-names": "Rutherford",
"given-names": "Saige",
"orcid": ("https://orcid.org/0000-0003-3006-9044"),
"affiliations": [
"Donders Institute for Brain, Cognition, and Behavior",
"Radboud University Medical Center",
"University of Michigan",
],
},
"Charlotte Fraza": {
"family-names": "Fraza",
"given-names": "Charlotte",
"orcid": ("https://orcid.org/0000-0002-7088-9250"),
"affiliations": [
"Donders Institute for Brain, Cognition, and Behavior",
"Radboud University Medical Center",
],
},
"Barbora Rehak Buckova": {
"family-names": "Rehak Buckova",
"given-names": "Barbora",
"orcid": ("https://orcid.org/0000-0001-5619-3946"),
"affiliations": [
"Donders Institute for Brain, Cognition, and Behavior",
"Radboud University Medical Center",
"National Institute of Mental Health: Klecany, CZ",
"Czech Technical University in Prague: Prague, CZ",
"Institute of Computer Science: Prague, CZ",
],
},
"Pieter Barkema": {
"family-names": "Barkema",
"given-names": "Pieter",
"affiliations": [
"University College London",
"Donders Center for Brain, Cognition, and Behavior",
],
},
"Thomas Wolfers": {
"family-names": "Wolfers",
"given-names": "Thomas",
},
"Johanna Bayer": {
"family-names": "Bayer",
"given-names": "Johanna",
"orcid": ("https://orcid.org/0000-0003-4891-6256"),
"affiliations": [
"Donders Institute for Brain, Cognition and Behaviour",
],
},
"Maarten Mennes": {
"family-names": "Mennes",
"given-names": "Maarten",
"orcid": ("https://orcid.org/0000-0002-7279-3439"),
"affiliations": [
"Donders Institute for Brain, Cognition and Behaviour",
"Radboud University",
"SBGneuro Ltd.",
],
},
"Hester Huijsdens": {
"family-names": "Huijsdens",
"given-names": "Hester",
"orcid": ("https://orcid.org/0000-0001-7039-8390"),
"affiliations": [
"Donders Institute for Brain, Cognition and Behaviour",
],
},
"Pierre Berthet": {
"family-names": "Berthet",
"given-names": "Pierre",
"orcid": ("https://orcid.org/0000-0002-6878-6842"),
"affiliations": [
"Donders Institute for Brain, Cognition and Behaviour",
"University of Oslo",
"Stockholm University",
"Universite de Bordeaux",
"Universite Pierre Mendes-France",
"Universite Savoie Mont-Blanc",
],
},
}
# --------------------------------------------------
# Map git author names → canonical names.
# Handles contributors who commit under different
# usernames or email-linked display names.
# --------------------------------------------------
NAME_MAP: dict[str, str] = {
"amarquand": "Andre Marquand",
"andre": "Andre Marquand",
"Stijn": "Stijn de Boer",
"Augub": "Stijn de Boer",
"AuguB": "Stijn de Boer",
"S.M.Kia": "Seyed Mostafa Kia",
"contsili": "Konstantinos Tsilimparis",
"saigerutherford": "Saige Rutherford",
"RindKind": "Thomas Wolfers",
"Hesterhuijsdens": "Hester Huijsdens",
"pierre berthet": "Pierre Berthet",
"Barbora": "Barbora Rehak Buckova",
"Barbora Buckova": "Barbora Rehak Buckova",
"likeajumprope": "Johanna Bayer",
"PBarkema": "Pieter Barkema",
"lindenmp": "Linden Parkes",
}
# Git names to exclude (bots, placeholder entries).
EXCLUDE: set[str] = {"dependabot[bot]", "="}
def get_lines_per_author() -> dict[str, int]:
"""Get total lines added per canonical author.
Runs ``git log --numstat`` across all refs and
sums the additions for each author, deduplicating
names via NAME_MAP.
Returns
-------
dict[str, int]
Mapping of canonical author name to total
lines added.
"""
# Run git log with numstat to get per-file
# insertions and deletions for every commit
# that was merged into the current branch.
result = subprocess.run(
[
"git",
"log",
"--format=%aN",
"--numstat",
],
capture_output=True,
text=True,
check=True,
)
# Accumulate lines added per author name.
lines_by_author: dict[str, int] = defaultdict(int)
current_author: str | None = None
for line in result.stdout.splitlines():
stripped = line.strip()
# Skip empty lines between commits.
if not stripped:
continue
# Numstat lines: adds<TAB>dels<TAB>filename.
# Binary files show "-" instead of numbers.
match = re.match(r"^(\d+|-)\t(\d+|-)\t", stripped)
if match:
# Only count text-file additions.
if current_author and match.group(1) != "-":
additions = int(match.group(1))
# Resolve to canonical name.
canonical = NAME_MAP.get(current_author, current_author)
lines_by_author[canonical] += additions
else:
# This line is an author name.
current_author = stripped
return dict(lines_by_author)
def format_author_yaml(
name: str,
meta: dict | None,
) -> str:
"""Format a single author as CFF YAML lines.
Parameters
----------
name : str
Canonical author name.
meta : dict | None
Author metadata dict, or None for unknown
authors whose names are guessed from git.
Returns
-------
str
Indented YAML lines for one author entry.
"""
yaml_lines: list[str] = []
if meta:
# Use the curated metadata.
yaml_lines.append(f' - family-names: "{meta["family-names"]}"')
yaml_lines.append(f' given-names: "{meta["given-names"]}"')
if "orcid" in meta:
yaml_lines.append(f' orcid: "{meta["orcid"]}"')
for aff in meta.get("affiliations", []):
yaml_lines.append(f' affiliation: "{aff}"')
else:
# Unknown author: split on last space.
parts = name.rsplit(" ", 1)
if len(parts) == 2:
yaml_lines.append(f' - family-names: "{parts[1]}"')
yaml_lines.append(f' given-names: "{parts[0]}"')
else:
# Single-word name / username.
yaml_lines.append(f' - family-names: "{name}"')
return "\n".join(yaml_lines)
def build_authors_yaml(
lines_by_author: dict[str, int],
) -> str:
"""Build CFF authors YAML ordered by lines added.
Parameters
----------
lines_by_author : dict[str, int]
Canonical name → total lines added.
Returns
-------
str
YAML string for the ``authors:`` section body.
"""
# Sort by lines contributed (descending).
sorted_authors = sorted(
lines_by_author.items(),
key=lambda x: x[1],
reverse=True,
)
# Build a YAML entry for each contributor.
entries: list[str] = []
for name, lines in sorted_authors:
# Skip excluded names.
if name in EXCLUDE:
continue
meta = AUTHOR_METADATA.get(name)
entries.append(format_author_yaml(name, meta))
# Print contributor ranking to stdout.
print(f" {name}: {lines} lines added")
return "\n".join(entries)
def main() -> None:
"""Render CITATION.cff from template and git."""
# Load version from pyproject.toml.
pyproject = toml.load("pyproject.toml")
meta = pyproject["project"]
version = meta["version"]
# Collect contributor stats from git history.
print("Collecting git contributor stats...")
lines_by_author = get_lines_per_author()
authors_yaml = build_authors_yaml(lines_by_author)
# Template context with version, date, DOI, and
# the generated authors YAML block. Use the Zenodo *concept* DOI
# that cites all versions of the software.
context = {
"version": version,
"date": date.today().isoformat(),
"doi": "10.5281/zenodo.7498917",
"authors_yaml": authors_yaml,
}
# Load and render the Jinja2 template.
with open("citation.cff.in", encoding="utf-8") as f:
template = Template(f.read())
# Write rendered output to CITATION.cff.
with open("CITATION.cff", "w", encoding="utf-8") as f:
f.write(template.render(**context))
print("CITATION.cff generated.")
if __name__ == "__main__":
main()