Merge pull request 'Wrapped vm.run in cmd.run. Working --wayland' (#745) from Qubasa-main into main
This commit is contained in:
@@ -2,8 +2,6 @@ import argparse
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import shlex
|
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
@@ -235,16 +233,12 @@ def run_vm(
|
|||||||
"XDG_DATA_DIRS"
|
"XDG_DATA_DIRS"
|
||||||
] = f"{remote_viewer_mimetypes}:{env.get('XDG_DATA_DIRS', '')}"
|
] = f"{remote_viewer_mimetypes}:{env.get('XDG_DATA_DIRS', '')}"
|
||||||
|
|
||||||
print("$ " + shlex.join(qemu_cmd))
|
run(
|
||||||
res = subprocess.run(
|
|
||||||
nix_shell(packages, qemu_cmd),
|
nix_shell(packages, qemu_cmd),
|
||||||
env=env,
|
env=env,
|
||||||
check=False,
|
log=Log.BOTH,
|
||||||
stdout=log_fd,
|
error_msg=f"Could not start vm {machine}",
|
||||||
stderr=log_fd,
|
|
||||||
)
|
)
|
||||||
if res.returncode != 0:
|
|
||||||
raise ClanError(f"qemu failed with {res.returncode}")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import os
|
|||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
import weakref
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -134,59 +133,3 @@ def spawn(
|
|||||||
mp_proc = MPProcess(name=proc_name, proc=proc, out_file=out_file)
|
mp_proc = MPProcess(name=proc_name, proc=proc, out_file=out_file)
|
||||||
|
|
||||||
return mp_proc
|
return mp_proc
|
||||||
|
|
||||||
|
|
||||||
# Processes are killed when the ProcessManager is garbage collected
|
|
||||||
class ProcessManager:
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self.procs: dict[str, MPProcess] = dict()
|
|
||||||
self._finalizer = weakref.finalize(self, self.kill_all)
|
|
||||||
|
|
||||||
def by_pid(self, pid: int) -> tuple[str, MPProcess] | None:
|
|
||||||
for ident, proc in self.procs.items():
|
|
||||||
if proc.proc.pid == pid:
|
|
||||||
return (ident, proc)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def by_proc(self, proc: mp.process.BaseProcess) -> tuple[str, MPProcess] | None:
|
|
||||||
if proc.pid is None:
|
|
||||||
return None
|
|
||||||
return self.by_pid(pid=proc.pid)
|
|
||||||
|
|
||||||
def running_procs(self) -> list[str]:
|
|
||||||
alive_procs = filter(lambda pair: pair[1].proc.is_alive(), self.procs.items())
|
|
||||||
self.procs = dict(alive_procs)
|
|
||||||
return list(self.procs.keys())
|
|
||||||
|
|
||||||
def spawn(
|
|
||||||
self,
|
|
||||||
*,
|
|
||||||
ident: str,
|
|
||||||
log_path: Path,
|
|
||||||
on_except: Callable[[Exception, mp.process.BaseProcess], None],
|
|
||||||
func: Callable,
|
|
||||||
**kwargs: Any,
|
|
||||||
) -> MPProcess:
|
|
||||||
proc = spawn(
|
|
||||||
log_path=log_path,
|
|
||||||
on_except=on_except,
|
|
||||||
func=func,
|
|
||||||
**kwargs,
|
|
||||||
)
|
|
||||||
if ident in self.procs:
|
|
||||||
raise ClanError(f"Process with id {ident} already exists")
|
|
||||||
self.procs[ident] = proc
|
|
||||||
return proc
|
|
||||||
|
|
||||||
def kill_all(self) -> None:
|
|
||||||
print("Killing all processes", file=sys.stderr)
|
|
||||||
for proc in self.procs.values():
|
|
||||||
proc.kill_group()
|
|
||||||
self.procs.clear()
|
|
||||||
|
|
||||||
def kill(self, ident: str) -> None:
|
|
||||||
if ident not in self.procs:
|
|
||||||
raise ClanError(f"Process with id {ident} does not exist")
|
|
||||||
proc = self.procs[ident]
|
|
||||||
proc.kill_group()
|
|
||||||
del self.procs[ident]
|
|
||||||
|
|||||||
Reference in New Issue
Block a user