1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
#!/usr/bin/python
"""Utility function for installing MathJax javascript library into
the notebook's 'static' directory, for offline use.
Authors:
* Min RK
* Mark Sienkiewicz
* Matthias Bussonnier
To download and install MathJax:
From Python:
>>> from IPython.external.mathjax import install_mathjax
>>> install_mathjax()
From the command line:
$ python -m IPython.external.mathjax
To a specific profile:
$ python -m IPython.external.mathjax --profile=research
To install MathJax from a file you have already downloaded:
$ python -m IPython.external.mathjax mathjax-xxx.tar.gz
$ python -m IPython.external.mathjax mathjax-xxx.zip
It will not install MathJax if it is already there. Use -r to
replace the existing copy of MathJax.
To find the directory where IPython would like MathJax installed:
$ python -m IPython.external.mathjax -d
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import shutil
import sys
import tarfile
import urllib2
import zipfile
from IPython.utils.path import locate_profile
from IPython.external import argparse
#-----------------------------------------------------------------------------
#
#-----------------------------------------------------------------------------
# Where mathjax will be installed.
static = os.path.join(locate_profile('default'), 'static')
default_dest = os.path.join(static, 'mathjax')
##
# Test for access to install mathjax.
def check_perms(dest, replace=False):
parent = os.path.abspath(os.path.join(dest, os.path.pardir))
components = dest.split(os.path.sep)
subpaths = [ os.path.sep+os.path.sep.join(components[1:i]) for i in range(1,len(components))]
existing_path = filter(os.path.exists, subpaths)
last_writable = existing_path[-1]
if not os.access(last_writable, os.W_OK):
raise IOError("Need have write access to %s" % parent)
not_existing = [ path for path in subpaths if path not in existing_path]
# subfolder we will create, will obviously be writable
# should we still considere checking separately that
# ipython profiles have been created ?
for folder in not_existing:
os.mkdir(folder)
if os.path.exists(dest):
if replace:
if not os.access(dest, os.W_OK):
raise IOError("Need have write access to %s" % dest)
print "removing previous MathJax install"
shutil.rmtree(dest)
return True
else:
print "offline MathJax apparently already installed"
return False
else :
return True
##
def extract_tar( fd, dest ) :
# use 'r|gz' stream mode, because socket file-like objects can't seek:
tar = tarfile.open(fileobj=fd, mode='r|gz')
# we just happen to know that the first entry in the mathjax
# archive is the directory that the remaining members are in.
topdir = tar.firstmember.path
# extract the archive (contains a single directory) to the static/ directory
parent = os.path.abspath(os.path.join(dest, os.path.pardir))
tar.extractall(parent)
# it will be mathjax-MathJax-<sha>, rename to just mathjax
os.rename(os.path.join(parent, topdir), dest)
##
def extract_zip( fd, dest ) :
z = zipfile.ZipFile( fd, 'r' )
# we just happen to know that the first entry in the mathjax
# archive is the directory that the remaining members are in.
topdir = z.namelist()[0]
# extract the archive (contains a single directory) to the static/ directory
parent = os.path.abspath(os.path.join(dest, os.path.pardir))
z.extractall( parent )
# it will be mathjax-MathJax-<sha>, rename to just mathjax
d = os.path.join(parent, topdir)
print d
os.rename(os.path.join(parent, topdir), dest)
##
def install_mathjax(tag='v2.0', dest=default_dest, replace=False, file=None, extractor=extract_tar ):
"""Download and/or install MathJax for offline use.
This will install mathjax to the 'static' dir in the IPython notebook
package, so it will fail if the caller does not have write access
to that location.
MathJax is a ~15MB download, and ~150MB installed.
Parameters
----------
replace : bool [False]
Whether to remove and replace an existing install.
dest : str [path to default profile]
Where to locally install mathjax
tag : str ['v2.0']
Which tag to download. Default is 'v2.0', the current stable release,
but alternatives include 'v1.1a' and 'master'.
file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}]
File handle from which to untar/unzip/... mathjax
extractor : function
Method tu use to untar/unzip/... `file`
"""
if not check_perms(dest, replace) :
return
if file is None :
# download mathjax
mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s" % tag
print "Downloading mathjax source from %s" % mathjax_url
response = urllib2.urlopen(mathjax_url)
file = response.fp
print "Extracting to %s" % dest
extractor( file, dest )
##
def test_func( remove, dest) :
"""See if mathjax appears to be installed correctly"""
status = 0
if not os.path.isdir( dest ) :
print "%s directory not found" % dest
status = 1
if not os.path.exists( dest + "/MathJax.js" ) :
print "MathJax.js not present in %s" % dest
status = 1
print "ok"
if remove and os.path.exists(dest):
shutil.rmtree( dest )
return status
##
def main() :
# This main is just simple enough that it is not worth the
# complexity of argparse
# What directory is mathjax in?
parser = argparse.ArgumentParser(
description="""Install mathjax from internet or local archive""",
)
parser.add_argument(
'-p',
'--profile',
default='default',
help='profile to install MathJax to (default is default)')
parser.add_argument(
'-i',
'--install-dir',
help='custom installation directory')
parser.add_argument(
'-d',
'--dest',
action='store_true',
help='print where current mathjax would be installed and exit')
parser.add_argument(
'-r',
'--replace',
action='store_true',
help='Whether to replace current mathjax if it already exists')
parser.add_argument(
'-t',
'--test',
action='store_true')
parser.add_argument('tarball',
help="the local tar/zip-ball containing mathjax",
nargs='?',
metavar='tarball')
pargs = parser.parse_args()
if pargs.install_dir:
# Explicit install_dir overrides profile
dest = pargs.install_dir
else:
profile = pargs.profile
dest = os.path.join(locate_profile(profile), 'static', 'mathjax')
if pargs.dest :
print dest
return
# remove/replace existing mathjax?
if pargs.replace :
replace = True
else :
replace = False
# undocumented test interface
if pargs.test :
return test_func( replace, dest)
# do it
if pargs.tarball :
fname = pargs.tarball
# automatically detect zip/tar - could do something based
# on file content, but really not cost-effective here.
if fname.endswith('.zip') :
extractor = extract_zip
else :
extractor = extract_tar
# do it
install_mathjax(file=open(fname, "r"), replace=replace, extractor=extractor, dest=dest )
else:
install_mathjax(replace=replace, dest=dest)
if __name__ == '__main__' :
sys.exit(main())
__all__ = ['install_mathjax', 'main', 'dest']
"""
Test notes:
IPython uses IPython.testing.iptest as a custom test controller
(though it is based on nose). It might be possible to fit automatic
tests of installation into that framework, but it looks awkward to me.
So, here is a manual procedure for testing this automatic installer.
Mark Sienkiewicz, 2012-08-06
first 8 letters of my last name @ stsci.edu
# remove mathjax from the installed ipython instance
# IOError ok if mathjax was never installed yet.
python -m IPython.external.mathjax --test -r
# download and install mathjax from command line:
python -m IPython.external.mathjax
python -m IPython.external.mathjax --test -r
# download and install from within python
python -c "from IPython.external.mathjax import install_mathjax; install_mathjax()"
python -m IPython.external.mathjax --test -r
# view http://www.mathjax.org/download/ in your browser
# save-as the link for MathJax-2.0 near the bottom of the page.
# The file it offers is mathjax-MathJax-v2.0-20-g07669ac.zip
python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.zip
python -m IPython.external.mathjax --test -r
# download https://github.com/mathjax/MathJax/tarball/v2.0 in your browser
# (this is the url used internally by install_mathjax)
# The file it offers is mathjax-MathJax-v2.0-20-g07669ac.tar.gz
python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
python -m IPython.external.mathjax --test
# note no -r
# install it again while it is already there
python -m IPython.external.mathjax mathjax-MathJax-v2.0-20-g07669ac.tar.gz
# says "offline MathJax apparently already installed"
python -m IPython.external.mathjax ~/mathjax-MathJax-v2.0-20-g07669ac.tar.gz
python -m IPython.external.mathjax --test
"""
|