-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
162 lines (124 loc) · 5.05 KB
/
Copy pathbuild.py
File metadata and controls
162 lines (124 loc) · 5.05 KB
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
"""Build and render functions for whole sites and single HTML pages."""
import logging
import shutil
import traceback
from logging import LoggerAdapter
from subprocess import CalledProcessError
from typing import TYPE_CHECKING
from sites import SITES_MAP
from utils import PROJECT_ROOT
if TYPE_CHECKING:
from collections.abc import Mapping, Sequence
from collections.abc import Set as AbstractSet
from logging import Logger
from pathlib import Path, PurePosixPath
from typing import Final
import htpy as h
from utils import CaughtException
__all__: Sequence[str] = ("build_all_sites", "build_single_page", "build_single_site")
logger: Final[Logger] = logging.getLogger("static-websites-builder")
extra_context_logger: Final[Logger] = logging.getLogger(
"static-websites-builder-extra-context"
)
def build_single_page(
*,
page_path: PurePosixPath,
page_content: h.Element,
site_name: str,
site_deploy_directory: Path,
) -> None:
"""Render a single HTML page into a string output."""
if page_path.is_absolute():
INVALID_PAGE_PATH_MESSAGE: str = (
"Page path must be relative to site name (cannot be an absolute `PurePosixPath`)."
)
raise ValueError(INVALID_PAGE_PATH_MESSAGE)
PAGE_LOGGER: LoggerAdapter[Logger] = LoggerAdapter(
extra_context_logger, {"extra_context": f"{site_name}/{page_path.as_posix()}"}
)
DEPLOY_PAGE_PATH: Path = site_deploy_directory / page_path
DEPLOY_PAGE_PATH.parent.mkdir(parents=True, exist_ok=True)
rendered_page: str = str(page_content)
PAGE_LOGGER.debug("HTML successfully rendered.")
DEPLOY_PAGE_PATH.write_text(f"{rendered_page.strip()}\n", encoding="utf-8")
PAGE_LOGGER.debug("Rendered HTML file successfully saved to `deploy/` directory.")
def build_single_site(
*,
site_name: str,
site_pages: Mapping[PurePosixPath, h.Element],
site_deploy_directory: Path,
) -> None:
"""Render a single site's HTML pages into string outputs."""
SITE_LOGGER: Final[LoggerAdapter[Logger]] = LoggerAdapter(
extra_context_logger, {"extra_context": site_name}
)
SITE_LOGGER.debug("Begin building single site.")
SITE_LOGGER.debug("Creating `deploy/` directory.")
if site_deploy_directory.exists():
shutil.rmtree(site_deploy_directory)
site_deploy_directory.mkdir(parents=True)
SITE_LOGGER.debug(
"Creating symlink to original static directory from inside `deploy/` directory."
)
static_dir: Path = PROJECT_ROOT / f"static/{site_name}"
if static_dir.is_dir():
(site_deploy_directory / "static").symlink_to(static_dir, target_is_directory=True)
page_path: PurePosixPath
page_content: h.Element
for page_path, page_content in site_pages.items():
build_single_page(
page_path=page_path,
page_content=page_content,
site_name=site_name,
site_deploy_directory=site_deploy_directory,
)
SITE_LOGGER.debug("Completed building single site successfully.")
def build_all_sites() -> AbstractSet[Path]:
"""Render all sites HTML pages into string outputs."""
logger.info("Begin building all sites.")
built_sites: dict[Path, CaughtException | None] = {}
site_name: str
site_pages: Mapping[PurePosixPath, h.Element]
for site_name, site_pages in SITES_MAP.items():
site_deploy_directory: Path = PROJECT_ROOT / f"deploy/{site_name}"
try:
build_single_site(
site_name=site_name,
site_pages=site_pages,
site_deploy_directory=site_deploy_directory,
)
except (
ValueError,
RuntimeError,
AttributeError,
TypeError,
OSError,
CalledProcessError,
) as caught_exception:
built_sites[site_deploy_directory] = caught_exception
continue
else:
built_sites[site_deploy_directory] = None
site_path: Path
build_outcome: CaughtException | None
for site_path, build_outcome in built_sites.items():
FORMATTED_SITE_NAME: str = (
site_path.parent.name if site_path.name == "deploy" else site_path.name
)
site_name_logger: LoggerAdapter[Logger] = LoggerAdapter(
extra_context_logger, {"extra_context": FORMATTED_SITE_NAME}
)
build_failed_logger: LoggerAdapter[Logger] = LoggerAdapter(
extra_context_logger, {"extra_context": f"{FORMATTED_SITE_NAME} | Build Failed"}
)
if build_outcome is None:
continue
traceback_messages: Sequence[str] = traceback.format_exception(build_outcome)
build_failed_logger.error(traceback_messages[-1].strip())
site_name_logger.debug("%s\n", "".join(traceback_messages[:-1]).strip())
built_site_paths: AbstractSet[Path] = {
site_path for site_path, build_outcome in built_sites.items() if build_outcome is None
}
if built_site_paths:
logger.info("Building all sites completed successfully.")
return built_site_paths