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