-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathminify_static.py
More file actions
executable file
·99 lines (76 loc) · 2.73 KB
/
minify_static.py
File metadata and controls
executable file
·99 lines (76 loc) · 2.73 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
import os
import shutil
import cssmin
import jsmin
print("Minify Static Files (Javascripts & Stylesheets)")
DIR = os.path.abspath(os.path.dirname(__file__))
STATIC_DIR = os.path.join(DIR, "site", "static")
SOURCE_DIR = os.path.join(STATIC_DIR, "compiled")
OUTPUT_DIR = os.path.join(STATIC_DIR, "minified")
def clear_dir(dirname, ignore_errors=True):
shutil.rmtree(dirname, ignore_errors=ignore_errors)
def create_dir(dirname, remove_existing=True):
if remove_existing and os.path.exists(dirname):
clear_dir(dirname)
os.mkdir(dirname)
def copy_dir(source_dir, output_dir):
if not os.path.exists(source_dir):
return
if os.path.exists(output_dir):
clear_dir(output_dir)
shutil.copytree(source_dir, output_dir)
if __name__ == "__main__":
# create/clear output dir
create_dir(OUTPUT_DIR)
# copy fonts
print("Copying fonts...")
copy_dir(
os.path.join(SOURCE_DIR, "fonts"),
os.path.join(OUTPUT_DIR, "fonts")
)
# copy images
print("Copying images...")
copy_dir(
os.path.join(SOURCE_DIR, "images"),
os.path.join(OUTPUT_DIR, "images")
)
# copy contrib javascripts
print("Copying contrib javascripts...")
copy_dir(
os.path.join(SOURCE_DIR, "javascript", "contrib"),
os.path.join(OUTPUT_DIR, "javascript", "contrib")
)
# copy contrib stylesheets
print("Copying contrib stylesheets...")
copy_dir(
os.path.join(SOURCE_DIR, "stylesheets", "contrib"),
os.path.join(OUTPUT_DIR, "stylesheets", "contrib")
)
# TODO: minify javascripts
js_source_dir = os.path.join(SOURCE_DIR, "javascript")
for pathname in os.listdir(js_source_dir):
path = os.path.join(js_source_dir, pathname)
if os.path.isfile(path):
# open source file
with open(path, 'r') as f:
content = f.read()
# minify
minified = jsmin.jsmin(content)
# write minified output file
output_filepath = os.path.join(OUTPUT_DIR, "javascript", pathname)
with open(output_filepath, 'w') as f:
f.write(minified)
# minify stylesheets
css_source_dir = os.path.join(SOURCE_DIR, "stylesheets")
for pathname in os.listdir(css_source_dir):
path = os.path.join(css_source_dir, pathname)
if os.path.isfile(path):
# open source file
with open(path, 'r') as f:
content = f.read()
# minify
minified = cssmin.cssmin(content)
# write minified output file
output_filepath = os.path.join(OUTPUT_DIR, "stylesheets", pathname)
with open(output_filepath, 'w') as f:
f.write(minified)