-
Notifications
You must be signed in to change notification settings - Fork 43
/
make.py
executable file
·262 lines (193 loc) · 6.95 KB
/
make.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python3
# * Imports
import os
import shutil
import subprocess
import sys
import multiprocessing
import functools
from collections import namedtuple
from tempfile import mkstemp
# * Variables
sites_dir="sites"
themes_dir = "themes"
css_dir = "css"
screenshots_dir="screenshots"
phantomjs_command = "phantomjs --ssl-protocol=any --ignore-ssl-errors=true screenshot.js".split()
common_deps = ["styl/index.styl", "styl/mixins.styl"]
CSS = namedtuple("CSS", ['path', 'deps', 'theme', 'site'])
Theme = namedtuple("Theme", ['name', 'styl_path', 'support_files'])
# * Functions
def main():
"Update CSS files by default, or update screenshots."
if len(sys.argv) > 1 and sys.argv[1] == "screenshots":
update_screenshots()
else:
update_css_files()
# ** CSS
def update_css_files():
"Build CSS files that need to be built."
css_files = list_css(themes(), sites())
# Make directories first to avoid race condition
for css in css_files:
dir = os.path.join(css_dir, css.theme.name)
if not os.path.isdir(dir):
os.makedirs(dir)
pool = multiprocessing.Pool(multiprocessing.cpu_count())
pool.map(build, css_files)
def build(css):
"Build CSS file if necessary."
css_mtime = mtime(css.path)
make = False
for dep in css.deps:
if mtime(dep) > css_mtime:
make = True
break
if make:
stylus(css)
def stylus(css):
"Run Stylus to build CSS file."
output_file = css.path
command = ["stylus", "--include", "styl",
"--import", css.theme.styl_path,
"--import", "styl",
"-p", "sites/%s.styl" % css.site]
result = subprocess.check_output(command)
with open(output_file, "wb") as f:
f.write(result)
print(output_file)
# ** Screenshots
def update_screenshots():
"Update screenshots."
css_files = list_css(themes(), sites())
if not os.path.isdir(screenshots_dir):
# If the directory does not exist, create a new worktree for it.
# Assumes the screenshots branch exists.
subprocess.call(["git", "worktree", "prune"])
subprocess.call(["git", "worktree", "add",
screenshots_dir, "screenshots"])
# Make directories first to avoid race condition
for css in css_files:
output_dir = os.path.join(screenshots_dir, css.theme.name)
if not os.path.isdir(output_dir):
os.makedirs(output_dir)
pool = multiprocessing.Pool(multiprocessing.cpu_count())
pool.map(update_screenshot, css_files)
commit_screenshots()
def commit_screenshots():
if os.path.exists(os.path.join(screenshots_dir, ".git")):
subprocess.call(["git", "-C", screenshots_dir,
"add", "-A"])
# amend changes instead of keeping them to save space
subprocess.call(["git", "-C", screenshots_dir,
"commit", "--amend", "-m", "Update screenshots"])
else:
print("screenshot dir was not a worktree, aborting commit")
def update_screenshot(css):
"Update screenshot for CSS if necessary."
screenshot_path = screenshot_path_for_css(css)
if mtime(css.path) > mtime(screenshot_path):
save_screenshot(css)
def screenshot_path_for_css(css):
"Return path of screenshot for CSS."
return os.path.join(screenshots_dir, css.theme.name, "%s.png" % css.site)
def save_screenshot(css):
"Save screenshot for CSS."
# Prepare filename
screenshot_path = screenshot_path_for_css(css)
# Get URL
url = css_screenshot_url(css)
if not url:
# Screenshot disabled
return False
# Prepare command
command = list(phantomjs_command)
command.extend([url, screenshot_path, css.path])
# Run PhantomJS
subprocess.check_output(command)
# Compress with pngcrush
_, tempfile_path = mkstemp(suffix=".png")
subprocess.check_output(["pngcrush", screenshot_path, tempfile_path], stderr=subprocess.DEVNULL)
shutil.move(tempfile_path, screenshot_path)
print(screenshot_path)
def css_screenshot_url(css):
"Return URL for taking screenshots of CSS."
# Get site URL
site_url_filename = os.path.join(sites_dir, css.site + ".url")
if os.path.exists(site_url_filename):
with open(site_url_filename, "r") as f:
url = f.readlines()
if url:
# Use URL given in .url file
url = url[0].strip()
else:
# Use name of site file (without .styl extension)
url = "http://" + css.site
return url
# ** Support
def list_css(themes, sites):
"Return list of CSS files for THEMES and SITES."
return [CSS("%s/%s/%s-%s.css" % (css_dir, theme.name, theme.name,
site.strip('_')),
dependencies(theme, site), theme, site)
for theme in themes
for site in sites]
def themes():
"Return list of themes."
theme_names = []
themes = []
# Make list of theme directories
for d in os.listdir(themes_dir):
theme_names.append(d)
# Iterate over theme directories
for theme in theme_names:
support_files = []
variant_files = []
directory = os.path.join(themes_dir, theme)
# Iterate over files in theme directory
for f in os.listdir(directory):
path = os.path.join(themes_dir, theme, f)
if f == "colors.styl":
# Support file
support_files.append(path)
elif f.endswith(".styl"):
# Theme file
variant_files.append({'variant': without_styl(f), 'path': path})
# Otherwise, not a relevant file
# Add theme object to list
if len(variant_files) == 1:
# Only one variant: omit variant name from theme name
themes.append(Theme(theme, variant_files[0]['path'], support_files))
else:
# Multiple variants: include variant name in theme name
for f in variant_files:
themes.append(Theme("%s-%s" % (theme, f['variant']), f['path'], support_files))
return themes
def sites():
"Return list of sites."
for path, dirs, files in os.walk(sites_dir):
return [site.replace(".styl", "")
for site in files
if site.endswith(".styl")]
def dependencies(theme, site):
"Return list of dependency .styl files for THEME and SITE."
deps = list(common_deps)
deps.append(theme.styl_path)
deps.extend(theme.support_files)
deps.append("sites/%s.styl" % site)
if site == "all-sites":
deps += ["sites/%s.styl" % s for s in sites()]
return deps
@functools.lru_cache()
def mtime(path):
"Return mtime for PATH."
if os.path.isfile(path):
return os.path.getmtime(path)
else:
return 0
def without_styl(s):
"""Return string S without ".styl" extension."""
return s.replace(".styl", "")
# * Footer
if __name__ == "__main__":
main()