-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathbuild.py
executable file
·308 lines (256 loc) · 9.14 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#! /usr/bin/env python
# :coding: utf-8
# :copyright: Copyright 2012 Martin Pengelly-Phillips
# :license: See LICENSE.txt.
import sys
import os
import argparse
import shutil
import logging
import subprocess
import Queue
import threading
from fnmatch import fnmatch
import re
def main(arguments=None):
'''Build Dbootstrap.'''
if arguments is None:
arguments = sys.argv[1:]
# Parse arguments
parser = argparse.ArgumentParser(description='Dbootstrap build.')
parser.add_argument('target', choices=['theme', 'demo'],
help='Target to build.')
parser.add_argument('--no-prune', action='store_true',
help='If set, prevent removal of unnecessary files.')
parser.add_argument('--verbosity', '-v', default='info',
choices=['debug', 'info', 'warning', 'error',
'critical'],
help='Logging output verbosity.')
namespace = parser.parse_args()
# Setup logging
level = getattr(logging, namespace.verbosity.upper())
logging.basicConfig(level=level)
log = logging.getLogger('dbootstrap.build')
# Configure common variables
target = namespace.target
project_path = os.path.abspath(os.path.dirname(__file__))
source_path = os.path.join(project_path, 'source')
build_path = os.path.join(project_path, 'build', target)
log.info('Building {0} to {1}'.format(target, build_path))
issues = 0
temporary_files = set()
# Clean build folder
if os.path.exists(build_path):
log.info('Removing existing build folder at {0}'.format(build_path))
shutil.rmtree(build_path)
# Build
log.info('Building CSS.')
nib_lib_path = os.path.join(source_path, 'nib', 'lib')
dbootstrap_theme_path = os.path.join(
source_path, 'dbootstrap', 'theme', 'dbootstrap'
)
result = execute([
'stylus', '--include', nib_lib_path,
'--include', dbootstrap_theme_path,
os.path.join(dbootstrap_theme_path, 'index.styl')
])
if not result:
issues += 1
dbootstrap_css_path = os.path.join(dbootstrap_theme_path, 'dbootstrap.css')
if os.path.exists(dbootstrap_css_path):
os.remove(dbootstrap_css_path)
os.rename(
os.path.join(dbootstrap_theme_path, 'index.css'),
dbootstrap_css_path
)
temporary_files.add(dbootstrap_css_path)
if target == 'demo':
result = execute([
'stylus', '--include', nib_lib_path,
os.path.join(source_path, 'gallery', 'theme', 'gallery.styl')
])
if not result:
issues += 1
temporary_files.add(
os.path.join(source_path, 'gallery', 'theme', 'gallery.css')
)
loader_path = os.path.join(source_path, 'gallery', 'entry_point.js')
profile_path = os.path.join(
project_path, 'resource', 'gallery_profile.js'
)
log.info('Building Javascript packages.')
result = execute([
'node', unix_style(os.path.join(source_path, 'dojo', 'dojo.js')),
'load=build',
'--require', unix_style(loader_path),
'--profile', unix_style(profile_path),
'--releaseDir', unix_style(build_path)
])
if not result:
issues += 1
# Copy html index file and alter to run from built files instead of
# source.
log.info('Copying and modifying HTML index file.')
index_file = os.path.join(build_path, 'index.html')
shutil.copy(
os.path.join(source_path, 'index.html'),
index_file
)
with open(index_file, 'r+') as file:
contents = file.read()
contents = re.sub('isDebug: 1', 'deps: ["gallery"]', contents)
contents = re.sub(
'.+src=\'gallery/entry_point\.js\'.+\n.+\</script\>.*\n',
'',
contents
)
file.seek(0)
file.truncate()
file.write(contents)
elif target == 'theme':
log.info('Building Javascript packages.')
profile_path = os.path.join(
project_path, 'resource', 'dbootstrap_profile.js'
)
result = execute([
'node', unix_style(os.path.join(source_path, 'dojo', 'dojo.js')),
'load=build',
'--profile', unix_style(profile_path),
'--releaseDir', unix_style(build_path)
])
if not result:
issues += 1
# Remove all unnecessary files.
if not namespace.no_prune:
log.info('Removing temporary and unnecessary files.')
for filepath in temporary_files:
log.debug('Removing {0}'.format(filepath))
os.remove(filepath)
if target == 'demo':
for package in ('dojox', 'put-selector', 'xstyle'):
package_path = os.path.join(build_path, package)
log.debug('Removing {0}'.format(package_path))
shutil.rmtree(package_path)
elif target == 'theme':
for package in ('dojo', 'dijit', 'xstyle'):
package_path = os.path.join(build_path, package)
log.debug('Removing {0}'.format(package_path))
shutil.rmtree(package_path)
if target == 'demo':
keepers = [
'*index.html',
'*nls*',
# Gallery
'*gallery/main.js',
'*gallery/theme/gallery.css',
# Dojo
'*dojo/dojo.js',
'*dojo/resources/blank.gif',
'*dijit/themes/a11y/indeterminate_progress.gif',
# Dbootstrap theme
'*dbootstrap/main.js',
'*package.json',
'*dbootstrap.css',
'*dbootstrap/dijit.css',
'*font/fontawesome-webfont*',
# Dgrid css
'*dgrid/css/dgrid.css'
]
elif target == 'theme':
keepers = [
'*dbootstrap/main.js',
'*package.json',
'*dbootstrap.css',
'*dbootstrap/dijit.css',
'*font/fontawesome-webfont*'
]
for path, directories, files in os.walk(build_path, topdown=False):
for name in files:
keep = False
filepath = os.path.join(path, name)
for pattern in keepers:
if fnmatch(filepath, pattern):
keep = True
break
if not keep:
log.debug('Removing {0}'.format(filepath))
os.remove(filepath)
# Remove empty directories
if not os.listdir(path):
log.debug('Removing empty directory {0}'.format(path))
os.rmdir(path)
if issues:
log.info('Build completed with issues.')
else:
log.info('Build completed successfully.')
def unix_style(path):
'''Return path converted to unix format.'''
return path.replace('\\', '/')
class CommandExecutionException(Exception):
'''Raise when a command execution issue occurs.'''
def execute(command):
'''Run the given *command* in a subprocess streaming output to a log.'''
log = logging.getLogger('dbootstrap.build.execute')
command = map(str, command)
# Windows doesn't auto resolve .cmd extensions so add manually when
# required.
if os.name in ('nt',):
if command[0] == 'stylus':
command[0] += '.cmd'
log.debug('Running command: {0}'.format(' '.join(command)))
try:
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
except OSError, error:
raise CommandExecutionException(
'{0} failed due to error: {1}.'.format(
' '.join(command),
str(error)
)
)
def _publish(handle, queue):
'''Stream output from *handle* into *queue*.'''
for line in iter(handle.readline, b''):
queue.put(line)
handle.close()
queue = Queue.Queue()
thread = threading.Thread(target=_publish, args=(process.stdout, queue))
thread.daemon = True
thread.start()
# Collect output whilst process executing
while True:
process.poll()
try:
line = queue.get(False)
except Queue.Empty:
if process.returncode is not None:
break
else:
line = line.strip()
if line:
log.debug(line)
process.wait()
thread.join()
while True:
try:
line = queue.get(False)
except Queue.Empty:
break
else:
line = line.strip()
if line:
log.debug(line)
# Check return code
return_code = process.returncode
if not return_code == 0:
log.warning('Command {0} exited with code {1}'.format(
' '.join(command),
return_code
))
return False
return True
if __name__ == '__main__':
raise SystemExit(main())