##// END OF EJS Templates
use argparse to make most of the parameters configurable
Matthias BUSSONNIER -
Show More
@@ -1,3 +1,4 b''
1 #!/usr/bin/python
1 """Utility function for installing MathJax javascript library into
2 """Utility function for installing MathJax javascript library into
2 the notebook's 'static' directory, for offline use.
3 the notebook's 'static' directory, for offline use.
3
4
@@ -32,6 +33,7 b' To find the directory where IPython would like MathJax installed:'
32
33
33 """
34 """
34
35
36
35 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
36 # Copyright (C) 2008-2011 The IPython Development Team
38 # Copyright (C) 2008-2011 The IPython Development Team
37 #
39 #
@@ -51,22 +53,26 b' import tarfile'
51 import urllib2
53 import urllib2
52 import zipfile
54 import zipfile
53
55
56
54 from IPython.utils.path import locate_profile
57 from IPython.utils.path import locate_profile
58 from IPython.external import argparse
55 #-----------------------------------------------------------------------------
59 #-----------------------------------------------------------------------------
56 #
60 #
57 #-----------------------------------------------------------------------------
61 #-----------------------------------------------------------------------------
58
62
59 # Where mathjax will be installed.
63 # Where mathjax will be installed.
60
64
61 default_dest = os.path.join(locate_profile('default'), 'static')
65 static = os.path.join(locate_profile('default'), 'static')
66 default_dest = os.path.join(static, 'mathjax')
62
67
63 ##
68 ##
64
69
65 # Test for access to install mathjax.
70 # Test for access to install mathjax.
66
71
67 def check_perms(replace=False, dest=default_dest):
72 def check_perms(dest, replace=False):
68 if not os.access(static, os.W_OK):
73 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
69 raise IOError("Need have write access to %s" % static)
74 if not os.access(parent, os.W_OK):
75 raise IOError("Need have write access to %s" % parent)
70 if os.path.exists(dest):
76 if os.path.exists(dest):
71 if replace:
77 if replace:
72 if not os.access(dest, os.W_OK):
78 if not os.access(dest, os.W_OK):
@@ -91,10 +97,11 b' def extract_tar( fd, dest ) :'
91 topdir = tar.firstmember.path
97 topdir = tar.firstmember.path
92
98
93 # extract the archive (contains a single directory) to the static/ directory
99 # extract the archive (contains a single directory) to the static/ directory
94 tar.extractall(static)
100 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
101 tar.extractall(parent)
95
102
96 # it will be mathjax-MathJax-<sha>, rename to just mathjax
103 # it will be mathjax-MathJax-<sha>, rename to just mathjax
97 os.rename(os.path.join(static, topdir), dest)
104 os.rename(os.path.join(parent, topdir), dest)
98
105
99 ##
106 ##
100
107
@@ -106,12 +113,13 b' def extract_zip( fd, dest ) :'
106 topdir = z.namelist()[0]
113 topdir = z.namelist()[0]
107
114
108 # extract the archive (contains a single directory) to the static/ directory
115 # extract the archive (contains a single directory) to the static/ directory
109 z.extractall( static )
116 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
117 z.extractall( parent )
110
118
111 # it will be mathjax-MathJax-<sha>, rename to just mathjax
119 # it will be mathjax-MathJax-<sha>, rename to just mathjax
112 d = os.path.join(static, topdir)
120 d = os.path.join(parent, topdir)
113 print d
121 print d
114 os.rename(os.path.join(static, topdir), dest)
122 os.rename(os.path.join(parent, topdir), dest)
115
123
116 ##
124 ##
117
125
@@ -139,83 +147,108 b" def install_mathjax(tag='v2.0', dest=default_dest, replace=False, file=None, ext"
139 extractor : function
147 extractor : function
140 Method tu use to untar/unzip/... `file`
148 Method tu use to untar/unzip/... `file`
141 """
149 """
142
150 if not check_perms(dest, replace) :
143 if not check_perms(replace, dest=dest) :
144 return
151 return
145
152
146 if file is None :
153 if file is None :
147 # download mathjax
154 # download mathjax
148 mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s"%tag
155 mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s" % tag
149 print "Downloading mathjax source from %s"%mathjax_url
156 print "Downloading mathjax source from %s" % mathjax_url
150 response = urllib2.urlopen(mathjax_url)
157 response = urllib2.urlopen(mathjax_url)
151 file = response.fp
158 file = response.fp
152
159
153 print "Extracting to %s"%dest
160 print "Extracting to %s" % dest
154 extractor( fd, dest )
161 extractor( file, dest )
155
162
156 ##
163 ##
157
164
158 def test_func( remove, dest=default_dest) :
165 def test_func( remove, dest) :
159 """See if mathjax appears to be installed correctly"""
166 """See if mathjax appears to be installed correctly"""
167 status = 0
160 if not os.path.isdir( dest ) :
168 if not os.path.isdir( dest ) :
161 print "%s directory not found"%dest
169 print "%s directory not found" % dest
162 status=1
170 status = 1
163 if not os.path.exists( dest + "/MathJax.js" ) :
171 if not os.path.exists( dest + "/MathJax.js" ) :
164 print "MathJax.js not present in %s"%dest
172 print "MathJax.js not present in %s" % dest
165 status=1
173 status = 1
166 print "ok"
174 print "ok"
167 if remove :
175 if remove and os.path.exists(dest):
168 shutil.rmtree( dest )
176 shutil.rmtree( dest )
169 return status
177 return status
170
178
171 ##
179 ##
172
180
173 def main( args ) :
181 def main() :
174 # This main is just simple enough that it is not worth the
182 # This main is just simple enough that it is not worth the
175 # complexity of argparse
183 # complexity of argparse
176
184
177 # What directory is mathjax in?
185 # What directory is mathjax in?
178 if '-d' in args :
186 parser = argparse.ArgumentParser(
187 description="""Install mathjax from internet or local archive""",
188 )
189
190 parser.add_argument(
191 '-i',
192 '--install-dir',
193 default=default_dest,
194 help='installation directory (by default : %s)' % (default_dest))
195 parser.add_argument(
196 '-d',
197 '--dest',
198 action='store_true',
199 help='print where is current mathjax would be installed and exit')
200 parser.add_argument(
201 '-r',
202 '--replace',
203 action='store_true',
204 help='Wether to replace current mathjax if already exist')
205 parser.add_argument(
206 '-t',
207 '--test',
208 action='store_true')
209 parser.add_argument('tarball',
210 type=int,
211 help="the local tar/zip-ball containing mathjax",
212 nargs='?',
213 metavar='tarball')
214
215 pargs = parser.parse_args()
216
217 dest = pargs.install_dir
218 if pargs.dest :
179 print dest
219 print dest
180 return
220 return
181
221
182 # help
183 if '-h' in args or '--help' in args :
184 print __doc__
185 return
186
187 # remove/replace existing mathjax?
222 # remove/replace existing mathjax?
188 if '-r' in args :
223 if pargs.replace :
189 replace = True
224 replace = True
190 args.remove('-r')
191 else :
225 else :
192 replace = False
226 replace = False
193
227
194 # undocumented test interface
228 # undocumented test interface
195 if '-test' in args :
229 if pargs.test :
196 return test_func( replace, dest=dest)
230 return test_func( replace, dest)
197
231
198 # do it
232 # do it
199 if len(args) == 0 :
233 if pargs.tarball :
200 # This is compatible with the interface documented in ipython 0.13
234 fname = pargs.tarball
201 install_mathjax( replace=replace )
202 else :
203 fname = args[0]
204
235
205 # automatically detect zip/tar - could do something based
236 # automatically detect zip/tar - could do something based
206 # on file content, but really not cost-effective here.
237 # on file content, but really not cost-effective here.
207 if fname.endswith('.zip') :
238 if fname.endswith('.zip') :
208 extractor = extract_zip
239 extractor = extract_zip
209 else :
240 else :
210 extractor = extract_tar
241 extractor = extract_tar
211
212 # do it
242 # do it
213 install_mathjax(fd=open(args[0],"r"), replace=replace, extractor=extractor )
243 install_mathjax(file=open(fname, "r"), replace=replace, extractor=extractor, dest=dest )
244 else:
245 install_mathjax(replace=replace, dest=dest)
246
214
247
215 if __name__ == '__main__' :
248 if __name__ == '__main__' :
216 sys.exit(main( sys.argv[1:] ))
249 sys.exit(main())
217
250
218 __all__ = ['install_mathjax','main','dest']
251 __all__ = ['install_mathjax', 'main', 'dest']
219
252
220 """
253 """
221 Test notes:
254 Test notes:
@@ -231,24 +264,24 b' So, here is a manual procedure for testing this automatic installer.'
231 # remove mathjax from the installed ipython instance
264 # remove mathjax from the installed ipython instance
232 # IOError ok if mathjax was never installed yet.
265 # IOError ok if mathjax was never installed yet.
233
266
234 python -m IPython.external.mathjax -test -r
267 python -m IPython.external.mathjax --test -r
235
268
236 # download and install mathjax from command line:
269 # download and install mathjax from command line:
237
270
238 python -m IPython.external.mathjax
271 python -m IPython.external.mathjax
239 python -m IPython.external.mathjax -test -r
272 python -m IPython.external.mathjax --test -r
240
273
241 # download and install from within python
274 # download and install from within python
242
275
243 python -c "from IPython.external.mathjax import install_mathjax; install_mathjax()"
276 python -c "from IPython.external.mathjax import install_mathjax; install_mathjax()"
244 python -m IPython.external.mathjax -test -r
277 python -m IPython.external.mathjax --test -r
245
278
246 # view http://www.mathjax.org/download/ in your browser
279 # view http://www.mathjax.org/download/ in your browser
247 # save-as the link for MathJax-2.0 near the bottom of the page.
280 # save-as the link for MathJax-2.0 near the bottom of the page.
248 # The file it offers is mathjax-MathJax-v2.0-20-g07669ac.zip
281 # The file it offers is mathjax-MathJax-v2.0-20-g07669ac.zip
249
282
250 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.zip
283 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.zip
251 python -m IPython.external.mathjax -test -r
284 python -m IPython.external.mathjax --test -r
252
285
253 # download https://github.com/mathjax/MathJax/tarball/v2.0 in your browser
286 # download https://github.com/mathjax/MathJax/tarball/v2.0 in your browser
254 # (this is the url used internally by install_mathjax)
287 # (this is the url used internally by install_mathjax)
@@ -256,7 +289,7 b' python -m IPython.external.mathjax -test -r'
256
289
257 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
290 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
258
291
259 python -m IPython.external.mathjax -test
292 python -m IPython.external.mathjax --test
260 # note no -r
293 # note no -r
261
294
262 # install it again while it is already there
295 # install it again while it is already there
@@ -265,7 +298,7 b' python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz'
265 # says "offline MathJax apparently already installed"
298 # says "offline MathJax apparently already installed"
266
299
267 python -m IPython.external.mathjax ~/mathjax-MathJax-v2.0-20-g07669ac.tar.gz
300 python -m IPython.external.mathjax ~/mathjax-MathJax-v2.0-20-g07669ac.tar.gz
268 python -m IPython.external.mathjax -test
301 python -m IPython.external.mathjax --test
269
302
270
303
271 """
304 """
General Comments 0
You need to be logged in to leave comments. Login now