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

Add notebook_exec option #82

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ passthrough = [
"PYAPP_DISTRIBUTION_VARIANT",
"PYAPP_EXEC_CODE",
"PYAPP_EXEC_MODULE",
"PYAPP_EXEC_NOTEBOOK",
"PYAPP_EXEC_SCRIPT",
"PYAPP_EXEC_SPEC",
"PYAPP_EXPOSE_METADATA",
Expand Down
24 changes: 23 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,26 +701,33 @@ fn set_execution_mode() {
let script_variable = "PYAPP_EXEC_SCRIPT";
let script = env::var(script_variable).unwrap_or_default();

let notebook_variable = "PYAPP_EXEC_NOTEBOOK";
let notebook = env::var(notebook_variable).unwrap_or_default();

// Set defaults
set_runtime_variable(module_variable, "");
set_runtime_variable(code_variable, "");
set_runtime_variable("PYAPP__EXEC_CODE_ENCODED", "0");
set_runtime_variable(script_variable, "");
set_runtime_variable("PYAPP__EXEC_SCRIPT_NAME", "");
set_runtime_variable("PYAPP__EXEC_SCRIPT_ID", "");
set_runtime_variable(notebook_variable, "");
set_runtime_variable("PYAPP__EXEC_NOTEBOOK_NAME", "");
set_runtime_variable("PYAPP__EXEC_NOTEBOOK_ID", "");

if [
module.is_empty(),
spec.is_empty(),
code.is_empty(),
script.is_empty(),
notebook.is_empty(),
]
.iter()
.filter(|x| !(**x))
.count()
> 1
{
panic!("\n\nThe {module_variable}, {spec_variable}, {code_variable}, and {script_variable} options are mutually exclusive\n\n");
panic!("\n\nThe {module_variable}, {spec_variable}, {code_variable}, {script_variable}, and {notebook_variable} options are mutually exclusive\n\n");
} else if !module.is_empty() {
set_runtime_variable(module_variable, &module);
} else if !spec.is_empty() {
Expand Down Expand Up @@ -748,6 +755,21 @@ fn set_execution_mode() {
set_runtime_variable(script_variable, STANDARD_NO_PAD.encode(contents));
set_runtime_variable("PYAPP__EXEC_SCRIPT_NAME", file_name);
set_runtime_variable("PYAPP__EXEC_SCRIPT_ID", hasher.finish());
} else if !notebook.is_empty() {
let path = PathBuf::from(&notebook);
if !path.is_file() {
panic!("\n\nNotebook is not a file: {notebook}\n\n");
}

let file_name = path.file_name().unwrap().to_str().unwrap();
let contents = fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("\n\nFailed to read notebook: {notebook}\n\n"));
let mut hasher = PortableHash::default();
hasher.write(contents.as_bytes());

set_runtime_variable(notebook_variable, STANDARD_NO_PAD.encode(contents));
set_runtime_variable("PYAPP__EXEC_NOTEBOOK_NAME", file_name);
set_runtime_variable("PYAPP__EXEC_NOTEBOOK_ID", hasher.finish());
} else {
set_runtime_variable(
module_variable,
Expand Down
11 changes: 11 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ pub fn exec_script_path() -> PathBuf {
.join(env!("PYAPP__EXEC_SCRIPT_NAME"))
}

pub fn exec_notebook() -> String {
decode_option(env!("PYAPP_EXEC_NOTEBOOK"))
}

pub fn exec_notebook_path() -> PathBuf {
cache_dir()
.join("notebooks")
.join(env!("PYAPP__EXEC_NOTEBOOK_ID"))
.join(env!("PYAPP__EXEC_NOTEBOOK_NAME"))
}

pub fn pip_extra_args() -> String {
env!("PYAPP_PIP_EXTRA_ARGS").into()
}
Expand Down
23 changes: 22 additions & 1 deletion src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn run_project() -> Result<()> {
command.args(["-c", app::exec_code().as_str()]);
} else if !app::exec_module().is_empty() {
command.args(["-m", app::exec_module().as_str()]);
} else {
} else if !app::exec_script().is_empty() {
let script_path = app::exec_script_path();
if !script_path.is_file() {
let script_directory = script_path.parent().unwrap();
Expand All @@ -38,6 +38,27 @@ pub fn run_project() -> Result<()> {
})?;
}
command.arg(script_path);
} else {
let notebook_path = app::exec_notebook_path();
if !notebook_path.is_file() {
let notebook_directory = notebook_path.parent().unwrap();
fs::create_dir_all(notebook_directory).with_context(|| {
format!(
"unable to create notebook cache directory {}",
&notebook_directory.display()
)
})?;
fs::write(&notebook_path, app::exec_notebook()).with_context(|| {
format!(
"unable to write project notebook {}",
&notebook_path.display()
)
})?;
}
command
.arg("-m")
.arg("notebook")
.arg(notebook_path.to_str().unwrap());
}
command.args(env::args().skip(1));

Expand Down
Loading