-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
executable file
·307 lines (255 loc) · 9.9 KB
/
update.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
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
#!/usr/bin/env python3
"""
novelWriter.io Maintenance Script
"""
import argparse
import json
import re
import sys
import urllib.request
from datetime import datetime
from pathlib import Path
from tools import AssetType, Documentation, DownloadAssets
def stripVersion(version: str) -> str:
"""Strip the pre-release part from a version number."""
if "a" in version:
return version.partition("a")[0]
elif "b" in version:
return version.partition("b")[0]
elif "rc" in version:
return version.partition("rc")[0]
else:
return version
def updateSetting(name: str, value: str) -> None:
"""Update a setting in the settings.json file."""
setFile = Path("source/settings.json")
if setFile.exists():
settings = json.loads(setFile.read_text())
else:
settings = {}
if isinstance(settings, dict):
settings[name] = value
setFile.write_text(json.dumps(settings, indent=2))
return
def writeReleaseInfo(version: str, force: bool, info: dict[str, str | dict[str, str]]) -> None:
"""Write the release info file."""
relFile = Path("source/_extra/release-latest.json")
current = json.loads(relFile.read_text(encoding="utf-8")) if relFile.exists() else {}
release = current.get("release", {})
numeric = tuple(int(x) for x in f"{version}.0.0.0".split(".")[:3])
previous = (release.get("major", 0), release.get("minor", 0), release.get("patch", 0))
if previous > numeric and not force:
print(("Current release info is for %s, not updating. "
"Use --force to override.") % release.get("version", "0"))
return
relFile.write_text(
json.dumps({
"release": {
"version": version,
"major": numeric[0],
"minor": numeric[1],
"patch": numeric[2],
"info": info,
}
}, indent=2), encoding="utf-8"
)
return
def processReleaseNotes(text):
"""Format the release notes text."""
def ghLinks(x):
return f"`#{x.group(1)} <https://github.com/vkbo/novelWriter/issues/{x.group(1)}>`_"
buffer = []
for line in text.splitlines():
if line.startswith("### "):
title = line.lstrip("#").lstrip()
icon = None
if "release" in title.lower():
icon = "info"
elif "changelog" in title.lower():
icon = "tasklist"
buffer.append(f".. dropdown:: {title.title()}")
buffer.append(" :animate: fade-in-slide-down")
if icon:
buffer.append(f" :icon: {icon}")
elif line.startswith("#"):
title = line.lstrip("#").lstrip()
buffer.append(f" **{title}**")
elif line.startswith("_") and line.endswith("_"):
text = line.strip("_")
buffer.append(f" *{text}*")
else:
line = line.replace("`", "``")
line = re.sub(r"#([0-9]+)\b", ghLinks, line)
buffer.append(f" {line}".rstrip())
buffer.append("")
return "\n".join(buffer)
##
# Documentation
##
def pullDocs(args):
"""Download docs and extract them into the website source."""
print("")
print("Pulling Documentation Source")
print("============================")
print("")
if args.branch:
target = args.branch
isBranch = True
elif args.tag:
target = args.tag
isBranch = False
else:
print("ERROR: Not tag or branch specified")
sys.exit(1)
docs = Documentation(target, isBranch=isBranch)
docs.pullDocs()
docs.writeDocsCopy()
updateSetting("docVersion", docs.release)
print("")
print("Docs updated")
print("")
return
##
# Releases
##
def pullRelease(args):
"""Download release info from the GitHub API."""
print("")
print("Pulling Release Info")
print("====================")
print("")
if args.remove_pre:
print("Removing Pre-Release")
Path("source/generated/download_pre_release.rst").write_text(
"*There is currently no pre-release available ...*", encoding="utf-8"
)
return
print(f"Tag: {args.tag}")
apiUrl = f"https://api.github.com/repos/vkbo/novelwriter/releases/tags/{args.tag}"
urlReq = urllib.request.Request(apiUrl)
urlReq.add_header("User-Agent", "Mozilla/5.0 (compatible; novelWriter (Python))")
urlReq.add_header("Accept", "application/vnd.github.v3+json")
urlData = urllib.request.urlopen(urlReq, timeout=10)
data = json.loads(urlData.read().decode())
outDir = Path("_temp")
outDir.mkdir(exist_ok=True)
with open(outDir / "api_data.json", mode="w") as apiDump:
json.dump(data, apiDump, indent=2)
releaseUrl = data.get("html_url", "Unknown")
releaseVersion = data.get("name", "Version ???")
releaseDate = data.get("published_at", "")
shortVersion = data.get("tag_name", "???").lstrip("v")
releaseRef = "main_release_" + "_".join(stripVersion(shortVersion).split(".")[:2])
isPreRelease = data.get("prerelease", False)
tarBall = data.get("tarball_url", "")
zipBall = data.get("zipball_url", "")
print(f"Release URL: {releaseUrl}")
print(f"Release Version: {releaseVersion}")
print(f"Release Short: {shortVersion}")
print(f"Release Date: {releaseDate}")
print(f"Release TarBall: {tarBall}")
print(f"Release ZipBall: {zipBall}")
print("")
releaseDateFmt = datetime.fromisoformat(releaseDate).strftime("%B %-d, %Y")
assets = DownloadAssets(data)
print("")
# Update Files
print("Updating Files")
aAppImg = assets.getAsset(AssetType.APP_IMAGE)
aDebian = assets.getAsset(AssetType.DEBIAN)
aWinExe = assets.getAsset(AssetType.WINDOWS_EXE)
aMacAMD = assets.getAsset(AssetType.MAC_DMG_INTEL)
aMacARM = assets.getAsset(AssetType.MAC_DMG_ARM)
if isPreRelease:
# Updating Pre-Release Info
buildFromTemplate("download_release.rst", "download_pre_release.rst", {
"release_version": releaseVersion,
"release_date": releaseDateFmt,
"release_url": releaseUrl,
"release_ref": releaseRef,
"download_tabs": assets.generateDownloadTabs(
f"novelWriter-{shortVersion}.zip", zipBall,
f"novelWriter-{shortVersion}.tar.gz", tarBall
),
})
else:
# Write the release-info.json file
writeReleaseInfo(shortVersion, args.force, {
"name": releaseVersion,
"date": releaseDate,
"url": releaseUrl,
"assets": {
"appimage": aAppImg.assetUrl,
"debian": aDebian.assetUrl,
"winexe": aWinExe.assetUrl,
"macx86": aMacAMD.assetUrl,
"macarm": aMacARM.assetUrl,
"zipball": zipBall,
"tarball": tarBall,
}
})
# Updating Latest Release Info
buildFromTemplate("download_block.rst", "download_block.rst", {
"release_version": releaseVersion,
"release_date": releaseDateFmt,
"release_ref": releaseRef,
"appimage_download": aAppImg.assetUrl,
"debian_download": aDebian.assetUrl,
"winexe_download": aWinExe.assetUrl,
"macx86_download": aMacAMD.assetUrl,
"macarm_download": aMacARM.assetUrl,
})
buildFromTemplate("checksum_block.rst", "checksum_block.rst", {
"appimage_name": aAppImg.assetName,
"appimage_shasum": aAppImg.assetShaSum,
"appimage_shasumfile": aAppImg.assetShaSumUrl,
"debian_name": aDebian.assetName,
"debian_shasum": aDebian.assetShaSum,
"debian_shasumfile": aDebian.assetShaSumUrl,
"winexe_name": aWinExe.assetName,
"winexe_shasum": aWinExe.assetShaSum,
"winexe_shasumfile": aWinExe.assetShaSumUrl,
"macx86_name": aMacAMD.assetName,
"macx86_shasum": aMacAMD.assetShaSum,
"macx86_shasumfile": aMacAMD.assetShaSumUrl,
"macarm_name": aMacARM.assetName,
"macarm_shasum": aMacARM.assetShaSum,
"macarm_shasumfile": aMacARM.assetShaSumUrl,
})
buildFromTemplate("download_release.rst", "download_release.rst", {
"release_version": releaseVersion,
"release_date": releaseDateFmt,
"release_url": releaseUrl,
"release_ref": releaseRef,
"download_tabs": assets.generateDownloadTabs(
f"novelWriter-{shortVersion}.zip", zipBall,
f"novelWriter-{shortVersion}.tar.gz", tarBall
),
})
print("")
return
def buildFromTemplate(name, output, data):
"""Write data to a template."""
output = output or name
templateDir = Path("templates")
generateDir = Path("source/generated")
templateText = (templateDir / name).read_text().format(**data)
if not output:
return templateText
(generateDir / output).write_text(templateText, encoding="utf-8")
print(f"Updated: {generateDir / output}")
return None
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subParsers = parser.add_subparsers()
pDocs = subParsers.add_parser("docs", help="Update documentation")
pDocs.add_argument("--branch", type=str, help="Pull docs from a specific branch")
pDocs.add_argument("--tag", type=str, help="Pull docs from a specific tag")
pDocs.set_defaults(func=pullDocs)
pRelease = subParsers.add_parser("release", help="Update release")
pRelease.add_argument("--tag", type=str, help="Pull release info from a specific tag")
pRelease.add_argument("--remove-pre", action="store_true", help="Remove pre release")
pRelease.add_argument("--force", action="store_true", help="Force update of release info")
pRelease.set_defaults(func=pullRelease)
args = parser.parse_args()
args.func(args)