-
Notifications
You must be signed in to change notification settings - Fork 10
/
preview.py
225 lines (164 loc) · 6.53 KB
/
preview.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
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import datetime
import pathlib
import jinja2
import markupsafe
import markdown
import os
import sys
import time
import requests
from loader import notes_dirs
from loader import ReleaseNotes
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
TEMPLATE_DOWNLOAD_URL = 'https://raw.githubusercontent.com/thunderbird/thunderbird-website/master/sites/www.thunderbird.net'
THUNDERBIRD_URL = 'https://www.thunderbird.net'
TEMPLATE_OUT_DIR = '_template'
parser = argparse.ArgumentParser(
description="""
Enter the version number you want to preview.
Example: `python preview.py 52.2.1`
""", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("notesfile", help="version number of the release notes to preview")
parser.add_argument("-r", "--refresh_template", help="whether to download the template from the thunderbird-website repo", action='store_true')
parser.add_argument("-s", "--simple_html", help="use simplified html output", action="store_true")
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
is_email = False
if "email" in sys.argv:
is_email = True
sys.argv.remove("email")
args = parser.parse_args()
def download_template():
"""Downloads the required templates from the thunderbird-website repo"""
base_url = TEMPLATE_DOWNLOAD_URL
files = ('includes/base/base.html',
'includes/base/footer.html',
'includes/base/nav.html',
'includes/base/page.html',
'includes/components/page-separator-cover.html',
'includes/_enonly/release-notes.html',
'includes/release-notes-feed.html')
print("Downloading latest templates from thunderbird-website")
for file in files:
print(f'{base_url}/{file}')
response = requests.get(f'{base_url}/{file}')
response.raise_for_status()
out_file = pathlib.Path(file)
if file.endswith('release-notes.html'):
out_file = out_file.name
out_path = pathlib.Path(TEMPLATE_OUT_DIR) / out_file
# Ensure we have the folder
out_path.parent.mkdir(parents=True, exist_ok=True)
with open(out_path, 'wb') as fh:
fh.write(response.content)
print("Finished downloading latest templates")
def stub_templates():
"""Stubs some template includes out that we don't really need"""
files = ('includes/donation-includes.html',
'includes/lang_switcher.html')
for file in files:
# Ensure we have the folder
pathlib.Path(f'{TEMPLATE_OUT_DIR}/{file}').parent.mkdir(parents=True, exist_ok=True)
with open(f'{TEMPLATE_OUT_DIR}/{file}', 'w') as fh:
fh.write('')
def safe_markdown(text):
if not text:
text = ''
return markupsafe.Markup(markdown.markdown(text))
def l10n_format_date(text):
return text
def url(*args, **kwargs):
return ''
def donate_url(*args, **kwargs):
return ''
def svg(name, *args, **kwargs):
return f'<img src="{THUNDERBIRD_URL}/media/svg/{name}.svg">'
def static(filepath):
return f'{THUNDERBIRD_URL}/media/{filepath}'
def l10n_css(*args, **kwargs):
return ''
def download_thunderbird(*args, **kwargs):
return ''
def thunderbird_url(*args, **kwargs):
return ''
def is_system_requirements_dict(*args, **kwargs):
return False
def f(s, *args, **kwargs):
return s.format(*args, **kwargs)
# Stub to make gettext work, no actual translation.
class Translation(object):
def gettext(self, text):
return text
ugettext = gettext
ngettext = gettext
class Handler(FileSystemEventHandler):
def __init__(self, version, preview_template):
translator = Translation()
self.jinja_env = jinja2.Environment(
extensions=["jinja2.ext.i18n"],
loader=jinja2.FileSystemLoader(os.path.abspath(f'./{TEMPLATE_OUT_DIR}')),
)
self.jinja_env.globals.update(url=url)
self.jinja_env.globals.update(svg=svg)
self.jinja_env.globals.update(static=static)
self.jinja_env.globals.update(l10n_css=l10n_css)
self.jinja_env.globals.update(download_thunderbird=download_thunderbird)
self.jinja_env.globals.update(thunderbird_url=thunderbird_url)
self.jinja_env.globals.update(donate_url=donate_url)
self.jinja_env.globals.update(is_system_requirements_dict=is_system_requirements_dict)
self.jinja_env.globals.update(NOW=datetime.datetime.now())
self.jinja_env.filters['markdown'] = safe_markdown
self.jinja_env.filters['f'] = f
self.jinja_env.filters['l10n_format_date'] = l10n_format_date
self.jinja_env.install_gettext_translations(translator)
self.jinja_env.trim_blocks = True
self.jinja_env.lstrip_blocks = True
self.template = self.jinja_env.get_template(preview_template)
self.version = version
self.updatetime = datetime.datetime.fromtimestamp(0)
self.updatepreview()
def updatepreview(self):
notes = ReleaseNotes()
note = notes.notes.get(self.version)
if not note:
print("Error: Can't find notes for version '{0}'.".format(self.version))
sys.exit(1)
with open("preview.html", "wb") as f:
o = self.template.render(is_preview=True, **note)
f.write(o.encode('utf8'))
def throttle_updates(self, timestamp):
# Multiple FileModified events can fire, so only update
# once per second.
delta = timestamp - self.updatetime
if delta.seconds > 0:
self.updatepreview()
print("{0}: Updated preview.html\n".format(timestamp.strftime("%H:%M:%S")))
self.updatetime = datetime.datetime.now()
def on_modified(self, event):
self.throttle_updates(datetime.datetime.now())
if args.refresh_template or not pathlib.Path('_template/release-notes.html').exists():
download_template()
stub_templates()
preview_template = pathlib.Path('_template/release-notes.html')
if args.simple_html or is_email:
preview_template = pathlib.Path("simple_html.html")
handler = Handler(args.notesfile, preview_template.name)
observer = Observer()
for notedir in notes_dirs:
observer.schedule(handler, path=notedir, recursive=False)
observer.start()
if is_email:
observer.stop()
else:
print("Updating preview.html every time data is modified, press Ctrl-C to end.")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()