-
Notifications
You must be signed in to change notification settings - Fork 0
/
websleydale.py
388 lines (309 loc) · 11.1 KB
/
websleydale.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import asyncio
import logging
import os
import shutil
from dataclasses import asdict, dataclass, field
from datetime import datetime
from itertools import chain
from pathlib import Path
from shlex import quote
from subprocess import PIPE
from tempfile import mkdtemp, mkstemp
from typing import (
Any,
Awaitable,
Counter,
Dict,
Iterable,
List,
Optional,
Set,
Tuple,
Union,
cast,
)
import jinja2
import yaml
from mistletoe import Document, HTMLRenderer
from mistletoe.block_token import Heading
from slugify import slugify
__version__ = "3.0-dev"
logger = logging.getLogger(__name__)
jinjaenv = jinja2.Environment(
loader=jinja2.FileSystemLoader("templates"),
trim_blocks=True,
lstrip_blocks=True,
autoescape=True,
)
tempdir: Optional[Path] = None
root = Path(".")
def outdir() -> Path:
if tempdir is None:
raise RuntimeError("outdir called while tempdir is None")
return Path(mkdtemp(dir=tempdir))
def outfile() -> Path:
if tempdir is None:
raise RuntimeError("outdir called while tempdir is None")
return Path(mkstemp(dir=tempdir)[1])
@dataclass
class Site:
known_authors: Set[Author]
name: str
repo_name: str
repo_url: str
tree: Dict[str, FileProducer]
generator: str = "websleydale"
time: datetime = datetime.now()
@dataclass
class Info:
path: str
site: Site
def build(site: Site, *, dest: str) -> None:
global tempdir
destdir = Path(dest)
if destdir.exists():
logger.debug("Removing dest directory %s", destdir)
shutil.rmtree(destdir)
tempdir = Path(mkdtemp(prefix="websleydale-"))
logger.debug("Using tempdir %s", tempdir)
awaitables = []
paths = []
for pathstr, producer in site.tree.items():
if pathstr.startswith("/"):
raise ValueError(f"Invalid path {pathstr!r} (starts with '/')")
if not isinstance(producer, FileProducer):
raise TypeError(
f"item for path {pathstr!r} has invalid type {type(producer)}"
)
destpath = destdir / pathstr
info = Info(path=pathstr, site=site)
awaitables.append(copy(destpath, producer, info))
paths.append(pathstr)
results = asyncio.run(gather(awaitables))
for result, path in zip(results, paths):
if isinstance(result, Exception):
logger.error(
"[%s] %s: %s", path, result.__class__.__name__, result, exc_info=result
)
elif result is None:
logger.info("[%s] Success!", path)
else:
logger.error("[%s] got unexpected result %r", path, result)
async def copy(dest: Path, source_producer: FileProducer, info: Info) -> None:
source = await source_producer.run(info)
if source.path.is_dir():
logger.debug("Copying directory %s -> %s", source.path, dest)
dest.parent.mkdir(exist_ok=True, parents=True)
shutil.copytree(source.path, dest, copy_function=shutil.copy)
else:
if info.path.endswith("/"):
dest = dest / "index.html"
logger.debug("Copying file %s -> %s", source.path, dest)
dest.parent.mkdir(exist_ok=True, parents=True)
shutil.copy(source.path, dest)
async def gather(awaitables: Iterable[Awaitable[None]],) -> List[Optional[Exception]]:
return cast(
List[Optional[Exception]],
await asyncio.gather(*awaitables, return_exceptions=True),
)
@dataclass
class Result:
sourceinfo: Optional[SourceInfo]
@dataclass
class FileResult(Result):
path: Path
@dataclass
class TextResult(Result):
content: str
pageinfo: Dict[str, Any]
class FileProducer:
async def run(self, info: Info) -> FileResult:
...
class TextProducer:
async def run(self, info: Info) -> TextResult:
...
@dataclass(frozen=True)
class Author:
display_name: str
emails: Set[str] = field(hash=False)
url: Optional[str]
@dataclass
class GitFileInfo:
authors: List[Author]
repo_source_path: Optional[str]
repo_url: Optional[str]
updated_date: Optional[datetime]
@dataclass
class SourceInfo:
authors: List[Author]
is_committed: bool
repo_source_path: Optional[str]
repo_url: Optional[str]
updated_date: datetime
class file(FileProducer):
def __init__(self, path: Path) -> None:
self.path = path
if not path.is_file():
raise ValueError("not a file: %s", path)
async def run(self, info: Info) -> FileResult:
logger.debug("[%s] Loading Git info", self.path)
gitinfo = await _git_file_info(self.path, info.site)
sourceinfo = SourceInfo(
authors=gitinfo.authors,
is_committed=gitinfo.updated_date is not None,
repo_source_path=gitinfo.repo_source_path,
repo_url=gitinfo.repo_url,
updated_date=gitinfo.updated_date or datetime.now(),
)
return FileResult(sourceinfo=sourceinfo, path=self.path)
class dir(FileProducer):
def __init__(self, path: Path) -> None:
self.path = path
if not path.is_dir():
raise ValueError("not a dir: %s", path)
async def run(self, info: Info) -> FileResult:
return FileResult(sourceinfo=None, path=self.path)
async def _git_file_info(file: Path, site: Site) -> GitFileInfo:
args = [
"bash",
"-c",
f"cd {quote(str(file.parent))} && git ls-files --full-name {quote(str(file.name))}",
]
proc = await asyncio.create_subprocess_exec(*args, stdout=PIPE)
stdout, _ = await proc.communicate()
if proc.returncode != 0:
raise RuntimeError("git failed")
lines = stdout.decode(errors="replace").splitlines()
repo_source_path = lines[0] if lines else None
args = ["bash", "-c", f"cd {quote(str(file.parent))} && git remote -v"]
proc = await asyncio.create_subprocess_exec(*args, stdout=PIPE)
stdout, _ = await proc.communicate()
if proc.returncode != 0:
raise RuntimeError("git failed")
repo_url = None
for line in stdout.decode(errors="replace").splitlines():
remote_name, remote_url, _ = line.split(maxsplit=2)
if remote_name == "origin":
repo_url = remote_url
break
if repo_url is not None:
if repo_url.endswith(".git"):
repo_url = repo_url[: -len(".git")]
if repo_url.startswith("[email protected]:"):
repo_name = repo_url[len("[email protected]:") :]
repo_url = f"https://github.com/{repo_name}"
args = [
"bash",
"-c",
f"cd {quote(str(file.parent))} && git log --format='%cI %ae %an' -- {quote(str(file.name))}",
]
proc = await asyncio.create_subprocess_exec(*args, stdout=PIPE)
stdout, _ = await proc.communicate()
if proc.returncode != 0:
raise RuntimeError("git failed")
authors: Dict[Author, None] = {}
date = None
for line in stdout.decode(errors="replace").splitlines():
datestr, email, name = line.split(maxsplit=2)
if date is None:
date = datetime.fromisoformat(datestr)
author = next((a for a in site.known_authors if email in a.emails), None)
if author is None:
author = Author(display_name=name, emails={email}, url=None)
authors[author] = None
return GitFileInfo(
authors=list(authors.keys()),
repo_source_path=repo_source_path,
repo_url=repo_url,
updated_date=date,
)
class markdown(TextProducer):
def __init__(self, path: Path) -> None:
self.source = file(path)
async def run(self, info: Info) -> TextResult:
source = await self.source.run(info)
content = source.path.read_text()
pageinfo: Dict[str, Any] = {}
if content.startswith("---\n"):
yamltext, content = content[4:].split("---\n", maxsplit=1)
pageinfo.update(yaml.safe_load(yamltext))
with WebsleydaleHTMLRenderer() as renderer:
rendered = renderer.render(Document(content))
return TextResult(
sourceinfo=source.sourceinfo, content=rendered, pageinfo=pageinfo
)
class fake(TextProducer):
def __init__(self, pageinfo: Optional[Dict[str, Any]] = None) -> None:
self.pageinfo = pageinfo if pageinfo is not None else {}
async def run(self, info: Info) -> TextResult:
sourceinfo = SourceInfo(
authors=[],
is_committed=False,
repo_source_path="",
repo_url=None,
updated_date=datetime.now(),
)
return TextResult(sourceinfo=sourceinfo, content="", pageinfo=self.pageinfo)
class jinja(FileProducer):
def __init__(self, source: TextProducer, template: str) -> None:
self.source = source
self.template = jinjaenv.get_template(template)
async def run(self, info: Info) -> FileResult:
source = await self.source.run(info)
content = source.content
pageinfo = source.pageinfo
dest = outfile()
pageinfo["content"] = content
pageinfo["path"] = info.path
rendered = self.template.render(
page=pageinfo, site=info.site, source=source.sourceinfo
)
dest.write_text(rendered)
return FileResult(sourceinfo=source.sourceinfo, path=dest)
class sass(FileProducer):
def __init__(self, path: Path) -> None:
self.source = file(path)
async def run(self, info: Info) -> FileResult:
source = await self.source.run(info)
dest = outfile()
args = ["pysassc", "--style", "compressed", str(source.path), str(dest)]
proc = await asyncio.create_subprocess_exec(*args)
exitcode = await proc.wait()
if exitcode != 0:
raise RuntimeError("pysassc failed")
return FileResult(sourceinfo=source.sourceinfo, path=dest)
class IdGenerator:
def __init__(self) -> None:
self.used_ids: Counter[str] = Counter()
def get_id(self, text: str) -> str:
id_base = slugify(text, entities=False, decimal=False, hexadecimal=False)
if id_base in self.used_ids:
real_id = f"{id_base}-{self.used_ids[id_base]}"
else:
real_id = id_base
self.used_ids[id_base] += 1
return real_id
class WebsleydaleHTMLRenderer(HTMLRenderer):
def __init__(self) -> None:
super().__init__()
self.ids = IdGenerator()
def render_heading(self, token: Heading) -> str:
level = token.level
inner = self.render_inner(token)
identifier = self.ids.get_id(self.render_to_plain(token))
return f'<a class=anchor href="#{identifier}"><h{level} id="{identifier}">{inner}</h{level}></a>'
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="wb")
parser.add_argument("rcfile", nargs="?", default="websleydalerc.py")
parser.add_argument("--verbose", action="store_true", help="print lots of messages")
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO, format="%(message)s"
)
logging.getLogger("asyncio").setLevel(logging.INFO)
logger.debug("Loading %r", args.rcfile)
with open(args.rcfile) as f:
exec(f.read(), {})