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