-
Notifications
You must be signed in to change notification settings - Fork 10
/
post-build.py
192 lines (154 loc) · 5.2 KB
/
post-build.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
# DearEIS is licensed under the GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.html).
# Copyright 2023 DearEIS developers
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# The licenses of DearEIS' dependencies and/or sources of portions of code are included in
# the LICENSES folder.
from dataclasses import dataclass
from datetime import date
from pathlib import Path
from re import search
from shutil import (
copy,
copytree,
rmtree,
)
from typing import (
IO,
List,
Match,
Optional,
Union,
)
PARENT_DIRECTORY: Path = Path(__file__).parent
def copy_html(src: Path, dst: Path):
if dst.is_dir():
rmtree(dst)
files: List[Path] = []
path: Path
for path in src.glob("*"):
if path.is_file():
files.append(path)
assert len(files) > 0
files = [
path
for path in files
if not str(path).startswith(".")
and path.suffix.lower()
in (
".html",
".js",
".png",
".py",
".svg",
)
]
dirs: List[Path] = list(map(Path, ("_images", "_static", "_sources")))
if not dst.is_dir():
dst.mkdir(parents=True)
for path in files:
copy(src.joinpath(path.name), dst.joinpath(path.name))
for path in dirs:
copytree(src.joinpath(path.name), dst.joinpath(path.name))
def copy_pdf(src: Path, dst: Path, name: str, version_path: Path):
version: str = ""
fp: IO
with open(version_path, "r") as fp:
version = fp.read().strip().replace(".", "-")
assert version != ""
ext: str = src.suffix
dst = dst.joinpath(f"{name}-{version}{ext}")
if dst.is_file():
dst.unlink()
copy(src, dst)
@dataclass(frozen=True)
class Version:
major: int
minor: int
patch: int
year: int
month: int
day: int
def validate_changelog(path: Path):
def parse_version(match: Match) -> Version:
return Version(
major=int(match.group("major")),
minor=int(match.group("minor")),
patch=int(match.group("patch")),
year=int(match.group("year")),
month=int(match.group("month")),
day=int(match.group("day")),
)
def validate_date(
version: Version,
comparison: Union[Version, date] = date.today(),
):
assert version.year <= comparison.year, (version, comparison)
assert 1 <= version.month <= 12, version
assert 1 <= version.day <= 31, version
if version.year == comparison.year:
assert version.month <= comparison.month, (version, comparison)
if version.month == comparison.month:
assert version.day <= comparison.day, (version, comparison)
def validate_version(earlier: Version, current: Version):
assert earlier.major <= current.major, (earlier, current)
if earlier.major < current.major:
return
assert earlier.minor <= current.minor, (earlier, current)
if earlier.minor < current.minor:
return
assert earlier.patch < current.patch, (earlier, current)
assert path.is_file(), path
fp: IO
with open(path, "r") as fp:
lines: List[str] = fp.readlines()
pattern: str = (
r"# (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"
r" \((?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})\)"
)
try:
match: Optional[Match] = search(pattern, lines.pop(0))
assert match is not None, pattern
versions: List[Version] = list(
map(
parse_version,
[match]
+ [
match
for match in map(lambda _: search(pattern, _), lines)
if match is not None
],
)
)
list(map(validate_date, versions))
while len(versions) > 1:
current: Version = versions.pop(0)
earlier = versions[0]
validate_date(earlier, current)
validate_version(earlier, current)
except AssertionError:
raise Exception("The changelog needs to be updated!")
if __name__ == "__main__":
copy_html(
src=PARENT_DIRECTORY.joinpath("docs", "build", "html"),
dst=PARENT_DIRECTORY.joinpath("dist", "html"),
)
copy_pdf(
src=PARENT_DIRECTORY.joinpath("docs", "build", "latex", "latex", "deareis.pdf"),
dst=PARENT_DIRECTORY.joinpath("dist"),
name="DearEIS",
version_path=PARENT_DIRECTORY.joinpath("version.txt"),
)
validate_changelog(PARENT_DIRECTORY.joinpath("CHANGELOG.md"))