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