-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
53 lines (39 loc) · 1.52 KB
/
Copy pathRakefile
File metadata and controls
53 lines (39 loc) · 1.52 KB
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
require 'digest'
BASE_DIR = '/tmp/nbviewer'
def notebook_name(ipynb)
/(.*)\.ipynb/.match(ipynb)[1]
end
def notebook_signature(ipynb)
Digest::MD5.file(ipynb).hexdigest
end
notebooks = Dir["**/*.ipynb"].reject do |ipynb|
ipynb.end_with?(".built.ipynb") ||
ipynb.split('/').include?('.ipynb_checkpoints')
end
notebooks.each do |ipynb|
Rake.load_rakefile(ipynb.sub('.ipynb', '.rake')) if File.file?(ipynb.sub('.ipynb', '.rake'))
name = notebook_name(ipynb)
dir = File.dirname(name)
sig = notebook_signature(ipynb)
# Generate html repr tagged with sig, to its own persistent dir
file "#{BASE_DIR}/#{name}.#{sig}.html" do |t|
mkdir_p File.dirname(t.name)
sh "runipy --skip-exceptions #{ipynb} --html #{t.name}"
end
# Link from deploy dir to original html file for this signature in persistent dir
# DON'T explicitly depend on the html file in /tmp/nbviewer - will set off chain reaction
# of building everything because base dependency is in this repo (ie newer than /tmp/nbviewer)
file "nbviewer/#{name}.html" do |t|
mkdir_p "nbviewer/#{dir}"
orig_html_file = "#{BASE_DIR}/#{name}.#{sig}.html"
Rake::Task[orig_html_file].invoke unless File.exist?(orig_html_file)
sh "ln -s #{orig_html_file} #{t.name}"
end
end
task :build_notebooks => notebooks.map {|ipynb| "nbviewer/#{notebook_name(ipynb)}.html"} do
puts "I'm finished!"
end
task :setup_pre_commit_hook do
repo_abs_path = `git rev-parse --show-toplevel`
sh "ln -s #{repo_abs_path.strip}/hooks/pre-commit .git/hooks/pre-commit"
end