forked from jupyter/nbviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
125 lines (99 loc) · 3.73 KB
/
tasks.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import hashlib
import shutil
import tempfile
import sys
from tarfile import TarFile
import pip
import invoke
NOTEBOOK_VERSION = '5.0.0' # the notebook version whose LESS we will use
NOTEBOOK_CHECKSUM = '1cea3bbbd03c8e5842a1403347a8cc8134486b3ce081a2e5b1952a00ea66ed54' # sha256 checksum of notebook tarball
APP_ROOT = os.path.dirname(__file__)
NPM_BIN = os.path.join(APP_ROOT, "node_modules", ".bin")
NOTEBOOK_STATIC_PATH = os.path.join(APP_ROOT, 'notebook-%s' % NOTEBOOK_VERSION, 'notebook', 'static')
@invoke.task
def test(ctx):
ctx.run("nosetests -v")
@invoke.task
def bower(ctx):
ctx.run(
"cd {}/nbviewer/static &&".format(APP_ROOT) +
" {}/bower install".format(NPM_BIN) +
" --config.interactive=false --allow-root"
)
@invoke.task
def notebook_static(ctx):
if os.path.exists(NOTEBOOK_STATIC_PATH):
return
fname = 'notebook-%s.tar.gz' % NOTEBOOK_VERSION
nb_archive = os.path.join(APP_ROOT, fname)
if not os.path.exists(nb_archive):
print("Downloading from pypi -> %s" % nb_archive)
pip.main(['download', 'notebook=={}'.format(NOTEBOOK_VERSION), '--no-deps', '-d', APP_ROOT, '--no-binary', ':all:'])
with open(nb_archive, 'rb') as f:
checksum = hashlib.sha256(f.read()).hexdigest()
if checksum != NOTEBOOK_CHECKSUM:
print("Notebook sdist checksum mismatch", file=sys.stderr)
print("Expected: %s" % NOTEBOOK_CHECKSUM, file=sys.stderr)
print("Got: %s" % checksum, file=sys.stderr)
sys.exit(1)
with TarFile.open(nb_archive, 'r:gz') as nb_archive_file:
print("Extract {0} in {1}".format(nb_archive, nb_archive_file.extractall()))
@invoke.task
def less(ctx, debug=False):
notebook_static(ctx)
if debug:
extra = "--source-map"
else:
extra = " --clean-css='--s1 --advanced --compatibility=ie8'"
tmpl = (
"cd {}/nbviewer/static/less ".format(APP_ROOT) +
" && {}/lessc".format(NPM_BIN) +
" {1} "
" --include-path={2}"
" --autoprefix='> 1%'"
" {0}.less ../build/{0}.css"
)
args = (extra, NOTEBOOK_STATIC_PATH)
for less_file in ["styles", "notebook", "slides"]:
ctx.run(tmpl.format(less_file, *args))
@invoke.task
def screenshots(ctx, root="http://localhost:5000/", dest="./screenshots"):
dest = os.path.abspath(dest)
script = """
root = "{root}"
urls = ({{name, url}} for name, url of {{
home: ""
dir: "github/ipython/ipython/tree/3.x/examples/"
user: "github/ipython/"
gists: "gist/fperez/"
notebook: "github/ipython/ipython/blob/3.x/examples/Notebook/Notebook%20Basics.ipynb"}})
screens = ({{name, w, h}} for name, [w, h] of {{
smartphone_portrait: [320, 480]
smartphone_landscape: [480, 320]
tablet_portrait: [768, 1024]
tablet_landscape: [1024, 768]
desktop_standard: [1280, 1024]
desktop_1080p: [1920, 1080]
}})
casper.start root
casper.each screens, (_, screen) ->
@then ->
@viewport screen.w, screen.h, ->
_.each urls, (_, page) ->
@thenOpen root + page.url, ->
@wait 1000
@then ->
@echo "#{{page.name}} #{{screen.name}}"
@capture "{dest}/#{{page.name}}-#{{screen.name}}.png"
casper.run()
""".format(root=root, dest=dest)
tmpdir = tempfile.mkdtemp()
tmpfile = os.path.join(tmpdir, "screenshots.coffee")
with open(tmpfile, "w+") as f:
f.write(script)
ctx.run("casperjs test {script}".format(script=tmpfile))
shutil.rmtree(tmpdir)