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