This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
generated from communitiesuk/funding-service-design-TEMPLATE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.py
106 lines (83 loc) · 3.29 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
import glob
import os
import shutil
import urllib.request
import zipfile
import static_assets
def build_govuk_assets(static_dist_root="app/static/dist"):
DIST_ROOT = "./" + static_dist_root
GOVUK_DIR = "/govuk-frontend"
GOVUK_URL = "https://github.com/alphagov/govuk-frontend/releases/download/v4.7.0/release-v4.7.0.zip"
ZIP_FILE = "./govuk_frontend.zip"
DIST_PATH = DIST_ROOT + GOVUK_DIR
ASSETS_DIR = "/assets"
ASSETS_PATH = DIST_PATH + ASSETS_DIR
# Checks if GovUK Frontend Assets already built
if os.path.exists(DIST_PATH):
print("GovUK Frontend assets already built.If you require a rebuild manually run build.build_govuk_assets")
return True
# Download zips from GOVUK_URL
# There is a known problem on Mac where one must manually
# run the script "Install Certificates.command" found
# in the python application folder for this to work.
print("Downloading static file zip.")
urllib.request.urlretrieve(GOVUK_URL, ZIP_FILE) # nosec
# Attempts to delete the old files, states if
# one doesn't exist.
print("Deleting old " + DIST_PATH)
try:
shutil.rmtree(DIST_PATH)
except FileNotFoundError:
print("No old " + DIST_PATH + " to remove.")
# Extract the previously downloaded zip to DIST_PATH
print("Unzipping file to " + DIST_PATH + "...")
with zipfile.ZipFile(ZIP_FILE, "r") as zip_ref:
zip_ref.extractall(DIST_PATH)
# Move files from ASSETS_PATH to DIST_PATH
print("Moving files from " + ASSETS_PATH + " to " + DIST_PATH)
for file_to_move in os.listdir(ASSETS_PATH):
shutil.move("/".join([ASSETS_PATH, file_to_move]), DIST_PATH)
# Update relative paths
print("Updating relative paths in css files to " + GOVUK_DIR)
cwd = os.getcwd()
os.chdir(DIST_PATH)
for css_file in glob.glob("*.css"):
# Read in the file
with open(css_file, "r") as file:
filedata = file.read()
# Replace the target string
filedata = filedata.replace(ASSETS_DIR, ASSETS_DIR + GOVUK_DIR)
# Write the file out again
with open(css_file, "w") as file:
file.write(filedata)
os.chdir(cwd)
# Copy css
os.makedirs("./app/static/dist/styles")
shutil.copyfile(
"app/static/src/styles/landing.css",
"./app/static/dist/styles/landing.css",
)
shutil.copyfile(
"app/static/src/styles/govuk-overrides.css",
"./app/static/dist/styles/govuk-overrides.css",
)
shutil.copyfile(
"app/static/src/styles/comments.css",
"./app/static/dist/styles/comments.css",
)
# Copy over JS source
os.makedirs("./app/static/dist/js")
shutil.copyfile("app/static/src/js/fsd_cookies.js", "./app/static/dist/js/fsd_cookies.js")
# Delete temp files
print("Deleting " + ASSETS_PATH)
shutil.rmtree(ASSETS_PATH)
os.remove(ZIP_FILE)
def build_all(static_dist_root="app/static/dist", remove_existing=False):
if remove_existing:
relative_dist_root = "./" + static_dist_root
if os.path.exists(relative_dist_root):
shutil.rmtree(relative_dist_root)
build_govuk_assets(static_dist_root=static_dist_root)
static_assets.build_bundles(static_folder=static_dist_root)
if __name__ == "__main__":
build_all(remove_existing=True)