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 PYAPP_EXEC_SCRIPT option #46

Merged
merged 1 commit into from
Jun 26, 2023
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 @@ -49,6 +49,7 @@ passthrough = [
"PYAPP_DISTRIBUTION_VARIANT",
"PYAPP_EXEC_CODE",
"PYAPP_EXEC_MODULE",
"PYAPP_EXEC_SCRIPT",
"PYAPP_EXEC_SPEC",
"PYAPP_EXPOSE_METADATA",
"PYAPP_EXPOSE_PIP",
Expand Down
40 changes: 33 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,17 +651,28 @@ fn set_execution_mode() {
let code_variable = "PYAPP_EXEC_CODE";
let code = env::var(code_variable).unwrap_or_default();

let script_variable = "PYAPP_EXEC_SCRIPT";
let script = env::var(script_variable).unwrap_or_default();

// Set defaults
set_runtime_variable(module_variable, "");
set_runtime_variable(code_variable, "");

if [module.is_empty(), spec.is_empty(), code.is_empty()]
.iter()
.filter(|x| !(**x))
.count()
set_runtime_variable(script_variable, "");
set_runtime_variable("PYAPP__EXEC_SCRIPT_NAME", "");
set_runtime_variable("PYAPP__EXEC_SCRIPT_ID", "");

if [
module.is_empty(),
spec.is_empty(),
code.is_empty(),
script.is_empty(),
]
.iter()
.filter(|x| !(**x))
.count()
> 1
{
panic!("\n\nThe {module_variable}, {spec_variable}, and {code_variable} options are mutually exclusive\n\n");
panic!("\n\nThe {module_variable}, {spec_variable}, {code_variable}, and {script_variable} options are mutually exclusive\n\n");
} else if !module.is_empty() {
set_runtime_variable(module_variable, &module);
} else if !spec.is_empty() {
Expand All @@ -672,7 +683,22 @@ fn set_execution_mode() {
format!("import {module};{module}.{object}()"),
);
} else if !code.is_empty() {
set_runtime_variable(code_variable, STANDARD_NO_PAD.encode(&code));
set_runtime_variable(code_variable, STANDARD_NO_PAD.encode(code));
} else if !script.is_empty() {
let path = PathBuf::from(&script);
if !path.is_file() {
panic!("\n\nScript is not a file: {script}\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 script: {script}\n\n"));
let mut hasher = PortableHash::default();
hasher.write(contents.as_bytes());

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 {
set_runtime_variable(
module_variable,
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

***Added:***

- Add `PYAPP_EXEC_SCRIPT` option for executing a project using a script
- Add support for overriding the installation directory

***Fixed:***
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The following options are mutually exclusive:
| `PYAPP_EXEC_MODULE` | This is the name of the module to execute via `python -m <MODULE>` |
| `PYAPP_EXEC_SPEC` | This is an [object reference](https://packaging.python.org/en/latest/specifications/entry-points/#data-model) to execute e.g. `pkg.foo:cli` |
| `PYAPP_EXEC_CODE` | This is arbitrary code to run via `python -c <CODE>` (the spec option uses this internally) |
| `PYAPP_EXEC_SCRIPT` | This is a path to a script to embed in the binary and run |

If none are set then the `PYAPP_EXEC_MODULE` option will default to the value of `PYAPP_PROJECT_NAME` with hyphens replaced by underscores.

Expand Down
11 changes: 11 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ pub fn exec_code() -> String {
decode_option(env!("PYAPP_EXEC_CODE"))
}

pub fn exec_script() -> String {
decode_option(env!("PYAPP_EXEC_SCRIPT"))
}

pub fn exec_script_path() -> PathBuf {
cache_dir()
.join("scripts")
.join(env!("PYAPP__EXEC_SCRIPT_ID"))
.join(env!("PYAPP__EXEC_SCRIPT_NAME"))
}

pub fn pip_extra_args() -> String {
env!("PYAPP_PIP_EXTRA_ARGS").into()
}
Expand Down
19 changes: 17 additions & 2 deletions src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ pub fn python_command(python: &PathBuf) -> Command {
pub fn run_project() -> Result<()> {
let mut command = python_command(&app::python_path());

if app::exec_module().is_empty() {
if !app::exec_code().is_empty() {
command.args(["-c", app::exec_code().as_str()]);
} else {
} else if !app::exec_module().is_empty() {
command.args(["-m", app::exec_module().as_str()]);
} else {
let script_path = app::exec_script_path();
if !script_path.is_file() {
let script_directory = script_path.parent().unwrap();
fs::create_dir_all(script_directory).with_context(|| {
format!(
"unable to create script cache directory {}",
&script_directory.display()
)
})?;
fs::write(&script_path, app::exec_script()).with_context(|| {
format!("unable to write project script {}", &script_path.display())
})?;
}
command.arg(script_path);
}
command.args(env::args().skip(1));

Expand Down