cli: use nix_shell also in ssh.py

This commit is contained in:
Jörg Thalheim
2023-07-26 08:12:37 +02:00
committed by Mic92
parent 24b9ded397
commit 9aa75336b7
2 changed files with 78 additions and 53 deletions

View File

@@ -3,6 +3,8 @@ import json
import subprocess import subprocess
from typing import Optional from typing import Optional
from .nix import nix_shell
def ssh( def ssh(
host: str, host: str,
@@ -10,15 +12,10 @@ def ssh(
password: Optional[str] = None, password: Optional[str] = None,
ssh_args: list[str] = [], ssh_args: list[str] = [],
) -> None: ) -> None:
nix_shell_args = [] packages = ["tor", "openssh"]
password_args = [] password_args = []
if password: if password:
nix_shell_args = [ packages.append("sshpass")
"nix",
"shell",
"nixpkgs#sshpass",
"-c",
]
password_args = [ password_args = [
"sshpass", "sshpass",
"-p", "-p",
@@ -32,23 +29,22 @@ def ssh(
"StrictHostKeyChecking=no", "StrictHostKeyChecking=no",
f"{user}@{host}", f"{user}@{host}",
] ]
cmd = nix_shell_args + ["torify"] + password_args + _ssh_args cmd = nix_shell(packages, ["torify"] + password_args + _ssh_args)
subprocess.run(cmd) subprocess.run(cmd)
def qrcode_scan(pictureFile: str) -> str: def qrcode_scan(pictureFile: str) -> str:
return ( return (
subprocess.run( subprocess.run(
nix_shell(
["zbar"],
[ [
"nix",
"shell",
"nixpkgs#zbar",
"-c",
"zbarimg", "zbarimg",
"--quiet", "--quiet",
"--raw", "--raw",
pictureFile, pictureFile,
], ],
),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
check=True, check=True,
) )

View File

@@ -1,5 +1,7 @@
import os
import sys import sys
from typing import Union from contextlib import contextmanager
from typing import Iterator, Union
import pytest import pytest
import pytest_subprocess.fake_process import pytest_subprocess.fake_process
@@ -18,11 +20,30 @@ def test_no_args(
assert captured.err.startswith("usage:") assert captured.err.startswith("usage:")
@contextmanager
def mock_env(**environ: str) -> Iterator[None]:
original_environ = dict(os.environ)
os.environ.update(environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(original_environ)
# using fp fixture from pytest-subprocess # using fp fixture from pytest-subprocess
def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None: def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
with mock_env(CLAN_NIXPKGS="/mocked-nixpkgs"):
host = "somehost" host = "somehost"
user = "user" user = "user"
cmd: list[Union[str, utils.Any]] = [ cmd: list[Union[str, utils.Any]] = [
"nix",
"shell",
"-f",
"/mocked-nixpkgs",
"tor",
"openssh",
"-c",
"torify", "torify",
"ssh", "ssh",
"-o", "-o",
@@ -41,13 +62,21 @@ def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
def test_ssh_with_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None: def test_ssh_with_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None:
with mock_env(CLAN_NIXPKGS="/mocked-nixpkgs"):
host = "somehost" host = "somehost"
user = "user" user = "user"
cmd: list[Union[str, utils.Any]] = [ cmd: list[Union[str, utils.Any]] = [
"nix", "nix",
"shell", "shell",
"nixpkgs#sshpass", "-f",
"/mocked-nixpkgs",
"tor",
"openssh",
"sshpass",
"-c", "-c",
"torify",
"sshpass",
"-p",
fp.any(), fp.any(),
] ]
fp.register(cmd) fp.register(cmd)