Repository navigation
Expand file tree
/
Copy pathbuild.py
More file actions
60 lines (49 loc) · 1.66 KB
/
Copy pathbuild.py
File metadata and controls
60 lines (49 loc) · 1.66 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
from data import DATA_DIR, load_data, save_data
from jinja2 import Template, Environment, FileSystemLoader
from pathlib import Path
import shutil
import itertools
import time
import os
TEMPLATE_DIR = Path(__file__).parent / 'templates'
TEMPLATE_STATIC_DIR = TEMPLATE_DIR / 'static'
BUILD_DIR = Path(__file__).parent / '_build'
BUILD_STATIC_DIR = BUILD_DIR / 'static'
BUILD_DATA_DIR = BUILD_DIR / 'api'
def save_rendered(rendered, output_name):
BUILD_DIR.mkdir(exist_ok=True, parents=True)
output_path = BUILD_DIR / output_name
with open(output_path, 'w') as f:
f.write(rendered)
def copy_static():
BUILD_STATIC_DIR.mkdir(exist_ok=True, parents=True)
for item in TEMPLATE_STATIC_DIR.iterdir():
if item.is_file():
dest = BUILD_STATIC_DIR / item.name
if item.name == 'favicon.ico':
# Favicon must be in the root for browsers to find it
dest = BUILD_DIR / item.name
shutil.copy(item, dest)
def copy_data():
shutil.copytree(DATA_DIR, BUILD_DATA_DIR, dirs_exist_ok=True)
def get_context_data():
return dict(
base_path=os.environ.get('BASE_PATH', ''),
)
def build():
start = time.perf_counter()
# Clear previous build
if BUILD_DIR.exists():
shutil.rmtree(BUILD_DIR)
env = Environment(loader=FileSystemLoader(TEMPLATE_DIR))
template = env.get_template('index.html')
save_rendered(
template.render(**get_context_data()),
BUILD_DIR / 'index.html',
)
copy_data()
copy_static()
end = time.perf_counter()
print(f'Build completed in {end - start:.2f} seconds.')
if __name__ == '__main__':
build()