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