Refactor subprocess to cmd.py part 1. Fixed clan_uri test.

This commit is contained in:
Qubasa
2024-01-02 16:34:10 +01:00
parent 69d08241e9
commit 3f55c688d9
5 changed files with 22 additions and 44 deletions

View File

@@ -1,17 +1,18 @@
import json import json
import subprocess
from pathlib import Path from pathlib import Path
from clan_cli.nix import nix_eval from clan_cli.nix import nix_eval
from .cmd import run
def get_clan_module_names( def get_clan_module_names(
flake_dir: Path, flake_dir: Path,
) -> tuple[list[str], str | None]: ) -> list[str]:
""" """
Get the list of clan modules from the clan-core flake input Get the list of clan modules from the clan-core flake input
""" """
proc = subprocess.run( proc = run(
nix_eval( nix_eval(
[ [
"--impure", "--impure",
@@ -25,11 +26,8 @@ def get_clan_module_names(
""", """,
], ],
), ),
capture_output=True,
text=True,
cwd=flake_dir, cwd=flake_dir,
) )
if proc.returncode != 0:
return [], proc.stderr
module_names = json.loads(proc.stdout) module_names = json.loads(proc.stdout)
return module_names, None return module_names

View File

@@ -1,5 +1,7 @@
import logging import logging
import shlex import shlex
import subprocess
import sys
from collections.abc import Callable from collections.abc import Callable
from pathlib import Path from pathlib import Path
from typing import Any, NamedTuple from typing import Any, NamedTuple
@@ -15,10 +17,6 @@ class CmdOut(NamedTuple):
cwd: Path | None = None cwd: Path | None = None
import subprocess
import sys
def run(cmd: list[str], cwd: Path = Path.cwd()) -> CmdOut: def run(cmd: list[str], cwd: Path = Path.cwd()) -> CmdOut:
# Start the subprocess # Start the subprocess
process = subprocess.Popen( process = subprocess.Popen(

View File

@@ -1,11 +1,9 @@
import argparse import argparse
import json import json
import shlex
import subprocess
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from ..errors import ClanError from ..cmd import run
from ..nix import nix_config, nix_eval from ..nix import nix_config, nix_eval
@@ -31,17 +29,8 @@ def inspect_vm(flake_url: str | Path, flake_attr: str) -> VmConfig:
] ]
) )
proc = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE) proc = run(cmd)
assert proc.stdout is not None
if proc.returncode != 0:
raise ClanError(
f"""
command: {shlex.join(cmd)}
exit code: {proc.returncode}
stdout:
{proc.stdout}
"""
)
data = json.loads(proc.stdout) data = json.loads(proc.stdout)
return VmConfig(flake_url=flake_url, flake_attr=flake_attr, **data) return VmConfig(flake_url=flake_url, flake_attr=flake_attr, **data)

View File

@@ -10,6 +10,7 @@ from dataclasses import dataclass, field
from pathlib import Path from pathlib import Path
from typing import IO from typing import IO
from ..cmd import run
from ..dirs import module_root, specific_groot_dir from ..dirs import module_root, specific_groot_dir
from ..errors import ClanError from ..errors import ClanError
from ..nix import nix_build, nix_config, nix_shell from ..nix import nix_build, nix_config, nix_shell
@@ -20,9 +21,10 @@ log = logging.getLogger(__name__)
def get_qemu_version() -> list[int]: def get_qemu_version() -> list[int]:
# Run the command and capture the output # Run the command and capture the output
output = subprocess.check_output(["qemu-kvm", "--version"]) res = run(["qemu-kvm", "--version"])
# Decode the output from bytes to string # Decode the output from bytes to string
output_str = output.decode("utf-8") output_str = res.stdout
# Split the output by newline and get the first line # Split the output by newline and get the first line
first_line = output_str.split("\n")[0] first_line = output_str.split("\n")[0]
# Split the first line by space and get the third element # Split the first line by space and get the third element
@@ -39,7 +41,7 @@ def graphics_options(vm: VmConfig) -> list[str]:
# Check if the version is greater than 8.1.3 to enable virtio audio # Check if the version is greater than 8.1.3 to enable virtio audio
# if get_qemu_version() > [8, 1, 3]: # if get_qemu_version() > [8, 1, 3]:
# common = ["-audio", "driver=pa,model=virtio"] # common = ["-audio", "driver=pa,model=virtio"]
if vm.wayland: if vm.wayland:
# fmt: off # fmt: off
@@ -135,16 +137,7 @@ def get_vm_create_info(vm: VmConfig, nix_options: list[str]) -> dict[str, str]:
specific_groot_dir(clan_name=vm.clan_name, flake_url=str(vm.flake_url)) specific_groot_dir(clan_name=vm.clan_name, flake_url=str(vm.flake_url))
/ f"vm-{machine}", / f"vm-{machine}",
) )
proc = subprocess.run( proc = run(cmd)
cmd,
check=False,
stdout=subprocess.PIPE,
text=True,
)
if proc.returncode != 0:
raise ClanError(
f"Failed to build vm config: {shlex.join(cmd)} failed with: {proc.returncode}"
)
try: try:
return json.loads(Path(proc.stdout.strip()).read_text()) return json.loads(Path(proc.stdout.strip()).read_text())
except json.JSONDecodeError as e: except json.JSONDecodeError as e:

View File

@@ -9,7 +9,7 @@ def test_get_internal() -> None:
assert uri.get_internal() == "https://example.com?password=1234" assert uri.get_internal() == "https://example.com?password=1234"
uri = ClanURI("clan://~/Downloads") uri = ClanURI("clan://~/Downloads")
assert uri.get_internal() == "~/Downloads" assert uri.get_internal().endswith("/Downloads")
uri = ClanURI("clan:///home/user/Downloads") uri = ClanURI("clan:///home/user/Downloads")
assert uri.get_internal() == "/home/user/Downloads" assert uri.get_internal() == "/home/user/Downloads"
@@ -42,7 +42,7 @@ def test_is_remote() -> None:
def test_direct_local_path() -> None: def test_direct_local_path() -> None:
# Create a ClanURI object from a remote URI # Create a ClanURI object from a remote URI
uri = ClanURI("clan://~/Downloads") uri = ClanURI("clan://~/Downloads")
assert uri.get_internal() == "~/Downloads" assert uri.get_internal().endswith("/Downloads")
def test_direct_local_path2() -> None: def test_direct_local_path2() -> None:
@@ -109,17 +109,17 @@ def test_from_str() -> None:
params = ClanParameters(flake_attr="myVM") params = ClanParameters(flake_attr="myVM")
uri = ClanURI.from_str(url=uri_str, params=params) uri = ClanURI.from_str(url=uri_str, params=params)
assert uri.params.flake_attr == "myVM" assert uri.params.flake_attr == "myVM"
assert uri.get_internal() == "~/Downloads/democlan" assert uri.get_internal().endswith("/Downloads/democlan")
uri_str = "~/Downloads/democlan" uri_str = "~/Downloads/democlan"
uri = ClanURI.from_str(url=uri_str) uri = ClanURI.from_str(url=uri_str)
assert uri.params.flake_attr == "defaultVM" assert uri.params.flake_attr == "defaultVM"
assert uri.get_internal() == "~/Downloads/democlan" assert uri.get_internal().endswith("/Downloads/democlan")
uri_str = "clan://~/Downloads/democlan" uri_str = "clan://~/Downloads/democlan"
uri = ClanURI.from_str(url=uri_str) uri = ClanURI.from_str(url=uri_str)
assert uri.params.flake_attr == "defaultVM" assert uri.params.flake_attr == "defaultVM"
assert uri.get_internal() == "~/Downloads/democlan" assert uri.get_internal().endswith("/Downloads/democlan")
def test_remote_with_all_params() -> None: def test_remote_with_all_params() -> None: