-
Notifications
You must be signed in to change notification settings - Fork 16
/
build_sass.py
123 lines (93 loc) · 4.02 KB
/
build_sass.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
"""
Build the SASS - main.css and optionally the widgets
~~CMSBuildSASS:Script->CMS:Script~~
~~->SASS:Technology~~
"""
import sass
import os
import shutil
from datetime import datetime
# SASS directory
SASS = os.path.join("cms", "sass")
# Output style
STYLE = "compressed"
# SASS and error file for the main CSS
MAIN_SETTINGS = (os.path.join(SASS, "main.scss"),
os.path.join("portality", "static", "doaj", "css", "main.css"),
os.path.join("cms", "error_sass.txt"))
# SASS file and error file for each widget
FQ_WIDGET_SETTINGS = (os.path.join(SASS, "fq_widget.scss"),
os.path.join("portality", "static", "doaj", "css", "fq_widget.css"),
os.path.join("cms", "error_fqw_sass.txt"))
SS_WIDGET_SETTINGS = (os.path.join(SASS, "simple_widget.scss"),
os.path.join("portality", "static", "doaj", "css", "simple_widget.css"),
os.path.join("cms", "error_ssw_sass.txt"))
def _localise_paths(paths, base_path=None):
SCSS_IN, CSS_OUT, ERROR_OUT = paths
SOURCE_MAP = CSS_OUT + ".map"
now = datetime.utcnow().timestamp()
if base_path is None:
return SASS, SCSS_IN, CSS_OUT, CSS_OUT + "." + str(now), SOURCE_MAP, SOURCE_MAP + "." + str(now), ERROR_OUT
return (os.path.join(base_path, SASS),
os.path.join(base_path, SCSS_IN),
os.path.join(base_path, CSS_OUT),
os.path.join(base_path, CSS_OUT + "." + str(now)),
os.path.join(base_path, SOURCE_MAP),
os.path.join(base_path, SOURCE_MAP + "." + str(now)),
os.path.join(base_path, ERROR_OUT))
def _swap(old, new):
if os.path.exists(old):
os.rename(old, old + ".old")
os.rename(new, old)
if os.path.exists(old + ".old"):
try:
shutil.rmtree(old + ".old")
except OSError:
os.remove(old + ".old")
def build(paths, base_path=None):
# ~~->CMSSASS:Build~~
sass_file, main_file, css_file, css_tmp, map_file, map_tmp, error_file = None, None, None, None, None, None, None
try:
sass_file, main_file, css_file, css_tmp, map_file, map_tmp, error_file = _localise_paths(paths, base_path)
css, src_map = sass.compile(filename=main_file,
output_style=STYLE,
source_map_filename=map_file,
include_paths=[sass_file],
omit_source_map_url=True)
# Enable the following line to add the sourcemap URL, since it was being generated incorrectly. Requires serving /cms/sass in STATIC_PATHS
# css += f'\n/*# sourceMappingURL={os.path.basename(map_file)} */'
with open(css_tmp, "w") as f:
f.write(css)
with open(map_tmp, "w") as f:
f.write(src_map)
_swap(css_file, css_tmp)
_swap(map_file, map_tmp)
except Exception as e:
try:
print("Error occurred building sass")
if error_file:
with open(error_file, "w") as f:
f.write(str(e))
if css_tmp and os.path.exists(css_tmp):
os.remove(css_tmp)
if css_file and os.path.exists(css_file + ".old"):
os.rename(css_file + ".old", css_file)
if map_tmp and os.path.exists(map_tmp):
os.remove(map_tmp)
if map_file and os.path.exists(map_file + ".old"):
os.rename(map_file + ".old", map_file)
raise e
except:
print("Error occurred building sass and could not complete cleanup")
raise e
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-w", "--widgets", help="Generate the widgets CSS", action="store_true")
args = parser.parse_args()
# Build the site CSS
build(MAIN_SETTINGS)
# If this is run manually with the widget arg, also build the widgets (intended to commit result to the tree)
if args.widgets:
build(SS_WIDGET_SETTINGS)
build(FQ_WIDGET_SETTINGS)