-
Notifications
You must be signed in to change notification settings - Fork 13
/
deploy_shiviz.py
executable file
·217 lines (160 loc) · 6.67 KB
/
deploy_shiviz.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
#!/usr/bin/python
'''
Purpose:
=========
This script packages and deploys the shiviz project to:
https://bestchai.bitbucket.io/shiviz/
Usage:
=======
- This script must be run from within the top level shiviz source directory.
- This script must be run from OSX :)
- This script:
1. Generates the documentation for shiviz using jsdoc3
2. Removes the proxy hack that allows shiviz to access log files
when shiviz 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 https://bitbucket.org/bestchai/shiviz/src/default/ repo.
5. Commits and pushes the https://bitbucket.org/bestchai/bestchai.bitbucket.org/src/default/ repo.
'''
import sys
import os
import fileinput
import subprocess
import argparse
import time
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(revid):
'''
Minifies all of the js code under js/ using Google Closure Compiler and
writes the minified resulting js code to js/min.js.
'''
exit_code = runcmd("google-closure-compiler js/**.js !dev.js local_scripts/**.js --js_output_file js/min.js")
if exit_code != 0:
print("Minification failed!")
sys.exit(-1)
minified_file = fileinput.input("js/min.js", inplace=True)
# Add hg revision id to the minified code.
for line in minified_file:
sys.stdout.write(line.replace("revision: ZZZ", "revision: %s" % revid))
def parse_args():
'''
Method to process the command line arguments. Expects only one of the two following options:
-d or --dev to deploy to bestchai.bitbucket.org/shiviz-dev/
-p or --prod to deploy to bestchai.bitbucket.org/shiviz/
If no flag specified or the specified flag is invalid, outputs the help dialogue.
'''
parser = argparse.ArgumentParser()
argument_group = parser.add_mutually_exclusive_group(required=True)
argument_group.add_argument("-d", "--dev", help="Deploys ShiViz to development environment.", action="store_true")
argument_group.add_argument("-p", "--prod", help="Deploys ShiViz to production environment.", action="store_true")
return parser.parse_args()
def main(args):
'''
Workhorse method to execute all the of the steps described in the file header.
'''
src_dir = "./"
if args.prod:
dist_dir = "../bestchai.bitbucket.org/shiviz/"
elif args.dev:
dist_dir = "../bestchai.bitbucket.org/shiviz-dev/"
print "Deploying to: " + dist_dir
print "from: " + src_dir
# TODO: add a confirmation yes/no dialog, before going ahead with rm.
# Remove previously deployed version of shiviz.
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)
do_minification = raw_input("Do you want to minify the code? (Y/N) ") == "Y"
if do_minification:
# Check if google-closure-compiler is installed.
if (runcmd("google-closure-compiler --version") != 0):
print("You need to install google-closure-compiler for minification.")
print("The easiest way to install the compiler is with NPM or Yarn.")
continue_without_modification = raw_input("Do you want to continue without modification? (Y/N)") == "Y"
if not continue_without_modification:
sys.exit(-1)
do_minification = False
# Copy over the source.
if (os.path.exists(src_dir)):
runcmd("cp -R " + src_dir + "* " + dist_dir)
if do_minification:
# 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:
# hg:
# revid = get_cmd_output('hg', ['id', '-i']);
# git:
revid = get_cmd_output('git', ['rev-parse', '--short', 'HEAD']);
revid = revid.rstrip()
# Find out the current branch:
# hg:
# branch = get_cmd_output('hg', ['branch']);
# git:
branch = get_cmd_output('git', ['rev-parse', '--abbrev-ref', 'HEAD']);
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")
if do_minification:
# Minify the code
print "Minifying... please wait"
minify(revid)
minified_size = os.path.getsize('js/min.js')
print "Minified size: %i" % minified_size
if minified_size < 500:
print "Minification failed!"
return
print "Minification successful!"
# 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")
else:
# Replace dev.js import in index.html with deployed.js import
runcmd("sed -i '' -e 's/\"js\/dev.js\"/\"js\/deployed.js\"/g' " + dist_dir + "index.html")
# Change the contents of deployed.js to include correct hg revision id
runcmd("sed -i '' -e 's/revision: ZZZ/revision: " + revid + "/g' " + dist_dir + "/js/deployed.js")
# Add any files that are new and remove any files that no longer exist
# hg:
# runcmd("cd " + dist_dir + " && hg addremove")
# git:
runcmd("cd " + dist_dir + " && git add -A")
# Commit the deployed dir.
runcmd("cd " + dist_dir + " && git commit -m 'shiviz auto-deployment'")
# Push the deployed dir.
runcmd("cd " + dist_dir + " && git push")
print
print "Done."
return
if __name__ == "__main__":
args = parse_args()
main(args)