##// END OF EJS Templates
use argparse to make most of the parameters configurable
Matthias BUSSONNIER -
Show More
@@ -1,3 +1,4 b''
1 #!/usr/bin/python
1 2 """Utility function for installing MathJax javascript library into
2 3 the notebook's 'static' directory, for offline use.
3 4
@@ -32,6 +33,7 b' To find the directory where IPython would like MathJax installed:'
32 33
33 34 """
34 35
36
35 37 #-----------------------------------------------------------------------------
36 38 # Copyright (C) 2008-2011 The IPython Development Team
37 39 #
@@ -51,22 +53,26 b' import tarfile'
51 53 import urllib2
52 54 import zipfile
53 55
56
54 57 from IPython.utils.path import locate_profile
58 from IPython.external import argparse
55 59 #-----------------------------------------------------------------------------
56 60 #
57 61 #-----------------------------------------------------------------------------
58 62
59 63 # Where mathjax will be installed.
60 64
61 default_dest = os.path.join(locate_profile('default'), 'static')
65 static = os.path.join(locate_profile('default'), 'static')
66 default_dest = os.path.join(static, 'mathjax')
62 67
63 68 ##
64 69
65 70 # Test for access to install mathjax.
66 71
67 def check_perms(replace=False, dest=default_dest):
68 if not os.access(static, os.W_OK):
69 raise IOError("Need have write access to %s" % static)
72 def check_perms(dest, replace=False):
73 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
74 if not os.access(parent, os.W_OK):
75 raise IOError("Need have write access to %s" % parent)
70 76 if os.path.exists(dest):
71 77 if replace:
72 78 if not os.access(dest, os.W_OK):
@@ -91,10 +97,11 b' def extract_tar( fd, dest ) :'
91 97 topdir = tar.firstmember.path
92 98
93 99 # extract the archive (contains a single directory) to the static/ directory
94 tar.extractall(static)
100 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
101 tar.extractall(parent)
95 102
96 103 # it will be mathjax-MathJax-<sha>, rename to just mathjax
97 os.rename(os.path.join(static, topdir), dest)
104 os.rename(os.path.join(parent, topdir), dest)
98 105
99 106 ##
100 107
@@ -106,12 +113,13 b' def extract_zip( fd, dest ) :'
106 113 topdir = z.namelist()[0]
107 114
108 115 # extract the archive (contains a single directory) to the static/ directory
109 z.extractall( static )
116 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
117 z.extractall( parent )
110 118
111 119 # it will be mathjax-MathJax-<sha>, rename to just mathjax
112 d = os.path.join(static, topdir)
120 d = os.path.join(parent, topdir)
113 121 print d
114 os.rename(os.path.join(static, topdir), dest)
122 os.rename(os.path.join(parent, topdir), dest)
115 123
116 124 ##
117 125
@@ -139,83 +147,108 b" def install_mathjax(tag='v2.0', dest=default_dest, replace=False, file=None, ext"
139 147 extractor : function
140 148 Method tu use to untar/unzip/... `file`
141 149 """
142
143 if not check_perms(replace, dest=dest) :
150 if not check_perms(dest, replace) :
144 151 return
145 152
146 153 if file is None :
147 154 # download mathjax
148 mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s"%tag
149 print "Downloading mathjax source from %s"%mathjax_url
155 mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s" % tag
156 print "Downloading mathjax source from %s" % mathjax_url
150 157 response = urllib2.urlopen(mathjax_url)
151 158 file = response.fp
152 159
153 print "Extracting to %s"%dest
154 extractor( fd, dest )
160 print "Extracting to %s" % dest
161 extractor( file, dest )
155 162
156 163 ##
157 164
158 def test_func( remove, dest=default_dest) :
165 def test_func( remove, dest) :
159 166 """See if mathjax appears to be installed correctly"""
167 status = 0
160 168 if not os.path.isdir( dest ) :
161 print "%s directory not found"%dest
162 status=1
169 print "%s directory not found" % dest
170 status = 1
163 171 if not os.path.exists( dest + "/MathJax.js" ) :
164 print "MathJax.js not present in %s"%dest
165 status=1
172 print "MathJax.js not present in %s" % dest
173 status = 1
166 174 print "ok"
167 if remove :
175 if remove and os.path.exists(dest):
168 176 shutil.rmtree( dest )
169 177 return status
170 178
171 179 ##
172 180
173 def main( args ) :
181 def main() :
174 182 # This main is just simple enough that it is not worth the
175 183 # complexity of argparse
176 184
177 185 # What directory is mathjax in?
178 if '-d' in args :
186 parser = argparse.ArgumentParser(
187 description="""Install mathjax from internet or local archive""",
188 )
189
190 parser.add_argument(
191 '-i',
192 '--install-dir',
193 default=default_dest,
194 help='installation directory (by default : %s)' % (default_dest))
195 parser.add_argument(
196 '-d',
197 '--dest',
198 action='store_true',
199 help='print where is current mathjax would be installed and exit')
200 parser.add_argument(
201 '-r',
202 '--replace',
203 action='store_true',
204 help='Wether to replace current mathjax if already exist')
205 parser.add_argument(
206 '-t',
207 '--test',
208 action='store_true')
209 parser.add_argument('tarball',
210 type=int,
211 help="the local tar/zip-ball containing mathjax",
212 nargs='?',
213 metavar='tarball')
214
215 pargs = parser.parse_args()
216
217 dest = pargs.install_dir
218 if pargs.dest :
179 219 print dest
180 220 return
181 221
182 # help
183 if '-h' in args or '--help' in args :
184 print __doc__
185 return
186
187 222 # remove/replace existing mathjax?
188 if '-r' in args :
223 if pargs.replace :
189 224 replace = True
190 args.remove('-r')
191 225 else :
192 226 replace = False
193 227
194 228 # undocumented test interface
195 if '-test' in args :
196 return test_func( replace, dest=dest)
229 if pargs.test :
230 return test_func( replace, dest)
197 231
198 232 # do it
199 if len(args) == 0 :
200 # This is compatible with the interface documented in ipython 0.13
201 install_mathjax( replace=replace )
202 else :
203 fname = args[0]
233 if pargs.tarball :
234 fname = pargs.tarball
204 235
205 236 # automatically detect zip/tar - could do something based
206 # on file content, but really not cost-effective here.
237 # on file content, but really not cost-effective here.
207 238 if fname.endswith('.zip') :
208 239 extractor = extract_zip
209 240 else :
210 241 extractor = extract_tar
211
212 242 # do it
213 install_mathjax(fd=open(args[0],"r"), replace=replace, extractor=extractor )
243 install_mathjax(file=open(fname, "r"), replace=replace, extractor=extractor, dest=dest )
244 else:
245 install_mathjax(replace=replace, dest=dest)
246
214 247
215 248 if __name__ == '__main__' :
216 sys.exit(main( sys.argv[1:] ))
249 sys.exit(main())
217 250
218 __all__ = ['install_mathjax','main','dest']
251 __all__ = ['install_mathjax', 'main', 'dest']
219 252
220 253 """
221 254 Test notes:
@@ -231,24 +264,24 b' So, here is a manual procedure for testing this automatic installer.'
231 264 # remove mathjax from the installed ipython instance
232 265 # IOError ok if mathjax was never installed yet.
233 266
234 python -m IPython.external.mathjax -test -r
267 python -m IPython.external.mathjax --test -r
235 268
236 269 # download and install mathjax from command line:
237 270
238 271 python -m IPython.external.mathjax
239 python -m IPython.external.mathjax -test -r
272 python -m IPython.external.mathjax --test -r
240 273
241 274 # download and install from within python
242 275
243 276 python -c "from IPython.external.mathjax import install_mathjax; install_mathjax()"
244 python -m IPython.external.mathjax -test -r
277 python -m IPython.external.mathjax --test -r
245 278
246 279 # view http://www.mathjax.org/download/ in your browser
247 280 # save-as the link for MathJax-2.0 near the bottom of the page.
248 281 # The file it offers is mathjax-MathJax-v2.0-20-g07669ac.zip
249 282
250 283 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.zip
251 python -m IPython.external.mathjax -test -r
284 python -m IPython.external.mathjax --test -r
252 285
253 286 # download https://github.com/mathjax/MathJax/tarball/v2.0 in your browser
254 287 # (this is the url used internally by install_mathjax)
@@ -256,7 +289,7 b' python -m IPython.external.mathjax -test -r'
256 289
257 290 python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
258 291
259 python -m IPython.external.mathjax -test
292 python -m IPython.external.mathjax --test
260 293 # note no -r
261 294
262 295 # install it again while it is already there
@@ -265,7 +298,7 b' python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz'
265 298 # says "offline MathJax apparently already installed"
266 299
267 300 python -m IPython.external.mathjax ~/mathjax-MathJax-v2.0-20-g07669ac.tar.gz
268 python -m IPython.external.mathjax -test
301 python -m IPython.external.mathjax --test
269 302
270 303
271 304 """
General Comments 0
You need to be logged in to leave comments. Login now