Skip to content

Commit

Permalink
otaclient_common.common now uses suprocess_run_wrapper from otaclient…
Browse files Browse the repository at this point in the history
…_common.linux module
  • Loading branch information
Bodong-Yang committed Jun 10, 2024
1 parent a434d25 commit 09e3aa0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 35 deletions.
36 changes: 2 additions & 34 deletions src/otaclient_common/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import logging
import os
import shlex
import shutil
import subprocess
import time
Expand All @@ -33,6 +32,8 @@

import requests

from otaclient_common.linux import subprocess_run_wrapper

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -101,39 +102,6 @@ def write_str_to_file_sync(path: Union[Path, str], input: str):
os.fsync(f.fileno())


def subprocess_run_wrapper(
cmd: str | list[str],
*,
check: bool,
check_output: bool,
timeout: Optional[float] = None,
) -> subprocess.CompletedProcess[bytes]:
"""A wrapper for subprocess.run method.
NOTE: this is for the requirement of customized subprocess call
in the future, like chroot or nsenter before execution.
Args:
cmd (str | list[str]): command to be executed.
check (bool): if True, raise CalledProcessError on non 0 return code.
check_output (bool): if True, the UTF-8 decoded stdout will be returned.
timeout (Optional[float], optional): timeout for execution. Defaults to None.
Returns:
subprocess.CompletedProcess[bytes]: the result of the execution.
"""
if isinstance(cmd, str):
cmd = shlex.split(cmd)

return subprocess.run(
cmd,
check=check,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE if check_output else None,
timeout=timeout,
)


def subprocess_check_output(
cmd: str | list[str],
*,
Expand Down
7 changes: 6 additions & 1 deletion src/otaclient_common/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from subprocess import check_call
from typing import Any, Callable, Optional

from otaclient._utils.typing import StrOrPath
from otaclient_common.typing import StrOrPath

#
# ------ swapfile handling ------ #
Expand Down Expand Up @@ -158,6 +158,11 @@ def map_gid_by_grpnam(*, src_db: ParsedGroup, dst_db: ParsedGroup, gid: int) ->
raise ValueError(f"failed to find mapping for {gid}")


#
# ------ subprocess call ------ #
#


def subprocess_run_wrapper(
cmd: str | list[str],
*,
Expand Down

0 comments on commit 09e3aa0

Please sign in to comment.