2008-06-07 06:16:36 +08:00
|
|
|
# encoding: utf-8
|
2008-06-07 06:24:37 +08:00
|
|
|
"""
|
2015-04-11 04:20:40 +08:00
|
|
|
This module defines the things that are used in setup.py for building the notebook
|
2008-06-07 06:24:37 +08:00
|
|
|
|
|
|
|
This includes:
|
|
|
|
|
|
|
|
* Functions for finding things like packages, package data, etc.
|
|
|
|
* A function for checking dependencies.
|
|
|
|
"""
|
2008-06-07 06:16:36 +08:00
|
|
|
|
2014-04-10 06:52:59 +08:00
|
|
|
# Copyright (c) IPython Development Team.
|
|
|
|
# Distributed under the terms of the Modified BSD License.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
2008-06-07 06:16:36 +08:00
|
|
|
|
2010-10-27 08:57:12 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2008-06-07 06:16:36 +08:00
|
|
|
|
2014-07-24 06:33:08 +08:00
|
|
|
from distutils import log
|
2010-10-27 08:57:12 +08:00
|
|
|
from distutils.command.build_py import build_py
|
2013-05-01 07:18:52 +08:00
|
|
|
from distutils.cmd import Command
|
2014-12-27 16:16:28 +08:00
|
|
|
from distutils.errors import DistutilsExecError
|
2014-03-15 02:21:38 +08:00
|
|
|
from fnmatch import fnmatch
|
2008-06-07 06:16:36 +08:00
|
|
|
from glob import glob
|
2015-04-12 07:19:00 +08:00
|
|
|
from subprocess import Popen, PIPE, check_call
|
2008-06-07 06:16:36 +08:00
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Useful globals and utility functions
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# A few handy globals
|
|
|
|
isfile = os.path.isfile
|
|
|
|
pjoin = os.path.join
|
2013-05-05 04:38:32 +08:00
|
|
|
repo_root = os.path.dirname(os.path.abspath(__file__))
|
2008-06-07 06:16:36 +08:00
|
|
|
|
|
|
|
def oscmd(s):
|
2010-10-27 08:57:12 +08:00
|
|
|
print(">", s)
|
2008-06-07 06:16:36 +08:00
|
|
|
os.system(s)
|
2011-10-04 22:14:41 +08:00
|
|
|
|
2012-04-14 16:26:34 +08:00
|
|
|
# Py3 compatibility hacks, without assuming IPython itself is installed with
|
|
|
|
# the full py3compat machinery.
|
|
|
|
|
2011-08-05 05:32:15 +08:00
|
|
|
try:
|
|
|
|
execfile
|
|
|
|
except NameError:
|
|
|
|
def execfile(fname, globs, locs=None):
|
|
|
|
locs = locs or globs
|
|
|
|
exec(compile(open(fname).read(), fname, "exec"), globs, locs)
|
2008-06-07 06:16:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
# Basic project information
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2015-04-11 04:20:40 +08:00
|
|
|
name = 'jupyter_notebook'
|
|
|
|
|
2009-08-04 23:22:09 +08:00
|
|
|
# release.py contains version, authors, license, url, keywords, etc.
|
2015-04-11 04:20:40 +08:00
|
|
|
version_ns = {}
|
|
|
|
execfile(pjoin(repo_root, name, '_version.py'), version_ns)
|
|
|
|
|
|
|
|
version = version_ns['__version__']
|
2008-06-07 06:16:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
# Find packages
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def find_packages():
|
2008-06-07 06:24:37 +08:00
|
|
|
"""
|
2015-04-11 04:20:40 +08:00
|
|
|
Find all of the packages.
|
2008-06-07 06:24:37 +08:00
|
|
|
"""
|
2011-07-31 05:16:38 +08:00
|
|
|
packages = []
|
2015-04-11 04:20:40 +08:00
|
|
|
for dir,subdirs,files in os.walk(name):
|
2011-07-31 05:16:38 +08:00
|
|
|
package = dir.replace(os.path.sep, '.')
|
|
|
|
if '__init__.py' not in files:
|
|
|
|
# not a package
|
|
|
|
continue
|
|
|
|
packages.append(package)
|
2008-06-07 06:16:36 +08:00
|
|
|
return packages
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
# Find package data
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def find_package_data():
|
2008-06-07 06:24:37 +08:00
|
|
|
"""
|
2015-04-11 04:20:40 +08:00
|
|
|
Find package_data.
|
2008-06-07 06:24:37 +08:00
|
|
|
"""
|
2008-06-07 06:16:36 +08:00
|
|
|
# This is not enough for these things to appear in an sdist.
|
|
|
|
# We need to muck with the MANIFEST to get this to work
|
2011-12-06 13:19:56 +08:00
|
|
|
|
2014-03-15 02:21:38 +08:00
|
|
|
# exclude components and less from the walk;
|
2014-02-01 03:31:24 +08:00
|
|
|
# we will build the components separately
|
2015-04-11 04:20:40 +08:00
|
|
|
excludes = [
|
|
|
|
pjoin('static', 'components'),
|
|
|
|
pjoin('static', '*', 'less'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# walk notebook resources:
|
|
|
|
cwd = os.getcwd()
|
|
|
|
os.chdir('jupyter_notebook')
|
|
|
|
static_data = []
|
|
|
|
for parent, dirs, files in os.walk('static'):
|
|
|
|
if any(fnmatch(parent, pat) for pat in excludes):
|
|
|
|
# prevent descending into subdirs
|
|
|
|
dirs[:] = []
|
|
|
|
continue
|
|
|
|
for f in files:
|
|
|
|
static_data.append(pjoin(parent, f))
|
|
|
|
|
|
|
|
components = pjoin("static", "components")
|
|
|
|
# select the components we actually need to install
|
|
|
|
# (there are lots of resources we bundle for sdist-reasons that we don't actually use)
|
|
|
|
static_data.extend([
|
|
|
|
pjoin(components, "backbone", "backbone-min.js"),
|
|
|
|
pjoin(components, "bootstrap", "js", "bootstrap.min.js"),
|
|
|
|
pjoin(components, "bootstrap-tour", "build", "css", "bootstrap-tour.min.css"),
|
|
|
|
pjoin(components, "bootstrap-tour", "build", "js", "bootstrap-tour.min.js"),
|
|
|
|
pjoin(components, "es6-promise", "*.js"),
|
|
|
|
pjoin(components, "font-awesome", "fonts", "*.*"),
|
|
|
|
pjoin(components, "google-caja", "html-css-sanitizer-minified.js"),
|
|
|
|
pjoin(components, "jquery", "jquery.min.js"),
|
|
|
|
pjoin(components, "jquery-ui", "ui", "minified", "jquery-ui.min.js"),
|
|
|
|
pjoin(components, "jquery-ui", "themes", "smoothness", "jquery-ui.min.css"),
|
|
|
|
pjoin(components, "jquery-ui", "themes", "smoothness", "images", "*"),
|
|
|
|
pjoin(components, "marked", "lib", "marked.js"),
|
|
|
|
pjoin(components, "requirejs", "require.js"),
|
|
|
|
pjoin(components, "underscore", "underscore-min.js"),
|
|
|
|
pjoin(components, "moment", "moment.js"),
|
|
|
|
pjoin(components, "moment", "min", "moment.min.js"),
|
|
|
|
pjoin(components, "term.js", "src", "term.js"),
|
|
|
|
pjoin(components, "text-encoding", "lib", "encoding.js"),
|
|
|
|
])
|
|
|
|
|
|
|
|
# Ship all of Codemirror's CSS and JS
|
|
|
|
for parent, dirs, files in os.walk(pjoin(components, 'codemirror')):
|
|
|
|
for f in files:
|
|
|
|
if f.endswith(('.js', '.css')):
|
|
|
|
static_data.append(pjoin(parent, f))
|
|
|
|
|
2015-04-18 03:02:47 +08:00
|
|
|
# Trim mathjax
|
|
|
|
mj = lambda *path: pjoin(components, 'MathJax', *path)
|
|
|
|
static_data.extend([
|
|
|
|
mj('MathJax.js'),
|
|
|
|
mj('config', 'TeX-AMS_HTML-full.js'),
|
|
|
|
mj('jax', 'output', 'HTML-CSS', '*.js'),
|
|
|
|
])
|
|
|
|
for tree in [
|
|
|
|
mj('localization'), # limit to en?
|
|
|
|
mj('fonts', 'HTML-CSS', 'STIX-Web', 'woff'),
|
|
|
|
mj('jax', 'input', 'TeX'),
|
|
|
|
mj('jax', 'output', 'HTML-CSS', 'autoload'),
|
|
|
|
mj('jax', 'output', 'HTML-CSS', 'fonts', 'STIX-Web'),
|
|
|
|
]:
|
|
|
|
for parent, dirs, files in os.walk(tree):
|
|
|
|
for f in files:
|
|
|
|
static_data.append(pjoin(parent, f))
|
|
|
|
|
|
|
|
|
2015-04-11 04:20:40 +08:00
|
|
|
os.chdir(os.path.join('tests',))
|
|
|
|
js_tests = glob('*.js') + glob('*/*.js')
|
|
|
|
|
|
|
|
os.chdir(cwd)
|
2013-09-27 08:39:06 +08:00
|
|
|
|
2008-07-02 12:19:24 +08:00
|
|
|
package_data = {
|
2015-04-11 04:20:40 +08:00
|
|
|
'jupyter_notebook' : ['templates/*'] + static_data,
|
|
|
|
'jupyter_notebook.tests' : js_tests,
|
2008-07-02 12:19:24 +08:00
|
|
|
}
|
2014-02-01 06:42:40 +08:00
|
|
|
|
2014-02-06 13:36:49 +08:00
|
|
|
return package_data
|
|
|
|
|
2014-02-08 06:12:53 +08:00
|
|
|
|
2014-02-06 13:36:49 +08:00
|
|
|
def check_package_data(package_data):
|
|
|
|
"""verify that package_data globs make sense"""
|
|
|
|
print("checking package data")
|
2014-02-01 06:42:40 +08:00
|
|
|
for pkg, data in package_data.items():
|
|
|
|
pkg_root = pjoin(*pkg.split('.'))
|
|
|
|
for d in data:
|
|
|
|
path = pjoin(pkg_root, d)
|
|
|
|
if '*' in path:
|
|
|
|
assert len(glob(path)) > 0, "No files match pattern %s" % path
|
|
|
|
else:
|
|
|
|
assert os.path.exists(path), "Missing package data: %s" % path
|
|
|
|
|
2008-06-07 06:16:36 +08:00
|
|
|
|
2014-02-08 06:12:53 +08:00
|
|
|
def check_package_data_first(command):
|
|
|
|
"""decorator for checking package_data before running a given command
|
|
|
|
|
|
|
|
Probably only needs to wrap build_py
|
|
|
|
"""
|
|
|
|
class DecoratedCommand(command):
|
|
|
|
def run(self):
|
|
|
|
check_package_data(self.package_data)
|
|
|
|
command.run(self)
|
|
|
|
return DecoratedCommand
|
2008-06-07 06:16:36 +08:00
|
|
|
|
2013-05-01 07:18:52 +08:00
|
|
|
#---------------------------------------------------------------------------
|
2015-04-12 07:19:00 +08:00
|
|
|
# Notebook related
|
2013-05-01 07:18:52 +08:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2015-04-12 07:19:00 +08:00
|
|
|
try:
|
|
|
|
from shutil import which
|
|
|
|
except ImportError:
|
2015-04-13 22:09:05 +08:00
|
|
|
## which() function copied from Python 3.4.3; PSF license
|
2015-04-13 22:04:25 +08:00
|
|
|
def which(cmd, mode=os.F_OK | os.X_OK, path=None):
|
|
|
|
"""Given a command, mode, and a PATH string, return the path which
|
|
|
|
conforms to the given mode on the PATH, or None if there is no such
|
|
|
|
file.
|
|
|
|
|
|
|
|
`mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
|
|
|
|
of os.environ.get("PATH"), or can be overridden with a custom search
|
|
|
|
path.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Check that a given file can be accessed with the correct mode.
|
|
|
|
# Additionally check that `file` is not a directory, as on Windows
|
|
|
|
# directories pass the os.access check.
|
|
|
|
def _access_check(fn, mode):
|
|
|
|
return (os.path.exists(fn) and os.access(fn, mode)
|
|
|
|
and not os.path.isdir(fn))
|
|
|
|
|
|
|
|
# If we're given a path with a directory part, look it up directly rather
|
|
|
|
# than referring to PATH directories. This includes checking relative to the
|
|
|
|
# current directory, e.g. ./script
|
|
|
|
if os.path.dirname(cmd):
|
|
|
|
if _access_check(cmd, mode):
|
|
|
|
return cmd
|
|
|
|
return None
|
|
|
|
|
|
|
|
if path is None:
|
|
|
|
path = os.environ.get("PATH", os.defpath)
|
|
|
|
if not path:
|
|
|
|
return None
|
|
|
|
path = path.split(os.pathsep)
|
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
|
|
# The current directory takes precedence on Windows.
|
|
|
|
if not os.curdir in path:
|
|
|
|
path.insert(0, os.curdir)
|
|
|
|
|
|
|
|
# PATHEXT is necessary to check on Windows.
|
|
|
|
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
|
|
|
|
# See if the given file matches any of the expected path extensions.
|
|
|
|
# This will allow us to short circuit when given "python.exe".
|
|
|
|
# If it does match, only test that one, otherwise we have to try
|
|
|
|
# others.
|
|
|
|
if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
|
|
|
|
files = [cmd]
|
|
|
|
else:
|
|
|
|
files = [cmd + ext for ext in pathext]
|
|
|
|
else:
|
|
|
|
# On other platforms you don't have things like PATHEXT to tell you
|
|
|
|
# what file suffixes are executable, so just pass on cmd as-is.
|
|
|
|
files = [cmd]
|
|
|
|
|
|
|
|
seen = set()
|
|
|
|
for dir in path:
|
|
|
|
normdir = os.path.normcase(dir)
|
|
|
|
if not normdir in seen:
|
|
|
|
seen.add(normdir)
|
|
|
|
for thefile in files:
|
|
|
|
name = os.path.join(dir, thefile)
|
|
|
|
if _access_check(name, mode):
|
|
|
|
return name
|
|
|
|
return None
|
|
|
|
|
2015-04-12 07:19:00 +08:00
|
|
|
|
|
|
|
static = pjoin(repo_root, 'jupyter_notebook', 'static')
|
|
|
|
|
|
|
|
npm_path = os.pathsep.join([
|
|
|
|
pjoin(repo_root, 'node_modules', '.bin'),
|
|
|
|
os.environ.get("PATH", os.defpath),
|
|
|
|
])
|
2013-05-01 07:18:52 +08:00
|
|
|
|
2015-04-12 07:19:00 +08:00
|
|
|
def mtime(path):
|
|
|
|
"""shorthand for mtime"""
|
|
|
|
return os.stat(path).st_mtime
|
|
|
|
|
|
|
|
py3compat_ns = {}
|
|
|
|
|
|
|
|
class Bower(Command):
|
|
|
|
description = "fetch static client-side components with bower"
|
2013-05-01 07:18:52 +08:00
|
|
|
|
2015-04-12 07:19:00 +08:00
|
|
|
user_options = [
|
|
|
|
('force', 'f', "force fetching of bower dependencies"),
|
|
|
|
]
|
2013-05-01 07:18:52 +08:00
|
|
|
|
|
|
|
def initialize_options(self):
|
2015-04-12 07:19:00 +08:00
|
|
|
self.force = False
|
2013-05-01 07:18:52 +08:00
|
|
|
|
|
|
|
def finalize_options(self):
|
2015-04-12 07:19:00 +08:00
|
|
|
self.force = bool(self.force)
|
|
|
|
|
|
|
|
bower_dir = pjoin(static, 'components')
|
|
|
|
node_modules = pjoin(repo_root, 'node_modules')
|
|
|
|
|
|
|
|
def should_run(self):
|
|
|
|
if self.force:
|
|
|
|
return True
|
|
|
|
if not os.path.exists(self.bower_dir):
|
|
|
|
return True
|
|
|
|
return mtime(self.bower_dir) < mtime(pjoin(repo_root, 'bower.json'))
|
|
|
|
|
|
|
|
def should_run_npm(self):
|
|
|
|
if not which('npm'):
|
|
|
|
print("npm unavailable", file=sys.stderr)
|
|
|
|
return False
|
|
|
|
if not os.path.exists(self.node_modules):
|
|
|
|
return True
|
|
|
|
return mtime(self.node_modules) < mtime(pjoin(repo_root, 'package.json'))
|
2013-05-01 07:18:52 +08:00
|
|
|
|
|
|
|
def run(self):
|
2015-04-12 07:19:00 +08:00
|
|
|
if not self.should_run():
|
|
|
|
print("bower dependencies up to date")
|
|
|
|
return
|
2013-05-01 07:18:52 +08:00
|
|
|
|
2015-04-12 07:19:00 +08:00
|
|
|
if self.should_run_npm():
|
|
|
|
print("installing build dependencies with npm")
|
|
|
|
check_call(['npm', 'install'], cwd=repo_root)
|
2015-04-13 22:13:29 +08:00
|
|
|
os.utime(self.node_modules, None)
|
2015-04-12 07:19:00 +08:00
|
|
|
|
|
|
|
env = os.environ.copy()
|
|
|
|
env['PATH'] = npm_path
|
|
|
|
|
|
|
|
try:
|
|
|
|
check_call(
|
|
|
|
['bower', 'install', '--allow-root', '--config.interactive=false'],
|
|
|
|
cwd=repo_root,
|
2015-04-16 05:56:06 +08:00
|
|
|
env=env
|
2015-04-12 07:19:00 +08:00
|
|
|
)
|
|
|
|
except OSError as e:
|
|
|
|
print("Failed to run bower: %s" % e, file=sys.stderr)
|
|
|
|
print("You can install js dependencies with `npm install`", file=sys.stderr)
|
|
|
|
raise
|
2015-04-13 22:13:29 +08:00
|
|
|
os.utime(self.bower_dir, None)
|
2015-04-12 07:19:00 +08:00
|
|
|
# update package data in case this created new files
|
|
|
|
self.distribution.package_data = find_package_data()
|
2013-09-15 03:21:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CompileCSS(Command):
|
|
|
|
"""Recompile Notebook CSS
|
|
|
|
|
|
|
|
Regenerate the compiled CSS from LESS sources.
|
|
|
|
|
2015-04-16 05:56:06 +08:00
|
|
|
Requires various dev dependencies, such as gulp and lessc.
|
2013-09-15 03:21:05 +08:00
|
|
|
"""
|
|
|
|
description = "Recompile Notebook CSS"
|
2015-04-16 05:56:06 +08:00
|
|
|
user_options = []
|
|
|
|
|
2013-09-15 03:21:05 +08:00
|
|
|
def initialize_options(self):
|
2015-04-16 05:56:06 +08:00
|
|
|
pass
|
|
|
|
|
2013-09-15 03:21:05 +08:00
|
|
|
def finalize_options(self):
|
2015-04-16 05:56:06 +08:00
|
|
|
pass
|
|
|
|
|
2015-04-12 07:19:00 +08:00
|
|
|
def run(self):
|
|
|
|
|
|
|
|
self.run_command('js')
|
|
|
|
env = os.environ.copy()
|
|
|
|
env['PATH'] = npm_path
|
2015-04-16 05:56:06 +08:00
|
|
|
try:
|
|
|
|
check_call(['gulp','css'], cwd=repo_root, env=env)
|
|
|
|
except OSError as e:
|
|
|
|
print("Failed to run gulp: %s" % e, file=sys.stderr)
|
|
|
|
print("You can install js dependencies with `npm install`", file=sys.stderr)
|
|
|
|
raise
|
2015-04-12 07:19:00 +08:00
|
|
|
# update package data in case this created new files
|
|
|
|
self.distribution.package_data = find_package_data()
|
2014-07-24 05:19:27 +08:00
|
|
|
|
2013-10-26 05:01:31 +08:00
|
|
|
|
|
|
|
class JavascriptVersion(Command):
|
|
|
|
"""write the javascript version to notebook javascript"""
|
|
|
|
description = "Write IPython version to javascript"
|
|
|
|
user_options = []
|
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def finalize_options(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run(self):
|
2015-04-09 03:32:13 +08:00
|
|
|
nsfile = pjoin(repo_root, "jupyter_notebook", "static", "base", "js", "namespace.js")
|
2013-10-26 05:01:31 +08:00
|
|
|
with open(nsfile) as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
with open(nsfile, 'w') as f:
|
2015-01-31 06:19:25 +08:00
|
|
|
found = False
|
2013-10-26 05:01:31 +08:00
|
|
|
for line in lines:
|
2015-01-31 06:19:25 +08:00
|
|
|
if line.strip().startswith("IPython.version"):
|
|
|
|
line = ' IPython.version = "{0}";\n'.format(version)
|
|
|
|
found = True
|
2013-10-26 05:01:31 +08:00
|
|
|
f.write(line)
|
2015-01-31 06:19:25 +08:00
|
|
|
if not found:
|
|
|
|
raise RuntimeError("Didn't find IPython.version line in %s" % nsfile)
|
2014-07-24 05:19:27 +08:00
|
|
|
|
|
|
|
|
2015-01-10 06:02:39 +08:00
|
|
|
def css_js_prerelease(command):
|
2015-04-12 07:19:00 +08:00
|
|
|
"""decorator for building js/minified css prior to another command"""
|
2014-07-24 05:19:27 +08:00
|
|
|
class DecoratedCommand(command):
|
|
|
|
def run(self):
|
|
|
|
self.distribution.run_command('jsversion')
|
2015-04-12 07:19:00 +08:00
|
|
|
js = self.distribution.get_command_obj('js')
|
|
|
|
js.force = True
|
2014-07-24 05:19:27 +08:00
|
|
|
css = self.distribution.get_command_obj('css')
|
2015-04-12 07:19:00 +08:00
|
|
|
css.force = True
|
2014-07-24 06:33:08 +08:00
|
|
|
try:
|
|
|
|
self.distribution.run_command('css')
|
|
|
|
except Exception as e:
|
2015-01-10 06:02:39 +08:00
|
|
|
log.warn("rebuilding css and sourcemaps failed (not a problem)")
|
|
|
|
log.warn(str(e))
|
2014-07-24 05:19:27 +08:00
|
|
|
command.run(self)
|
|
|
|
return DecoratedCommand
|