Skip to content

Commit

Permalink
spawn a process on all OS instead of executing
Browse files Browse the repository at this point in the history
  • Loading branch information
trappitsch committed Mar 18, 2024
1 parent 25dd116 commit 18f2b48
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
13 changes: 11 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@ If none are set then the `PYAPP_EXEC_MODULE` option will default to the value of

## Graphical user interface (GUI)

If you are packaging for windows on windows and your python execution calls a GUI, you can set `PYAPP_IS_GUI` to `true` or `1`. This will use `pythonw.exe` instead of `python.exe` to execute the application. If a GUI is run with `python.exe` on Windows, a console window will appear alongside the GUI. Details can be found [here](https://docs.python.org/3/using/windows.html#python-application).
If you are packaging a GUI, you can set `PYAPP_IS_GUI` to `true` or `1`.

Even when you set `PYAPP_IS_GUI` to `1`, you can still run the application from the command line. Furthermore, PyApp specific functions (e.g., installation and setup) will still display a console window with status messages. Note however that running a GUI application with `pythonw.exe` means that all `stdout` and `stderr` output will be discarded.
On windows, this will use `pythonw.exe` instead of `python.exe` to execute the application, which avoids a console window from appearing.
Note that running a GUI application with `pythonw.exe` means that all `stdout` and `stderr` output from your GUI will be discarded.

On unix-like systems, `python` will be used for the execution.
PyApp will run your GUI by spawning a new process, such that the console window that calls the command terminates after successful spawning.

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

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

## Python distribution

Expand Down
1 change: 0 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ pub fn pip_external() -> bool {
env!("PYAPP_PIP_EXTERNAL") == "1"
}

#[cfg(windows)]
pub fn app_is_gui() -> bool {
env!("PYAPP_IS_GUI") == "1"
}
Expand Down
Binary file modified src/embed/project
Binary file not shown.
34 changes: 17 additions & 17 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use std::io::Read;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
#[cfg(windows)]
// #[cfg(windows)]
use std::process::exit;
use std::process::{Command, ExitStatus};

use anyhow::Result;

use crate::terminal;

#[cfg(windows)]
use crate::app;
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 @@ -35,25 +32,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::app_is_gui() {
exec_gui(command)
} else {
Err(command.exec().into())
}
}

#[cfg(windows)]
pub fn exec(mut command: Command) -> Result<()> {
if app::app_is_gui() {
let mut child = command.spawn()?;
match child.try_wait() {
Ok(Some(status)) => {
exit(status.code().unwrap_or(1));
}
Ok(None) => {
// The child is still running
Ok(())
}
Err(e) => Err(e.into()),
}
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()),
}
}

0 comments on commit 18f2b48

Please sign in to comment.