-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_julia_notebooks.jl
More file actions
executable file
·121 lines (98 loc) · 3.22 KB
/
update_julia_notebooks.jl
File metadata and controls
executable file
·121 lines (98 loc) · 3.22 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
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
#!/usr/bin/env julia
"""
Update Julia notebook metadata to use the custom 'juliabook' kernel.
This script updates all *_jl.ipynb files in the src/ directory.
"""
using JSON
using Glob
using Printf
function find_julia_notebooks(src_dir::String="src")
"""
Find all Julia notebook files (*_jl.ipynb) in the specified directory.
"""
pattern = joinpath(src_dir, "*_jl.ipynb")
return glob(pattern)
end
function update_notebook_kernel(notebook_path::String, kernel_name::String="juliabook")
"""
Update the kernel specification in a Julia notebook.
Args:
notebook_path: Path to the notebook file
kernel_name: Name of the kernel to use (default: 'juliabook')
Returns:
bool: True if the notebook was updated, False otherwise
"""
try
# Read the notebook
notebook = JSON.parsefile(notebook_path)
# Check if metadata exists
if !haskey(notebook, "metadata")
notebook["metadata"] = Dict{String, Any}()
end
# Update kernelspec
notebook["metadata"]["kernelspec"] = Dict(
"display_name" => "Julia $(VERSION)",
"language" => "julia",
"name" => kernel_name
)
# Update language_info
notebook["metadata"]["language_info"] = Dict(
"file_extension" => ".jl",
"mimetype" => "application/julia",
"name" => "julia",
"version" => "$(VERSION)"
)
# Write back the notebook
open(notebook_path, "w") do io
JSON.print(io, notebook, 1)
end
return true
catch e
@error "Error updating $notebook_path: $e"
return false
end
end
function main()
# Parse command line arguments
src_dir = length(ARGS) > 0 && startswith(ARGS[1], "--src-dir=") ?
split(ARGS[1], "=")[2] : "src"
kernel_name = length(ARGS) > 1 && startswith(ARGS[2], "--kernel-name=") ?
split(ARGS[2], "=")[2] : "juliabook"
dry_run = "--dry-run" in ARGS
# Find Julia notebooks
notebooks = find_julia_notebooks(src_dir)
if isempty(notebooks)
@printf "No Julia notebooks (*_jl.ipynb) found in %s/\n" src_dir
return
end
@printf "Found %d Julia notebook(s) in %s/:\n" length(notebooks) src_dir
for nb in notebooks
@printf " - %s\n" nb
end
if dry_run
@printf "\nDry run mode - would update kernel to '%s' for all notebooks\n" kernel_name
return
end
# Update notebooks
updated_count = 0
failed_count = 0
@printf "\nUpdating kernel metadata to '%s'...\n" kernel_name
for notebook_path in notebooks
@printf "Updating %s... " basename(notebook_path)
if update_notebook_kernel(notebook_path, kernel_name)
println("✓")
updated_count += 1
else
println("✗")
failed_count += 1
end
end
println("\nSummary:")
@printf " Updated: %d\n" updated_count
@printf " Failed: %d\n" failed_count
@printf " Total: %d\n" length(notebooks)
end
# Main execution
if abspath(PROGRAM_FILE) == @__FILE__
main()
end