##// END OF EJS Templates
Remove incorrect type=int conversion for commandline usage
codespaced -
Show More
@@ -1,316 +1,315 b''
1 #!/usr/bin/python
1 #!/usr/bin/python
2 """Utility function for installing MathJax javascript library into
2 """Utility function for installing MathJax javascript library into
3 the notebook's 'static' directory, for offline use.
3 the notebook's 'static' directory, for offline use.
4
4
5 Authors:
5 Authors:
6
6
7 * Min RK
7 * Min RK
8 * Mark Sienkiewicz
8 * Mark Sienkiewicz
9 * Matthias Bussonnier
9 * Matthias Bussonnier
10
10
11 To download and install MathJax:
11 To download and install MathJax:
12
12
13 From Python:
13 From Python:
14
14
15 >>> from IPython.external.mathjax import install_mathjax
15 >>> from IPython.external.mathjax import install_mathjax
16 >>> install_mathjax()
16 >>> install_mathjax()
17
17
18 From the command line:
18 From the command line:
19
19
20 $ python -m IPython.external.mathjax
20 $ python -m IPython.external.mathjax
21
21
22 To install MathJax from a file you have already downloaded:
22 To install MathJax from a file you have already downloaded:
23
23
24 $ python -m IPython.external.mathjax mathjax-xxx.tar.gz
24 $ python -m IPython.external.mathjax mathjax-xxx.tar.gz
25 $ python -m IPython.external.mathjax mathjax-xxx.zip
25 $ python -m IPython.external.mathjax mathjax-xxx.zip
26
26
27 It will not install MathJax if it is already there. Use -r to
27 It will not install MathJax if it is already there. Use -r to
28 replace the existing copy of MathJax.
28 replace the existing copy of MathJax.
29
29
30 To find the directory where IPython would like MathJax installed:
30 To find the directory where IPython would like MathJax installed:
31
31
32 $ python -m IPython.external.mathjax -d
32 $ python -m IPython.external.mathjax -d
33
33
34 """
34 """
35
35
36
36
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38 # Copyright (C) 2008-2011 The IPython Development Team
38 # Copyright (C) 2008-2011 The IPython Development Team
39 #
39 #
40 # Distributed under the terms of the BSD License. The full license is in
40 # Distributed under the terms of the BSD License. The full license is in
41 # the file COPYING, distributed as part of this software.
41 # the file COPYING, distributed as part of this software.
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44
44
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46 # Imports
46 # Imports
47 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
48
48
49 import os
49 import os
50 import shutil
50 import shutil
51 import sys
51 import sys
52 import tarfile
52 import tarfile
53 import urllib2
53 import urllib2
54 import zipfile
54 import zipfile
55
55
56
56
57 from IPython.utils.path import locate_profile
57 from IPython.utils.path import locate_profile
58 from IPython.external import argparse
58 from IPython.external import argparse
59 #-----------------------------------------------------------------------------
59 #-----------------------------------------------------------------------------
60 #
60 #
61 #-----------------------------------------------------------------------------
61 #-----------------------------------------------------------------------------
62
62
63 # Where mathjax will be installed.
63 # Where mathjax will be installed.
64
64
65 static = os.path.join(locate_profile('default'), 'static')
65 static = os.path.join(locate_profile('default'), 'static')
66 default_dest = os.path.join(static, 'mathjax')
66 default_dest = os.path.join(static, 'mathjax')
67
67
68 ##
68 ##
69
69
70 # Test for access to install mathjax.
70 # Test for access to install mathjax.
71
71
72 def check_perms(dest, replace=False):
72 def check_perms(dest, replace=False):
73 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
73 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
74 components = dest.split(os.path.sep)
74 components = dest.split(os.path.sep)
75 subpaths = [ os.path.sep+os.path.sep.join(components[1:i]) for i in range(1,len(components))]
75 subpaths = [ os.path.sep+os.path.sep.join(components[1:i]) for i in range(1,len(components))]
76
76
77 existing_path = filter(os.path.exists, subpaths)
77 existing_path = filter(os.path.exists, subpaths)
78 last_writable = existing_path[-1]
78 last_writable = existing_path[-1]
79 if not os.access(last_writable, os.W_OK):
79 if not os.access(last_writable, os.W_OK):
80 raise IOError("Need have write access to %s" % parent)
80 raise IOError("Need have write access to %s" % parent)
81 not_existing = [ path for path in subpaths if path not in existing_path]
81 not_existing = [ path for path in subpaths if path not in existing_path]
82 # subfolder we will create, will obviously be writable
82 # subfolder we will create, will obviously be writable
83 # should we still considere checking separately that
83 # should we still considere checking separately that
84 # ipython profiles have been created ?
84 # ipython profiles have been created ?
85 for folder in not_existing:
85 for folder in not_existing:
86 os.mkdir(folder)
86 os.mkdir(folder)
87
87
88 if os.path.exists(dest):
88 if os.path.exists(dest):
89 if replace:
89 if replace:
90 if not os.access(dest, os.W_OK):
90 if not os.access(dest, os.W_OK):
91 raise IOError("Need have write access to %s" % dest)
91 raise IOError("Need have write access to %s" % dest)
92 print "removing previous MathJax install"
92 print "removing previous MathJax install"
93 shutil.rmtree(dest)
93 shutil.rmtree(dest)
94 return True
94 return True
95 else:
95 else:
96 print "offline MathJax apparently already installed"
96 print "offline MathJax apparently already installed"
97 return False
97 return False
98 else :
98 else :
99 return True
99 return True
100
100
101 ##
101 ##
102
102
103 def extract_tar( fd, dest ) :
103 def extract_tar( fd, dest ) :
104 # use 'r|gz' stream mode, because socket file-like objects can't seek:
104 # use 'r|gz' stream mode, because socket file-like objects can't seek:
105 tar = tarfile.open(fileobj=fd, mode='r|gz')
105 tar = tarfile.open(fileobj=fd, mode='r|gz')
106
106
107 # we just happen to know that the first entry in the mathjax
107 # we just happen to know that the first entry in the mathjax
108 # archive is the directory that the remaining members are in.
108 # archive is the directory that the remaining members are in.
109 topdir = tar.firstmember.path
109 topdir = tar.firstmember.path
110
110
111 # extract the archive (contains a single directory) to the static/ directory
111 # extract the archive (contains a single directory) to the static/ directory
112 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
112 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
113 tar.extractall(parent)
113 tar.extractall(parent)
114
114
115 # it will be mathjax-MathJax-<sha>, rename to just mathjax
115 # it will be mathjax-MathJax-<sha>, rename to just mathjax
116 os.rename(os.path.join(parent, topdir), dest)
116 os.rename(os.path.join(parent, topdir), dest)
117
117
118 ##
118 ##
119
119
120 def extract_zip( fd, dest ) :
120 def extract_zip( fd, dest ) :
121 z = zipfile.ZipFile( fd, 'r' )
121 z = zipfile.ZipFile( fd, 'r' )
122
122
123 # we just happen to know that the first entry in the mathjax
123 # we just happen to know that the first entry in the mathjax
124 # archive is the directory that the remaining members are in.
124 # archive is the directory that the remaining members are in.
125 topdir = z.namelist()[0]
125 topdir = z.namelist()[0]
126
126
127 # extract the archive (contains a single directory) to the static/ directory
127 # extract the archive (contains a single directory) to the static/ directory
128 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
128 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
129 z.extractall( parent )
129 z.extractall( parent )
130
130
131 # it will be mathjax-MathJax-<sha>, rename to just mathjax
131 # it will be mathjax-MathJax-<sha>, rename to just mathjax
132 d = os.path.join(parent, topdir)
132 d = os.path.join(parent, topdir)
133 print d
133 print d
134 os.rename(os.path.join(parent, topdir), dest)
134 os.rename(os.path.join(parent, topdir), dest)
135
135
136 ##
136 ##
137
137
138 def install_mathjax(tag='v2.0', dest=default_dest, replace=False, file=None, extractor=extract_tar ):
138 def install_mathjax(tag='v2.0', dest=default_dest, replace=False, file=None, extractor=extract_tar ):
139 """Download and/or install MathJax for offline use.
139 """Download and/or install MathJax for offline use.
140
140
141 This will install mathjax to the 'static' dir in the IPython notebook
141 This will install mathjax to the 'static' dir in the IPython notebook
142 package, so it will fail if the caller does not have write access
142 package, so it will fail if the caller does not have write access
143 to that location.
143 to that location.
144
144
145 MathJax is a ~15MB download, and ~150MB installed.
145 MathJax is a ~15MB download, and ~150MB installed.
146
146
147 Parameters
147 Parameters
148 ----------
148 ----------
149
149
150 replace : bool [False]
150 replace : bool [False]
151 Whether to remove and replace an existing install.
151 Whether to remove and replace an existing install.
152 dest : str [path to default profile]
152 dest : str [path to default profile]
153 Where to locally install mathjax
153 Where to locally install mathjax
154 tag : str ['v2.0']
154 tag : str ['v2.0']
155 Which tag to download. Default is 'v2.0', the current stable release,
155 Which tag to download. Default is 'v2.0', the current stable release,
156 but alternatives include 'v1.1a' and 'master'.
156 but alternatives include 'v1.1a' and 'master'.
157 file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}]
157 file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}]
158 File handle from which to untar/unzip/... mathjax
158 File handle from which to untar/unzip/... mathjax
159 extractor : function
159 extractor : function
160 Method tu use to untar/unzip/... `file`
160 Method tu use to untar/unzip/... `file`
161 """
161 """
162 if not check_perms(dest, replace) :
162 if not check_perms(dest, replace) :
163 return
163 return
164
164
165 if file is None :
165 if file is None :
166 # download mathjax
166 # download mathjax
167 mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s" % tag
167 mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s" % tag
168 print "Downloading mathjax source from %s" % mathjax_url
168 print "Downloading mathjax source from %s" % mathjax_url
169 response = urllib2.urlopen(mathjax_url)
169 response = urllib2.urlopen(mathjax_url)
170 file = response.fp
170 file = response.fp
171
171
172 print "Extracting to %s" % dest
172 print "Extracting to %s" % dest
173 extractor( file, dest )
173 extractor( file, dest )
174
174
175 ##
175 ##
176
176
177 def test_func( remove, dest) :
177 def test_func( remove, dest) :
178 """See if mathjax appears to be installed correctly"""
178 """See if mathjax appears to be installed correctly"""
179 status = 0
179 status = 0
180 if not os.path.isdir( dest ) :
180 if not os.path.isdir( dest ) :
181 print "%s directory not found" % dest
181 print "%s directory not found" % dest
182 status = 1
182 status = 1
183 if not os.path.exists( dest + "/MathJax.js" ) :
183 if not os.path.exists( dest + "/MathJax.js" ) :
184 print "MathJax.js not present in %s" % dest
184 print "MathJax.js not present in %s" % dest
185 status = 1
185 status = 1
186 print "ok"
186 print "ok"
187 if remove and os.path.exists(dest):
187 if remove and os.path.exists(dest):
188 shutil.rmtree( dest )
188 shutil.rmtree( dest )
189 return status
189 return status
190
190
191 ##
191 ##
192
192
193 def main() :
193 def main() :
194 # This main is just simple enough that it is not worth the
194 # This main is just simple enough that it is not worth the
195 # complexity of argparse
195 # complexity of argparse
196
196
197 # What directory is mathjax in?
197 # What directory is mathjax in?
198 parser = argparse.ArgumentParser(
198 parser = argparse.ArgumentParser(
199 description="""Install mathjax from internet or local archive""",
199 description="""Install mathjax from internet or local archive""",
200 )
200 )
201
201
202 parser.add_argument(
202 parser.add_argument(
203 '-i',
203 '-i',
204 '--install-dir',
204 '--install-dir',
205 default=default_dest,
205 default=default_dest,
206 help='installation directory (by default : %s)' % (default_dest))
206 help='installation directory (by default : %s)' % (default_dest))
207 parser.add_argument(
207 parser.add_argument(
208 '-d',
208 '-d',
209 '--dest',
209 '--dest',
210 action='store_true',
210 action='store_true',
211 help='print where is current mathjax would be installed and exit')
211 help='print where is current mathjax would be installed and exit')
212 parser.add_argument(
212 parser.add_argument(
213 '-r',
213 '-r',
214 '--replace',
214 '--replace',
215 action='store_true',
215 action='store_true',
216 help='Wether to replace current mathjax if already exist')
216 help='Wether to replace current mathjax if already exist')
217 parser.add_argument(
217 parser.add_argument(
218 '-t',
218 '-t',
219 '--test',
219 '--test',
220 action='store_true')
220 action='store_true')
221 parser.add_argument('tarball',
221 parser.add_argument('tarball',
222 type=int,
223 help="the local tar/zip-ball containing mathjax",
222 help="the local tar/zip-ball containing mathjax",
224 nargs='?',
223 nargs='?',
225 metavar='tarball')
224 metavar='tarball')
226
225
227 pargs = parser.parse_args()
226 pargs = parser.parse_args()
228
227
229 dest = pargs.install_dir
228 dest = pargs.install_dir
230 if pargs.dest :
229 if pargs.dest :
231 print dest
230 print dest
232 return
231 return
233
232
234 # remove/replace existing mathjax?
233 # remove/replace existing mathjax?
235 if pargs.replace :
234 if pargs.replace :
236 replace = True
235 replace = True
237 else :
236 else :
238 replace = False
237 replace = False
239
238
240 # undocumented test interface
239 # undocumented test interface
241 if pargs.test :
240 if pargs.test :
242 return test_func( replace, dest)
241 return test_func( replace, dest)
243
242
244 # do it
243 # do it
245 if pargs.tarball :
244 if pargs.tarball :
246 fname = pargs.tarball
245 fname = pargs.tarball
247
246
248 # automatically detect zip/tar - could do something based
247 # automatically detect zip/tar - could do something based
249 # on file content, but really not cost-effective here.
248 # on file content, but really not cost-effective here.
250 if fname.endswith('.zip') :
249 if fname.endswith('.zip') :
251 extractor = extract_zip
250 extractor = extract_zip
252 else :
251 else :
253 extractor = extract_tar
252 extractor = extract_tar
254 # do it
253 # do it
255 install_mathjax(file=open(fname, "r"), replace=replace, extractor=extractor, dest=dest )
254 install_mathjax(file=open(fname, "r"), replace=replace, extractor=extractor, dest=dest )
256 else:
255 else:
257 install_mathjax(replace=replace, dest=dest)
256 install_mathjax(replace=replace, dest=dest)
258
257
259
258
260 if __name__ == '__main__' :
259 if __name__ == '__main__' :
261 sys.exit(main())
260 sys.exit(main())
262
261
263 __all__ = ['install_mathjax', 'main', 'dest']
262 __all__ = ['install_mathjax', 'main', 'dest']
264
263
265 """
264 """
266 Test notes:
265 Test notes:
267
266
268 IPython uses IPython.testing.iptest as a custom test controller
267 IPython uses IPython.testing.iptest as a custom test controller
269 (though it is based on nose). It might be possible to fit automatic
268 (though it is based on nose). It might be possible to fit automatic
270 tests of installation into that framework, but it looks awkward to me.
269 tests of installation into that framework, but it looks awkward to me.
271 So, here is a manual procedure for testing this automatic installer.
270 So, here is a manual procedure for testing this automatic installer.
272
271
273 Mark Sienkiewicz, 2012-08-06
272 Mark Sienkiewicz, 2012-08-06
274 first 8 letters of my last name @ stsci.edu
273 first 8 letters of my last name @ stsci.edu
275
274
276 # remove mathjax from the installed ipython instance
275 # remove mathjax from the installed ipython instance
277 # IOError ok if mathjax was never installed yet.
276 # IOError ok if mathjax was never installed yet.
278
277
279 python -m IPython.external.mathjax --test -r
278 python -m IPython.external.mathjax --test -r
280
279
281 # download and install mathjax from command line:
280 # download and install mathjax from command line:
282
281
283 python -m IPython.external.mathjax
282 python -m IPython.external.mathjax
284 python -m IPython.external.mathjax --test -r
283 python -m IPython.external.mathjax --test -r
285
284
286 # download and install from within python
285 # download and install from within python
287
286
288 python -c "from IPython.external.mathjax import install_mathjax; install_mathjax()"
287 python -c "from IPython.external.mathjax import install_mathjax; install_mathjax()"
289 python -m IPython.external.mathjax --test -r
288 python -m IPython.external.mathjax --test -r
290
289
291 # view http://www.mathjax.org/download/ in your browser
290 # view http://www.mathjax.org/download/ in your browser
292 # save-as the link for MathJax-2.0 near the bottom of the page.
291 # save-as the link for MathJax-2.0 near the bottom of the page.
293 # The file it offers is mathjax-MathJax-v2.0-20-g07669ac.zip
292 # The file it offers is mathjax-MathJax-v2.0-20-g07669ac.zip
294
293
295 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.zip
294 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.zip
296 python -m IPython.external.mathjax --test -r
295 python -m IPython.external.mathjax --test -r
297
296
298 # download https://github.com/mathjax/MathJax/tarball/v2.0 in your browser
297 # download https://github.com/mathjax/MathJax/tarball/v2.0 in your browser
299 # (this is the url used internally by install_mathjax)
298 # (this is the url used internally by install_mathjax)
300 # The file it offers is mathjax-MathJax-v2.0-20-g07669ac.tar.gz
299 # The file it offers is mathjax-MathJax-v2.0-20-g07669ac.tar.gz
301
300
302 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
301 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
303
302
304 python -m IPython.external.mathjax --test
303 python -m IPython.external.mathjax --test
305 # note no -r
304 # note no -r
306
305
307 # install it again while it is already there
306 # install it again while it is already there
308
307
309 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
308 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
310 # says "offline MathJax apparently already installed"
309 # says "offline MathJax apparently already installed"
311
310
312 python -m IPython.external.mathjax ~/mathjax-MathJax-v2.0-20-g07669ac.tar.gz
311 python -m IPython.external.mathjax ~/mathjax-MathJax-v2.0-20-g07669ac.tar.gz
313 python -m IPython.external.mathjax --test
312 python -m IPython.external.mathjax --test
314
313
315
314
316 """
315 """
General Comments 0
You need to be logged in to leave comments. Login now