Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making improvements to the consistency and functionality of the shell code #31

Closed
wants to merge 11 commits into from
Closed
6 changes: 3 additions & 3 deletions benchkit/adb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ def _devices() -> Iterable[ADBDevice]:
def _host_shell_out(
command: Command,
timeout: Optional[int] = None,
print_input: bool = False,
print_command: bool = False,
print_output: bool = False,
) -> str:
output = shell_out(
command=command,
timeout=timeout,
print_input=print_input,
print_command=print_command,
print_output=print_output,
)
return output
Expand Down Expand Up @@ -335,7 +335,7 @@ def wait_for(
command = ["adb", f"wait-for{transport_str}-{state}"]
self._host_shell_out(
command=command,
print_input=True,
print_command=True,
print_output=True,
)

Expand Down
2 changes: 1 addition & 1 deletion benchkit/adb/fastboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def flash(
shell_out(
command=f"fastboot flash {partition} ./{filename}",
current_dir=filedir,
print_input=True,
print_command=True,
print_output=True,
output_is_log=True,
)
Expand Down
6 changes: 3 additions & 3 deletions benchkit/adb/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def usbdevice_from_line(line: str) -> UsbDevice:

output = shell_out(
command="lsusb",
print_input=False,
print_command=False,
print_output=False,
).strip()
devices = [usbdevice_from_line(line=line.strip()) for line in output.splitlines()]
Expand Down Expand Up @@ -121,14 +121,14 @@ def usb_down_up() -> None:
shell_out(
command=f"sudo tee {auth_path}",
std_input="0",
print_input=False,
print_command=False,
print_output=False,
)
time.sleep(1)
shell_out(
command=f"sudo tee {auth_path}",
std_input="1",
print_input=False,
print_command=False,
print_output=False,
)
time.sleep(3)
2 changes: 1 addition & 1 deletion benchkit/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ def git_command(command):
output = self.platform.comm.shell(
command=command,
current_dir=self.bench_src_path,
print_input=False,
print_command=False,
print_output=False,
)
except CalledProcessError:
Expand Down
6 changes: 3 additions & 3 deletions benchkit/commandwrappers/perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _which(executable: str) -> Optional[PathType]:

try:
result = shell_out(
command=f"which {executable}", print_input=False, print_output=False
command=f"which {executable}", print_command=False, print_output=False
).strip()
except subprocess.CalledProcessError:
pass
Expand All @@ -79,7 +79,7 @@ def _find_perf_bin(search_path: Optional[PathType]) -> PathType:
result = None
kernel = shell_out(
"uname -r",
print_input=False,
print_command=False,
print_output=False,
).strip()

Expand Down Expand Up @@ -114,7 +114,7 @@ def _get_available_events(
) -> Tuple[List[PerfEvent], Dict[str, Dict[PerfEvent, str]]]:
raw_output = shell_out(
command=f"{perf_bin} list --no-desc",
print_input=False,
print_command=False,
print_output=False,
)
events = []
Expand Down
60 changes: 39 additions & 21 deletions benchkit/communication/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def shell(
current_dir: PathType | None = None,
environment: Environment = None,
shell: bool = False,
print_input: bool = True,
print_command: bool = True,
print_output: bool = True,
print_curdir: bool = True,
timeout: int | None = None,
Expand All @@ -80,7 +80,7 @@ def shell(
environment to pass to the command to run. Defaults to None.
shell (bool, optional):
whether a shell must be created to run the command. Defaults to False.
print_input (bool, optional):
print_command (bool, optional):
whether to print the command on benchkit logs. Defaults to True.
print_output (bool, optional):
whether to print the command output on benchkit logs. Defaults to True.
Expand Down Expand Up @@ -109,7 +109,7 @@ def shell_succeed(
current_dir: PathType | None = None,
environment: Environment = None,
shell: bool = False,
print_input: bool = True,
print_command: bool = True,
print_output: bool = True,
print_curdir: bool = True,
timeout: int | None = None,
Expand All @@ -130,7 +130,7 @@ def shell_succeed(
environment to pass to the command to run. Defaults to None.
shell (bool, optional):
whether a shell must be created to run the command. Defaults to False.
print_input (bool, optional):
print_command (bool, optional):
whether to print the command on benchkit logs. Defaults to True.
print_output (bool, optional):
whether to print the command output on benchkit logs. Defaults to True.
Expand Down Expand Up @@ -158,7 +158,7 @@ def shell_succeed(
current_dir=current_dir,
environment=environment,
shell=shell,
print_input=print_input,
print_command=print_command,
print_output=print_output,
print_curdir=print_curdir,
timeout=timeout,
Expand Down Expand Up @@ -337,7 +337,7 @@ def hostname(self) -> str:
"""
result = self.shell(
command="hostname",
print_input=False,
print_command=False,
print_output=False,
).strip()
return result
Expand All @@ -350,7 +350,7 @@ def current_user(self) -> str:
"""
result = self.shell(
command="whoami",
print_input=False,
print_command=False,
print_output=False,
).strip()
return result
Expand All @@ -367,7 +367,7 @@ def realpath(self, path: PathType) -> pathlib.Path:
"""
output = self.shell(
command=f"readlink -fm {path}",
print_input=False,
print_command=False,
print_output=False,
).strip()
result = pathlib.Path(output)
Expand Down Expand Up @@ -396,7 +396,21 @@ def makedirs(self, path: PathType, exist_ok: bool) -> None:
exist_opt = " -p " if exist_ok else ""
self.shell(
command=f"mkdir{exist_opt} {path}",
print_input=False,
print_command=False,
print_output=False,
)

def remove(self, path: PathType, recursive: bool) -> None:
"""Remove a file or directory on the target host.

Args:
path (PathType): path of file or directory that needs to be removed on the target host.
recursive (bool): whether to recursively delete everything in this path.
"""
command = ["rm"] + (["-r"] if recursive else []) + [str(path)]
self.shell(
command=command,
print_command=False,
print_output=False,
)

Expand Down Expand Up @@ -439,7 +453,7 @@ def which(self, cmd: str) -> pathlib.Path | None:
command = f"which {cmd}"
which_succeed = self.shell_succeed(
command=command,
print_input=False,
print_command=False,
print_output=False,
)

Expand All @@ -448,7 +462,7 @@ def which(self, cmd: str) -> pathlib.Path | None:

path = self.shell(
command=command,
print_input=False,
print_command=False,
print_output=False,
).strip()

Expand All @@ -465,7 +479,7 @@ def _bracket_test(
) -> bool:
succeed = True
try:
self.shell(command=f"[ {opt} {path} ]", print_input=False, print_output=False)
self.shell(command=f"[ {opt} {path} ]", print_command=False, print_output=False)
except subprocess.CalledProcessError as cpe:
if 1 != cpe.returncode:
raise cpe
Expand Down Expand Up @@ -497,25 +511,27 @@ def shell(
current_dir: PathType | None = None,
environment: Environment = None,
shell: bool = False,
print_input: bool = True,
print_command: bool = True,
print_output: bool = True,
print_curdir: bool = True,
timeout: int | None = None,
output_is_log: bool = False,
ignore_ret_codes: Iterable[int] = (),
split_arguments: bool = True,
) -> str:
return shell_out(
command=command,
std_input=std_input,
current_dir=current_dir,
environment=environment,
shell=shell,
print_input=print_input,
print_command=print_command,
print_output=print_output,
print_curdir=print_curdir,
timeout=timeout,
output_is_log=output_is_log,
ignore_ret_codes=ignore_ret_codes,
split_arguments=split_arguments,
)

def background_subprocess(
Expand Down Expand Up @@ -557,7 +573,7 @@ def get_process_status(
pid = process_handle.pid
status = shell_out(
f"ps -q {pid} -o state --no-headers",
print_input=False,
print_command=False,
print_output=False,
print_env=False,
print_curdir=False,
Expand Down Expand Up @@ -691,12 +707,13 @@ def shell(
current_dir: PathType | None = None,
environment: Environment = None,
shell: bool = False,
print_input: bool = True,
print_command: bool = True,
print_output: bool = True,
print_curdir: bool = True,
timeout: int | None = None,
output_is_log: bool = False,
ignore_ret_codes: Iterable[int] = (),
split_arguments: bool = True,
) -> str:
env_command = command_with_env(
command=command,
Expand All @@ -712,11 +729,12 @@ def shell(
command=full_command,
std_input=std_input,
current_dir=None,
print_input=print_input,
print_command=print_command,
print_output=print_output,
timeout=timeout,
output_is_log=output_is_log,
ignore_ret_codes=ignore_ret_codes,
split_arguments=split_arguments,
)

return output
Expand All @@ -729,7 +747,7 @@ def get_process_status(self, process_handle: subprocess.Popen) -> str:

def path_exists(self, path: PathType) -> bool:
try:
self.shell(command=f"[ -e {path} ]", print_input=False, print_output=False)
self.shell(command=f"[ -e {path} ]", print_command=False, print_output=False)
except subprocess.CalledProcessError as cpe:
if 1 == cpe.returncode:
return False
Expand All @@ -739,7 +757,7 @@ def path_exists(self, path: PathType) -> bool:
def read_file(self, path: PathType) -> str:
return self.shell(
command=f"cat {path}",
print_input=False,
print_command=False,
print_output=False,
)

Expand Down Expand Up @@ -810,7 +828,7 @@ def _remote_shell_command(

@staticmethod
def _get_ssh_info(host: str) -> Dict[str, str]:
output = shell_out(command=["ssh", "-G", str(host)], print_input=False, print_output=False)
output = shell_out(command=["ssh", "-G", str(host)], print_command=False, print_output=False)
ssh_host_info = dict([line.split(" ", maxsplit=1) for line in output.splitlines()])
return ssh_host_info

Expand All @@ -824,6 +842,6 @@ def _is_in_ssh_config(host: str) -> bool:
def _list_ssh_hosts() -> List[str]:
if not pathlib.Path("/usr/bin/fish").is_file():
return []
output = shell_out(command=["/usr/bin/fish", "-c", "__fish_print_hostnames"], print_input=False, print_output=False,)
output = shell_out(command=["/usr/bin/fish", "-c", "__fish_print_hostnames"], print_command=False, print_output=False,)
list_hosts = [line.strip() for line in output.splitlines()]
return list_hosts
10 changes: 6 additions & 4 deletions benchkit/communication/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ def shell(
current_dir: PathType | None = None,
environment: Environment = None,
shell: bool = False,
print_input: bool = True,
print_command: bool = True,
print_output: bool = True,
print_curdir: bool = True,
timeout: int | None = None,
output_is_log: bool = False,
ignore_ret_codes: Iterable[int] = (),
split_arguments: bool = True,
) -> str:
env_command = command_with_env(
command=command,
Expand All @@ -75,11 +76,12 @@ def shell(
command=full_command,
std_input=std_input,
current_dir=None,
print_input=print_input,
print_command=print_command,
print_output=print_output,
timeout=timeout,
output_is_log=output_is_log,
ignore_ret_codes=ignore_ret_codes,
split_arguments=split_arguments,
)

return output
Expand All @@ -92,7 +94,7 @@ def get_process_status(self, process_handle: subprocess.Popen) -> str:

def path_exists(self, path: PathType) -> bool:
try:
self.shell(command=f"[ -e {path} ]", print_input=False, print_output=False)
self.shell(command=f"[ -e {path} ]", print_command=False, print_output=False)
except subprocess.CalledProcessError as cpe:
if 1 == cpe.returncode:
return False
Expand All @@ -102,7 +104,7 @@ def path_exists(self, path: PathType) -> bool:
def read_file(self, path: PathType) -> str:
return self.shell(
command=f"cat {path}",
print_input=False,
print_command=False,
print_output=False,
)

Expand Down
Loading