##// END OF EJS Templates
removing unused line...
CJ Carey -
Show More
@@ -1,238 +1,237 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 your IPython nbextensions directory, for offline use.
3 your IPython nbextensions 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 a specific location:
22 To a specific location:
23
23
24 $ python -m IPython.external.mathjax -i /usr/share/
24 $ python -m IPython.external.mathjax -i /usr/share/
25
25
26 will install mathjax to /usr/share/mathjax
26 will install mathjax to /usr/share/mathjax
27
27
28 To install MathJax from a file you have already downloaded:
28 To install MathJax from a file you have already downloaded:
29
29
30 $ python -m IPython.external.mathjax mathjax-xxx.tar.gz
30 $ python -m IPython.external.mathjax mathjax-xxx.tar.gz
31 $ python -m IPython.external.mathjax mathjax-xxx.zip
31 $ python -m IPython.external.mathjax mathjax-xxx.zip
32
32
33 It will not install MathJax if it is already there. Use -r to
33 It will not install MathJax if it is already there. Use -r to
34 replace the existing copy of MathJax.
34 replace the existing copy of MathJax.
35
35
36 To find the directory where IPython would like MathJax installed:
36 To find the directory where IPython would like MathJax installed:
37
37
38 $ python -m IPython.external.mathjax -d
38 $ python -m IPython.external.mathjax -d
39
39
40 """
40 """
41 from __future__ import print_function
41 from __future__ import print_function
42
42
43
43
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45 # Copyright (C) 2011 The IPython Development Team
45 # Copyright (C) 2011 The IPython Development Team
46 #
46 #
47 # Distributed under the terms of the BSD License. The full license is in
47 # Distributed under the terms of the BSD License. The full license is in
48 # the file COPYING, distributed as part of this software.
48 # the file COPYING, distributed as part of this software.
49 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
50
50
51
51
52 #-----------------------------------------------------------------------------
52 #-----------------------------------------------------------------------------
53 # Imports
53 # Imports
54 #-----------------------------------------------------------------------------
54 #-----------------------------------------------------------------------------
55
55
56 import argparse
56 import argparse
57 import os
57 import os
58 import shutil
58 import shutil
59 import sys
59 import sys
60 import tarfile
60 import tarfile
61 import zipfile
61 import zipfile
62
62
63 from IPython.utils.path import get_ipython_dir
63 from IPython.utils.path import get_ipython_dir
64
64
65 try:
65 try:
66 from urllib.request import urlopen # Py 3
66 from urllib.request import urlopen # Py 3
67 except ImportError:
67 except ImportError:
68 from urllib2 import urlopen
68 from urllib2 import urlopen
69
69
70 #-----------------------------------------------------------------------------
70 #-----------------------------------------------------------------------------
71 #
71 #
72 #-----------------------------------------------------------------------------
72 #-----------------------------------------------------------------------------
73
73
74 # Where mathjax will be installed
74 # Where mathjax will be installed
75
75
76 nbextensions = os.path.join(get_ipython_dir(), 'nbextensions')
76 nbextensions = os.path.join(get_ipython_dir(), 'nbextensions')
77 default_dest = os.path.join(nbextensions, 'mathjax')
77 default_dest = os.path.join(nbextensions, 'mathjax')
78
78
79 # Test for access to install mathjax
79 # Test for access to install mathjax
80
80
81 def prepare_dest(dest, replace=False):
81 def prepare_dest(dest, replace=False):
82 """prepare the destination folder for mathjax install
82 """prepare the destination folder for mathjax install
83
83
84 Returns False if mathjax appears to already be installed and there is nothing to do,
84 Returns False if mathjax appears to already be installed and there is nothing to do,
85 True otherwise.
85 True otherwise.
86 """
86 """
87
87
88 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
88 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
89 if not os.path.exists(parent):
89 if not os.path.exists(parent):
90 os.makedirs(parent)
90 os.makedirs(parent)
91
91
92 if os.path.exists(dest):
92 if os.path.exists(dest):
93 if replace:
93 if replace:
94 print("removing existing MathJax at %s" % dest)
94 print("removing existing MathJax at %s" % dest)
95 shutil.rmtree(dest)
95 shutil.rmtree(dest)
96 return True
96 return True
97 else:
97 else:
98 mathjax_js = os.path.join(dest, 'MathJax.js')
98 mathjax_js = os.path.join(dest, 'MathJax.js')
99 if not os.path.exists(mathjax_js):
99 if not os.path.exists(mathjax_js):
100 raise IOError("%s exists, but does not contain MathJax.js" % dest)
100 raise IOError("%s exists, but does not contain MathJax.js" % dest)
101 print("%s already exists" % mathjax_js)
101 print("%s already exists" % mathjax_js)
102 return False
102 return False
103 else:
103 else:
104 return True
104 return True
105
105
106
106
107 def extract_tar(fd, dest):
107 def extract_tar(fd, dest):
108 """extract a tarball from filelike `fd` to destination `dest`"""
108 """extract a tarball from filelike `fd` to destination `dest`"""
109 # use 'r|gz' stream mode, because socket file-like objects can't seek:
109 # use 'r|gz' stream mode, because socket file-like objects can't seek:
110 tar = tarfile.open(fileobj=fd, mode='r|gz')
110 tar = tarfile.open(fileobj=fd, mode='r|gz')
111
111
112 # The first entry in the archive is the top-level dir
112 # The first entry in the archive is the top-level dir
113 topdir = tar.firstmember.path
113 topdir = tar.firstmember.path
114
114
115 # extract the archive (contains a single directory) to the destination directory
115 # extract the archive (contains a single directory) to the destination directory
116 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
116 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
117 tar.extractall(parent)
117 tar.extractall(parent)
118
118
119 # it will be mathjax-MathJax-<sha>, rename to just mathjax
119 # it will be mathjax-MathJax-<sha>, rename to just mathjax
120 os.rename(os.path.join(parent, topdir), dest)
120 os.rename(os.path.join(parent, topdir), dest)
121
121
122
122
123 def extract_zip(fd, dest):
123 def extract_zip(fd, dest):
124 """extract a zip file from filelike `fd` to destination `dest`"""
124 """extract a zip file from filelike `fd` to destination `dest`"""
125 z = zipfile.ZipFile(fd, 'r')
125 z = zipfile.ZipFile(fd, 'r')
126
126
127 # The first entry in the archive is the top-level dir
127 # The first entry in the archive is the top-level dir
128 topdir = z.namelist()[0]
128 topdir = z.namelist()[0]
129
129
130 # extract the archive (contains a single directory) to the static/ directory
130 # extract the archive (contains a single directory) to the static/ directory
131 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
131 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
132 z.extractall(parent)
132 z.extractall(parent)
133
133
134 # it will be mathjax-MathJax-<sha>, rename to just mathjax
134 # it will be mathjax-MathJax-<sha>, rename to just mathjax
135 d = os.path.join(parent, topdir)
136 os.rename(os.path.join(parent, topdir), dest)
135 os.rename(os.path.join(parent, topdir), dest)
137
136
138
137
139 def install_mathjax(tag='2.4.0', dest=default_dest, replace=False, file=None, extractor=extract_tar):
138 def install_mathjax(tag='2.4.0', dest=default_dest, replace=False, file=None, extractor=extract_tar):
140 """Download and/or install MathJax for offline use.
139 """Download and/or install MathJax for offline use.
141
140
142 This will install mathjax to the nbextensions dir in your IPYTHONDIR.
141 This will install mathjax to the nbextensions dir in your IPYTHONDIR.
143
142
144 MathJax is a ~15MB download, and ~150MB installed.
143 MathJax is a ~15MB download, and ~150MB installed.
145
144
146 Parameters
145 Parameters
147 ----------
146 ----------
148
147
149 replace : bool [False]
148 replace : bool [False]
150 Whether to remove and replace an existing install.
149 Whether to remove and replace an existing install.
151 dest : str [IPYTHONDIR/nbextensions/mathjax]
150 dest : str [IPYTHONDIR/nbextensions/mathjax]
152 Where to install mathjax
151 Where to install mathjax
153 tag : str ['2.4.0']
152 tag : str ['2.4.0']
154 Which tag to download. Default is '2.4.0', the current stable release,
153 Which tag to download. Default is '2.4.0', the current stable release,
155 but alternatives include 'v1.1a' and 'master'.
154 but alternatives include 'v1.1a' and 'master'.
156 file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}]
155 file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}]
157 File handle from which to untar/unzip/... mathjax
156 File handle from which to untar/unzip/... mathjax
158 extractor : function
157 extractor : function
159 Method to use to untar/unzip/... `file`
158 Method to use to untar/unzip/... `file`
160 """
159 """
161 try:
160 try:
162 anything_to_do = prepare_dest(dest, replace)
161 anything_to_do = prepare_dest(dest, replace)
163 except OSError as e:
162 except OSError as e:
164 print("ERROR %s, require write access to %s" % (e, dest))
163 print("ERROR %s, require write access to %s" % (e, dest))
165 return 1
164 return 1
166 else:
165 else:
167 if not anything_to_do:
166 if not anything_to_do:
168 return 0
167 return 0
169
168
170 if file is None:
169 if file is None:
171 # download mathjax
170 # download mathjax
172 mathjax_url = "https://github.com/mathjax/MathJax/archive/%s.tar.gz" %tag
171 mathjax_url = "https://github.com/mathjax/MathJax/archive/%s.tar.gz" %tag
173 print("Downloading mathjax source from %s" % mathjax_url)
172 print("Downloading mathjax source from %s" % mathjax_url)
174 response = urlopen(mathjax_url)
173 response = urlopen(mathjax_url)
175 file = response.fp
174 file = response.fp
176
175
177 print("Extracting to %s" % dest)
176 print("Extracting to %s" % dest)
178 extractor(file, dest)
177 extractor(file, dest)
179 return 0
178 return 0
180
179
181
180
182 def main():
181 def main():
183 parser = argparse.ArgumentParser(
182 parser = argparse.ArgumentParser(
184 description="""Install mathjax from internet or local archive""",
183 description="""Install mathjax from internet or local archive""",
185 )
184 )
186
185
187 parser.add_argument(
186 parser.add_argument(
188 '-i',
187 '-i',
189 '--install-dir',
188 '--install-dir',
190 default=nbextensions,
189 default=nbextensions,
191 help='custom installation directory. Mathjax will be installed in here/mathjax')
190 help='custom installation directory. Mathjax will be installed in here/mathjax')
192
191
193 parser.add_argument(
192 parser.add_argument(
194 '-d',
193 '-d',
195 '--print-dest',
194 '--print-dest',
196 action='store_true',
195 action='store_true',
197 help='print where mathjax would be installed and exit')
196 help='print where mathjax would be installed and exit')
198 parser.add_argument(
197 parser.add_argument(
199 '-r',
198 '-r',
200 '--replace',
199 '--replace',
201 action='store_true',
200 action='store_true',
202 help='Whether to replace current mathjax if it already exists')
201 help='Whether to replace current mathjax if it already exists')
203 parser.add_argument('filename',
202 parser.add_argument('filename',
204 help="the local tar/zip-ball filename containing mathjax",
203 help="the local tar/zip-ball filename containing mathjax",
205 nargs='?',
204 nargs='?',
206 metavar='filename')
205 metavar='filename')
207
206
208 pargs = parser.parse_args()
207 pargs = parser.parse_args()
209
208
210 dest = os.path.join(pargs.install_dir, 'mathjax')
209 dest = os.path.join(pargs.install_dir, 'mathjax')
211
210
212 if pargs.print_dest:
211 if pargs.print_dest:
213 print(dest)
212 print(dest)
214 return
213 return
215
214
216 # remove/replace existing mathjax?
215 # remove/replace existing mathjax?
217 replace = pargs.replace
216 replace = pargs.replace
218
217
219 # do it
218 # do it
220 if pargs.filename:
219 if pargs.filename:
221 fname = pargs.filename
220 fname = pargs.filename
222
221
223 # automatically detect zip/tar - could do something based
222 # automatically detect zip/tar - could do something based
224 # on file content, but really not cost-effective here.
223 # on file content, but really not cost-effective here.
225 if fname.endswith('.zip'):
224 if fname.endswith('.zip'):
226 extractor = extract_zip
225 extractor = extract_zip
227 else :
226 else :
228 extractor = extract_tar
227 extractor = extract_tar
229 # do it
228 # do it
230 return install_mathjax(file=open(fname, "rb"), replace=replace, extractor=extractor, dest=dest)
229 return install_mathjax(file=open(fname, "rb"), replace=replace, extractor=extractor, dest=dest)
231 else:
230 else:
232 return install_mathjax(replace=replace, dest=dest)
231 return install_mathjax(replace=replace, dest=dest)
233
232
234
233
235 if __name__ == '__main__' :
234 if __name__ == '__main__' :
236 sys.exit(main())
235 sys.exit(main())
237
236
238 __all__ = ['install_mathjax', 'main', 'default_dest']
237 __all__ = ['install_mathjax', 'main', 'default_dest']
General Comments 0
You need to be logged in to leave comments. Login now