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_IS_GUI option #97

Merged
merged 7 commits into from
Mar 24, 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 @@ -58,6 +58,7 @@ passthrough = [
"PYAPP_EXPOSE_PYTHON",
"PYAPP_EXPOSE_PYTHON_PATH",
"PYAPP_FULL_ISOLATION",
"PYAPP_IS_GUI",
"PYAPP_METADATA_TEMPLATE",
"PYAPP_PASS_LOCATION",
"PYAPP_PIP_ALLOW_CONFIG",
Expand Down
10 changes: 10 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,15 @@ fn set_execution_mode() {
}
}

fn set_is_gui() {
let variable = "PYAPP_IS_GUI";
if is_enabled(variable) {
set_runtime_variable(variable, "1");
} else {
set_runtime_variable(variable, "0");
}
}

fn set_isolation_mode() {
let variable = "PYAPP_FULL_ISOLATION";
if is_enabled(variable) {
Expand Down Expand Up @@ -931,6 +940,7 @@ fn main() {
set_project();
set_distribution();
set_execution_mode();
set_is_gui();
set_isolation_mode();
set_upgrade_virtualenv();
set_pip_external();
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

***Added:***

- Add `PYAPP_IS_GUI` option to support graphical applications

## 0.15.1 - 2024-03-03

***Fixed:***
Expand Down
13 changes: 13 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ The following options are mutually exclusive:

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

### GUI

If you are packaging a graphical user interface (GUI), you can set `PYAPP_IS_GUI` to `true` or `1`.

On Windows, this will use `pythonw.exe` instead of `python.exe` to execute [the application](https://docs.python.org/3/using/windows.html#python-application), which avoids a console window from appearing. Running a GUI application with `pythonw.exe` means that all `stdout` and `stderr` output from your GUI will be discarded.

Otherwise, the application will execute as usual. PyApp will run your GUI by spawning a new process, such that the console window that runs the application terminates after successful spawning.

Even when `PYAPP_IS_GUI` is enabled you can still run the application from the command line. Furthermore, PyApp-specific logic (e.g. installation and setup) will still display a console window with status messages.

!!! note
On macOS, the console by default does not automatically close when processes have terminated (however it can be closed manually without interferring with the GUI). The default console behavior [can be changed](https://stackoverflow.com/questions/5560167/osx-how-to-auto-close-terminal-window-after-the-exit-command-executed) in the user settings to close after the last process terminates successfully.

## Python distribution

### Known
Expand Down
15 changes: 15 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ fn installation_python_path() -> String {
env!("PYAPP__INSTALLATION_PYTHON_PATH").into()
}

#[cfg(windows)]
fn installation_pythonw_path() -> String {
let python_path: String = env!("PYAPP__INSTALLATION_PYTHON_PATH").into();
python_path.replace("python.exe", "pythonw.exe")
}

fn installation_site_packages_path() -> String {
env!("PYAPP__INSTALLATION_SITE_PACKAGES_PATH").into()
}
Expand Down Expand Up @@ -180,6 +186,10 @@ pub fn pip_external() -> bool {
env!("PYAPP_PIP_EXTERNAL") == "1"
}

pub fn is_gui() -> bool {
env!("PYAPP_IS_GUI") == "1"
}

pub fn full_isolation() -> bool {
env!("PYAPP_FULL_ISOLATION") == "1"
}
Expand All @@ -204,6 +214,11 @@ pub fn python_path() -> PathBuf {
install_dir().join(installation_python_path())
}

#[cfg(windows)]
pub fn pythonw_path() -> PathBuf {
install_dir().join(installation_pythonw_path())
}

pub fn site_packages_path() -> PathBuf {
install_dir().join(installation_site_packages_path())
}
Expand Down
7 changes: 7 additions & 0 deletions src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ pub fn python_command(python: &PathBuf) -> Command {
pub fn run_project() -> Result<()> {
let mut command = python_command(&app::python_path());

#[cfg(windows)]
{
if app::is_gui() {
command = python_command(&app::pythonw_path());
}
}

if !app::exec_code().is_empty() {
command.args(["-c", app::exec_code().as_str()]);
} else if !app::exec_module().is_empty() {
Expand Down
26 changes: 21 additions & 5 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::io::Read;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
#[cfg(windows)]
use std::process::exit;
use std::process::{Command, ExitStatus};

use anyhow::Result;

use crate::terminal;
use crate::{app, terminal};

pub fn wait_for(mut command: Command, message: String) -> Result<(ExitStatus, String)> {
let (mut reader, writer_stdout) = os_pipe::pipe()?;
Expand All @@ -32,11 +31,28 @@ pub fn wait_for(mut command: Command, message: String) -> Result<(ExitStatus, St

#[cfg(unix)]
pub fn exec(mut command: Command) -> Result<()> {
Err(command.exec().into())
if app::is_gui() {
exec_gui(command)
} else {
Err(command.exec().into())
}
}

#[cfg(windows)]
pub fn exec(mut command: Command) -> Result<()> {
let status = command.status()?;
exit(status.code().unwrap_or(1));
if app::is_gui() {
exec_gui(command)
} else {
let status = command.status()?;
exit(status.code().unwrap_or(1));
}
}

fn exec_gui(mut command: Command) -> Result<()> {
let mut child = command.spawn()?;
match child.try_wait() {
Ok(Some(status)) => exit(status.code().unwrap_or(1)),
Ok(None) => Ok(()), // The child is still running
Err(e) => Err(e.into()),
}
}