-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Fabrice Normandin <[email protected]>
- Loading branch information
Showing
6 changed files
with
506 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,73 @@ | ||
from __future__ import annotations | ||
|
||
import shlex | ||
import subprocess | ||
from subprocess import CompletedProcess | ||
from typing import IO, Any | ||
|
||
from typing_extensions import deprecated | ||
|
||
from .utils import CommandNotFoundError, T, shjoin | ||
|
||
|
||
class Local: | ||
def display(self, args): | ||
print(T.bold_green(f"(local) $ ", shjoin(args))) | ||
def display(self, args: list[str] | tuple[str, ...]) -> None: | ||
print(T.bold_green("(local) $ ", shjoin(args))) | ||
|
||
def silent_get(self, *args, **kwargs): | ||
return subprocess.check_output( | ||
args, | ||
universal_newlines=True, | ||
**kwargs, | ||
) | ||
def silent_get(self, *cmd: str) -> str: | ||
return subprocess.check_output(cmd, universal_newlines=True) | ||
|
||
def get(self, *args, **kwargs): | ||
self.display(args) | ||
return subprocess.check_output( | ||
args, | ||
universal_newlines=True, | ||
**kwargs, | ||
) | ||
@deprecated("This isn't used and will probably be removed. Don't start using it.") | ||
def get(self, *cmd: str) -> str: | ||
self.display(cmd) | ||
return subprocess.check_output(cmd, universal_newlines=True) | ||
|
||
def run(self, *args, **kwargs): | ||
self.display(args) | ||
def run( | ||
self, | ||
*cmd: str, | ||
stdout: int | IO[Any] | None = None, | ||
stderr: int | IO[Any] | None = None, | ||
capture_output: bool = False, | ||
) -> CompletedProcess[str]: | ||
self.display(cmd) | ||
try: | ||
return subprocess.run( | ||
args, | ||
cmd, | ||
stdout=stdout, | ||
stderr=stderr, | ||
capture_output=capture_output, | ||
universal_newlines=True, | ||
**kwargs, | ||
) | ||
except FileNotFoundError as e: | ||
if e.filename == args[0]: | ||
raise CommandNotFoundError(e.filename) | ||
else: | ||
raise | ||
if e.filename == cmd[0]: | ||
raise CommandNotFoundError(e.filename) from e | ||
raise | ||
|
||
def popen(self, *args, **kwargs): | ||
self.display(args) | ||
def popen( | ||
self, | ||
*cmd: str, | ||
stdout: int | IO[Any] | None = None, | ||
stderr: int | IO[Any] | None = None, | ||
) -> subprocess.Popen: | ||
self.display(cmd) | ||
return subprocess.Popen( | ||
args, | ||
universal_newlines=True, | ||
**kwargs, | ||
cmd, stdout=stdout, stderr=stderr, universal_newlines=True | ||
) | ||
|
||
def check_passwordless(self, host): | ||
def check_passwordless(self, host: str) -> bool: | ||
results = self.run( | ||
"ssh", | ||
"-oPreferredAuthentications=publickey", | ||
host, | ||
"echo OK", | ||
*shlex.split(f"ssh -oPreferredAuthentications=publickey {host} 'echo OK'"), | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
if results.returncode != 0: | ||
if "Permission denied" in results.stderr: | ||
return False | ||
else: | ||
print(results.stdout) | ||
print(results.stderr) | ||
exit(f"Failed to connect to {host}, could not understand error") | ||
print(results.stdout) | ||
print(results.stderr) | ||
exit(f"Failed to connect to {host}, could not understand error") | ||
# TODO: Perhaps we could actually check the output of the command here! | ||
# elif "OK" in results.stdout: | ||
else: | ||
print("# OK") | ||
return True |
Oops, something went wrong.