diff options
author | Aarni Koskela <akx@iki.fi> | 2023-05-11 07:34:19 +0000 |
---|---|---|
committer | Aarni Koskela <akx@iki.fi> | 2023-05-11 08:57:41 +0000 |
commit | 875bc270093b4f89b9d0d20a93a5e8ea514b3f6c (patch) | |
tree | eac9ad3b3c8dc478127b2f5c986e24f79746f8f0 | |
parent | 49db24ce273ebccb598fc13de946d1ac3cad38f8 (diff) | |
download | stable-diffusion-webui-gfx803-875bc270093b4f89b9d0d20a93a5e8ea514b3f6c.tar.gz stable-diffusion-webui-gfx803-875bc270093b4f89b9d0d20a93a5e8ea514b3f6c.tar.bz2 stable-diffusion-webui-gfx803-875bc270093b4f89b9d0d20a93a5e8ea514b3f6c.zip |
launch.py: Simplify run()
-rw-r--r-- | launch.py | 42 |
1 files changed, 23 insertions, 19 deletions
@@ -88,32 +88,36 @@ def git_tag(): return stored_git_tag
-def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live):
+def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live) -> str:
if desc is not None:
print(desc)
- if live:
- result = subprocess.run(command, shell=True, env=os.environ if custom_env is None else custom_env)
- if result.returncode != 0:
- raise RuntimeError(f"""{errdesc or 'Error running command'}.
-Command: {command}
-Error code: {result.returncode}""")
+ run_kwargs = {
+ "args": command,
+ "shell": True,
+ "env": os.environ if custom_env is None else custom_env,
+ "encoding": 'utf8',
+ "errors": 'ignore',
+ }
- return ""
+ if not live:
+ run_kwargs["stdout"] = run_kwargs["stderr"] = subprocess.PIPE
- result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, env=os.environ if custom_env is None else custom_env)
+ result = subprocess.run(**run_kwargs)
if result.returncode != 0:
-
- message = f"""{errdesc or 'Error running command'}.
-Command: {command}
-Error code: {result.returncode}
-stdout: {result.stdout.decode(encoding="utf8", errors="ignore") if len(result.stdout)>0 else '<empty>'}
-stderr: {result.stderr.decode(encoding="utf8", errors="ignore") if len(result.stderr)>0 else '<empty>'}
-"""
- raise RuntimeError(message)
-
- return result.stdout.decode(encoding="utf8", errors="ignore")
+ error_bits = [
+ f"{errdesc or 'Error running command'}.",
+ f"Command: {command}",
+ f"Error code: {result.returncode}",
+ ]
+ if result.stdout:
+ error_bits.append(f"stdout: {result.stdout}")
+ if result.stderr:
+ error_bits.append(f"stderr: {result.stderr}")
+ raise RuntimeError("\n".join(error_bits))
+
+ return (result.stdout or "")
def check_run(command):
|