-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy_tsviz.py
executable file
·244 lines (182 loc) · 6.66 KB
/
deploy_tsviz.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
#!/usr/bin/python
'''
Code for confimation message obtained from:
http://code.activestate.com/recipes/541096-prompt-the-user-for-confirmation/
Accessed on 03/Nov/2016
Purpose:
=========
This script packages and deploys the tsviz project to:
http://bestchai.bitbucket.org/tsviz/
Usage:
=======
- This script must be run from within the top level tsviz source directory.
- This script must be run from OSX :)
- This script:
1. Generates the documentation for tsviz using jsdoc3
2. Removes the proxy hack that allows tsviz to access log files
when tsviz is run locally.
3. Adds google analytics tracking.
4. Copies over the entire d3/ source tree over to a destination that
is assumed to be the http://bestchai.bitbucket.org/tsviz/ repo.
5. Commits and pushes the http://bestchai.bitbucket.org/tsviz/ repo.
'''
import sys
import os
import httplib
import urllib
import subprocess
def confirm(prompt=None, resp=False):
"""prompts for yes or no response from the user. Returns True for yes and
False for no.
'resp' should be set to the default value assumed by the caller when
user simply types ENTER.
>>> confirm(prompt='Create Directory?', resp=True)
Create Directory? [y]|n:
True
>>> confirm(prompt='Create Directory?', resp=False)
Create Directory? [n]|y:
False
>>> confirm(prompt='Create Directory?', resp=False)
Create Directory? [n]|y: y
True
"""
if prompt is None:
prompt = 'Confirm'
if resp:
prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
else:
prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')
while True:
ans = raw_input(prompt)
if not ans:
return resp
if ans not in ['y', 'Y', 'n', 'N']:
print 'please enter y or n.'
continue
if ans == 'y' or ans == 'Y':
return True
if ans == 'n' or ans == 'N':
return False
def get_cmd_output(cmd, args):
'''
Returns the standard output from running:
$ cmd args[0] args[1] .. args[n]
Where cmd is the command name (e.g., 'svn') and args is a list of
arguments to the command (e.g., ['help', 'log']).
'''
return subprocess.Popen([cmd] + args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()[0]
def runcmd(s):
'''
Logs and runs a shell command.
'''
print "os.system: " + s
return os.system(s)
def minify(branch, info):
'''
Minifies all of the js code under js/ using Google's API and
returns the minified resulting js code.
'''
params = [
('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
('output_format', 'text'),
('output_info', info)
]
url = 'https://bitbucket.org/mhnnunes/tsviz/raw/' + branch + '/'
#Include DEPENDENCIES
for r, d, f in os.walk('local_scripts'):
for f in f:
if f.endswith(".js"):
params += [('code_url', url + os.path.join(r, f))]
# Traverse all of the files underneath the js/ dir
for root, dirs, files in os.walk('js'):
for file in files:
if not ('dev.js' in file):
params += [('code_url', url + os.path.join(root, file))]
urlparams = urllib.urlencode(params)
headers = {'Content-type': 'application/x-www-form-urlencoded'}
conn = httplib.HTTPConnection('closure-compiler.appspot.com')
conn.request('POST', '/compile', urlparams, headers)
response = conn.getresponse()
data = response.read()
conn.close()
return data
def main():
'''
Workhorse method to execute all the of the steps described in the file header.
'''
src_dir = "./"
dist_dir = "../bestchai.bitbucket.org/tsviz/"
print "Deploying to: " + dist_dir
print "from: " + src_dir
# Confirmation message.
if( confirm("Is it okay to remove the previous deployed version?", False) ):
# Remove previously deployed version of tsviz.
if (os.path.exists(dist_dir)):
runcmd("rm -rf " + dist_dir + "*")
else:
print "Error: deployment dir is not where it is expected."
sys.exit(-1)
else:
print "Deployment failed."
sys.exit(-1)
# Copy over the source.
if (os.path.exists(src_dir)):
runcmd("cp -R " + src_dir + "* " + dist_dir)
# Remove js source code since we will be using a minified version (see below).
runcmd("rm -rf " + dist_dir + "/js/*")
else:
print "Error: source dir is not where it is expected."
sys.exit(-1)
# Compile docs
# if (runcmd("perl docgen.pl " + dist_dir) != 0):
# sys.exit(-1)
# Find out the current revision id:
revid = get_cmd_output('git', ['rev-parse', 'HEAD']);
revid = revid.rstrip()
# Find out the current branch:
branch = get_cmd_output('git', ['branch']);
branch = branch.rstrip();
print "Revid is : " + revid
print "Branch is : " + branch
# Remove any files containing '#'
runcmd("cd " + dist_dir + " && find . | grep '#' | xargs rm")
# Remove any files containing '~'
runcmd("cd " + dist_dir + " && find . | grep '~' | xargs rm")
# Remove any files containing '~'
runcmd("cd " + dist_dir + " && find . | grep '.orig' | xargs rm")
# Minify the code
print "Minifying... please wait"
data = minify(revid, 'compiled_code')
print "Minified size: %i" % len(data)
if len(data) < 500:
print "Minification failed!"
print minify(revid, 'errors')
return
print "Minification successful!"
# Add revision id to the minified code.
data = data.replace("revision: ZZZ", "revision: %s" % revid)
# Save the minified code into js/min.js
minified = open(dist_dir + 'js/min.js', 'w')
minified.write(data)
minified.close()
# Replace reference to js files with minified js in deployed version
# of index.html.
runcmd("sed -i '' -e 's/<script[^>]*><\/script>//g' " + dist_dir + "index.html")
runcmd("sed -i '' -e 's/<\/body>/<script src=\"js\/min.js\"><\/script><\/body>/g' " + dist_dir + "index.html")
# Add any files that are new and remove any files that no longer exist
runcmd("cd " + dist_dir + " && hg addremove")
# Commit the deployed dir.
runcmd("cd " + dist_dir + " && hg commit -m 'tsviz auto-deployment'")
# Push the deployed dir.
runcmd("cd " + dist_dir + " && hg push")
# Git version for above:
# runcmd("cd " + dist_dir + " && git add -A")
# runcmd("cd " + dist_dir + " && git commit -m 'shiviz auto-deployment test '")
# runcmd("cd " + dist_dir + " && git push")
print
print "Done."
return
if __name__ == "__main__":
main()