-
Notifications
You must be signed in to change notification settings - Fork 112
/
footnote_build_info.py
86 lines (76 loc) · 2.25 KB
/
footnote_build_info.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
import os
import subprocess
from shutil import copyfile
def make_patch():
"""
While being run in CI by GitHub Actions, collect repo, commit, and CI run
information to patch jheppub.sty.
"""
commit_SHA = (
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"])
.strip()
.decode("utf-8")
)
# GITHUB_REPOSITORY is set by GitHub Actions
try:
github_repository = os.environ["GITHUB_REPOSITORY"]
except KeyError:
github_repository = (
subprocess.check_output(["git", "config", "--get", "remote.origin.url"])
.strip()
.decode("utf-8")
.replace("[email protected]:", "")
.replace(".git", "")
)
github_repository_url = "https://github.com/" + github_repository
# GITHUB_RUN_ID is set by GitHub Actions
try:
github_run_id = os.environ["GITHUB_RUN_ID"]
except KeyError:
github_run_id = ""
github_actions_url = github_repository_url + "/actions/runs/" + github_run_id
github_commit_url = github_repository_url + "/tree/" + commit_SHA
footnote = (
r"\footnotesize Built \href{"
+ github_actions_url
+ r"}{\today}\ from \href{"
+ github_commit_url
+ "}{"
+ commit_SHA
+ "}"
)
patch = (
r"\usepackage{fancyhdr}"
+ "\n"
+ r"\newcommand\ps@titlepage{\renewcommand\@oddfoot{}\renewcommand\@oddhead{}"
+ "\n"
+ r"\pagestyle{fancy}"
+ "\n"
+ r"\renewcommand{\headrulewidth}{0pt}"
+ "\n"
+ r"\fancyfoot{}"
+ "\n"
+ r"\rfoot{"
+ footnote
+ "}"
+ "\n}"
)
return patch
def main():
"""
Patch jheppub.sty to record build information as a footnote on the title page.
"""
patch = make_patch()
copyfile("jheppub.sty", "jheppub.sty.bak")
with open("jheppub.sty.bak") as read_file, open(
"jheppub.sty", "w+"
) as write_file:
for line in read_file:
write_file.write(
line.replace(
r"\newcommand\ps@titlepage{\renewcommand\@oddfoot{}\renewcommand\@oddhead{}}",
patch,
)
)
if __name__ == "__main__":
main()