Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli/tools/jupyter) Add --directory flag to control where jupyter kernelspec installs #23595

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ pub struct JupyterFlags {
pub install: bool,
pub kernel: bool,
pub conn_file: Option<String>,
pub directory: Option<PathBuf>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -2056,6 +2057,14 @@ fn jupyter_subcommand() -> Command {
.value_parser(value_parser!(String))
.value_hint(ValueHint::FilePath)
.conflicts_with("install"))
.arg(
Arg::new("dir")
.long("dir")
.help("Sets the directory to install kernelspec.")
.value_parser(value_parser!(PathBuf))
.value_hint(ValueHint::DirPath)
.requires("install")
)
.about("Deno kernel for Jupyter notebooks")
}

Expand Down Expand Up @@ -3823,11 +3832,17 @@ fn jupyter_parse(flags: &mut Flags, matches: &mut ArgMatches) {
let conn_file = matches.remove_one::<String>("conn");
let kernel = matches.get_flag("kernel");
let install = matches.get_flag("install");
let directory = if matches.contains_id("dir") {
matches.remove_one::<PathBuf>("dir")
} else {
None
};

flags.subcommand = DenoSubcommand::Jupyter(JupyterFlags {
install,
kernel,
conn_file,
directory,
});
}

Expand Down Expand Up @@ -9256,6 +9271,7 @@ mod tests {
install: false,
kernel: false,
conn_file: None,
directory: None,
}),
..Flags::default()
}
Expand All @@ -9269,6 +9285,27 @@ mod tests {
install: true,
kernel: false,
conn_file: None,
directory: None,
}),
..Flags::default()
}
);

let r = flags_from_vec(svec![
"deno",
"jupyter",
"--install",
"--dir",
"/tmp/deno"
]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Jupyter(JupyterFlags {
install: true,
kernel: false,
conn_file: None,
directory: Some(PathBuf::from_str("/tmp/deno").unwrap_or_default())
}),
..Flags::default()
}
Expand All @@ -9288,6 +9325,7 @@ mod tests {
install: false,
kernel: true,
conn_file: Some(String::from("path/to/conn/file")),
directory: None,
}),
..Flags::default()
}
Expand Down
52 changes: 44 additions & 8 deletions cli/tools/jupyter/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::env::current_exe;
use std::io::ErrorKind;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use tempfile::TempDir;

const DENO_ICON_32: &[u8] = include_bytes!("./resources/deno-logo-32x32.png");
Expand Down Expand Up @@ -48,13 +49,39 @@ fn install_icon(
Ok(())
}

pub fn install() -> Result<(), AnyError> {
let temp_dir = TempDir::new().unwrap();
let kernel_json_path = temp_dir.path().join("kernel.json");
pub fn install(directory: Option<PathBuf>) -> Result<(), AnyError> {
let outcome = match directory {
Some(path) => install_via_directory_flag(&path),
None => install_via_jupyter(),
};

if outcome.is_err() {
bail!("🆘 Failed to install Deno kernel: {:?}", outcome.unwrap_err());
}

println!("✅ Deno kernelspec installed successfully.");
Ok(())
}

fn install_via_directory_flag(output_dir: &PathBuf) -> Result<(), AnyError> {
let mut dir = output_dir.clone();
if !dir.ends_with("deno") {
dir.push("deno");
}
Comment on lines +68 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good for the first pass 👍 for the future we might want to derive the name from the current version - eg. in Deno v1.43 this would add deno-v1.43 and for Deno v1.44.2 it would add deno-v1.44. That would make it very handy managing multiple Deno installations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bartlomieju I can try it out and confirm it works, if we want it later, may as well start with that behavior.


std::fs::create_dir_all(dir.as_path())?;
create_kernelspec_in_directory(&dir)?;

log::info!("📂 Installing to custom location: {}.", &dir.display());
Ok(())
}

fn create_kernelspec_in_directory(directory: &PathBuf) -> Result<(), AnyError> {
let kernel_json_path = directory.join("kernel.json");
// TODO(bartlomieju): add remaining fields as per
// https://jupyter-client.readthedocs.io/en/stable/kernels.html#kernel-specs
// FIXME(bartlomieju): replace `current_exe` before landing?

let json_data = json!({
"argv": [current_exe().unwrap().to_string_lossy(), "jupyter", "--kernel", "--conn", "{connection_file}"],
"display_name": "Deno",
Expand All @@ -63,9 +90,20 @@ pub fn install() -> Result<(), AnyError> {

let f = std::fs::File::create(kernel_json_path)?;
serde_json::to_writer_pretty(f, &json_data)?;
install_icon(temp_dir.path(), "logo-32x32.png", DENO_ICON_32)?;
install_icon(temp_dir.path(), "logo-64x64.png", DENO_ICON_64)?;
install_icon(temp_dir.path(), "logo-svg.svg", DENO_ICON_SVG)?;
install_icon(directory, "logo-32x32.png", DENO_ICON_32)?;
install_icon(directory, "logo-64x64.png", DENO_ICON_64)?;
install_icon(directory, "logo-svg.svg", DENO_ICON_SVG)?;
Ok(())
}

fn install_via_jupyter() -> Result<(), AnyError> {
let temp_dir = TempDir::new().unwrap();

let create_kernelspec_result =
create_kernelspec_in_directory(&temp_dir.path().to_path_buf());
if create_kernelspec_result.is_err() {
return create_kernelspec_result;
}

let child_result = std::process::Command::new("jupyter")
.args([
Expand Down Expand Up @@ -106,8 +144,6 @@ pub fn install() -> Result<(), AnyError> {
bail!("Failed to install kernelspec: {}", err);
}
}

let _ = std::fs::remove_dir(temp_dir);
println!("✅ Deno kernelspec installed successfully.");
Ok(())
}
2 changes: 1 addition & 1 deletion cli/tools/jupyter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn kernel(
}

if jupyter_flags.install {
install::install()?;
install::install(jupyter_flags.directory)?;
return Ok(());
}

Expand Down