-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.py
203 lines (156 loc) · 5.92 KB
/
build.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
import sass
import shutil
import http.server
import socketserver
import os
import watchgod
import argparse
import hashlib
import shortuuid
import datetime
import json
from pathlib import Path
from jinja2 import Template
DIST = "./web/dist"
WEB = "./web"
LOCAL = "./web/local"
SRC = "./web/src"
DATA = "./data"
CUSTOM = "./custom"
CONFIG = "./config"
ASSETS = "./web/assets"
JAVASCRIPT_SRC = "./web/src/js"
SASS_SRC = "./web/src/css"
HTML_SRC = "./web/src/html"
def build_html(folder_name, prefix=""):
all_source_files = os.listdir(HTML_SRC)
jinja_files = [f for f in all_source_files if f.endswith(".jinja2")]
templates = {}
index_raw = ""
for jinja_file in jinja_files:
index_name = jinja_file.replace(".html.jinja2", "")
with open(f"{HTML_SRC}/{jinja_file}", "r") as jt:
templates[index_name] = jt.read()
jt.close()
with open(f"{SRC}/index.html.jinja2", "r") as ir:
index_raw = ir.read()
ir.close()
index_template = Template(index_raw)
with open(f"{folder_name}/index.html", "w") as compiled_index:
compiled_index.write(index_template.render(templates=templates, prefix=prefix))
compiled_index.close()
def compile_css_assets(folder_name, prefix=""):
all_source_files = os.listdir(SASS_SRC)
sass_files = [f for f in all_source_files if f.endswith(".scss")]
full_sass = ""
sass_files.sort() # so variables get processed first
for css_file in sass_files:
print(f"Ingesting '{css_file}'.")
with open(f"{SASS_SRC}/{css_file}", "r") as style:
full_sass = f"{full_sass}{style.read()}\n"
style.close()
css_out = sass.compile(string=full_sass)
with open(f"{folder_name}/{prefix}styles.css", "w") as compiled_style:
compiled_style.write(css_out)
compiled_style.close()
def compile_javascript_assets(folder_name, prefix=""):
all_source_files = os.listdir(JAVASCRIPT_SRC)
js_files = [f for f in all_source_files if f.endswith(".js")]
full_js = ""
for js_file in js_files:
with open(f"{JAVASCRIPT_SRC}/{js_file}", "r") as script:
full_js = f"{full_js}{script.read()}\n\n"
script.close()
with open(f"{folder_name}/{prefix}app.js", "w") as compiled_js:
compiled_js.write(full_js)
compiled_js.close()
def local():
print("Serving fresh copy of assets.")
print("Compiling CSS and Javascript.")
compile_css_assets(LOCAL)
compile_javascript_assets(LOCAL)
print("Building HTML.")
build_html(LOCAL)
print("Copying data files.")
copy_data(LOCAL)
print("Web server running.")
serve_local(8080)
def serve_local(port):
web_dir = os.path.join(os.path.dirname(__file__), LOCAL)
os.chdir(web_dir)
handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), handler)
print(f"Listening on port '{port}'.")
httpd.serve_forever()
def copy_data(folder_name, force_fresh=False):
for m in ["airline", "aircraft", "airport"]:
source_file_name = f"{DATA}/{m}.json"
destination_file_name = f"{folder_name}/data/{m}.json"
Path(f"{folder_name}/data").mkdir(parents=True, exist_ok=True)
copy_file(source_file_name, destination_file_name, force_fresh)
def delete_dist():
fid = os.listdir(DIST)
filtered_files = [f for f in fid if f.endswith(".html") or f.endswith(".js") or f.endswith(".css")]
for file in filtered_files:
path_to_file = os.path.join(DIST, file)
os.remove(path_to_file)
print(f"Deleting existing files at '{path_to_file}'.")
def build():
delete_dist()
prefix = (shortuuid.uuid()[:7] + "_").lower()
print("Compiling CSS.")
compile_css_assets(DIST, prefix)
compile_javascript_assets(DIST, prefix)
print("Copying HTML and JS.")
build_html(DIST, prefix)
copy_data(DIST)
copy_static_assets()
generate_version_references(prefix)
def copy_file(source_file_name, destination_file_name, force_fresh=False):
changes = True
if os.path.isfile(source_file_name):
if os.path.isfile(destination_file_name):
if not force_fresh:
source_hash = hashlib.md5(open(source_file_name, "rb").read()).hexdigest()
destination_hash = hashlib.md5(open(destination_file_name, "rb").read()).hexdigest()
if source_hash == destination_hash:
changes = False
if changes:
print("There have been changes, copying new data files.")
shutil.copy2(source_file_name, destination_file_name)
else:
print("Sorry, the source file doesn't exist. Try 'generate' first.")
def generate_version_references(prefix):
data_version = prefix[:-1]
build_date = datetime.datetime.now()
build_date = str(build_date.replace(tzinfo=datetime.timezone.utc))
version = "v0.0.0"
with open("VERSION", "r") as v:
version = v.read()
v.close()
version_details = {
"build_date": build_date,
"data_version": data_version,
"version": version
}
f = open(f"{DIST}/version.json", "wt")
f.write(json.dumps(version_details, indent=4))
f.close()
print(f"Deployed version '{version}'.")
def copy_static_assets():
print("Copying static assets.")
static_assets = os.listdir(ASSETS)
for sa in static_assets:
source_file = os.path.join(ASSETS, sa)
destination_file = os.path.join(DIST, sa)
copy_file(source_file, destination_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Build tool for sim-atc.com.")
parser.add_argument("command", metavar="C", type=str, help="build, generate, local, or deploy")
args = parser.parse_args()
if args.command == "local":
watchgod.run_process(f"{SRC}", local, watcher_cls=watchgod.AllWatcher)
elif args.command == "build":
build()
else:
print("Sorry, that wasn't a valid command. Try again.")