You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9KB

  1. from pathlib import PurePath
  2. from typing import Iterable, Union, Optional, Iterator, NoReturn, Sequence, Mapping
  3. from typing_extensions import Protocol
  4. import subprocess
  5. from .util import Pathish
  6. CommandLineArg = Union[str, Pathish, int, float]
  7. CommandLineArg1 = Union[CommandLineArg, Iterable[CommandLineArg]]
  8. CommandLineArg2 = Union[CommandLineArg1, Iterable[CommandLineArg1]]
  9. CommandLineArg3 = Union[CommandLineArg2, Iterable[CommandLineArg2]]
  10. CommandLineArg4 = Union[CommandLineArg3, Iterable[CommandLineArg3]]
  11. class CommandLine(Protocol):
  12. def __iter__(self) -> Iterator[Union['CommandLine', CommandLineArg]]:
  13. pass
  14. # CommandLine = Union[CommandLineArg4, Iterable[CommandLineArg4]]
  15. class ProcessResult(Protocol):
  16. args: Sequence[str]
  17. returncode: int
  18. stdout: bytes
  19. stderr: bytes
  20. def flatten_cmd(cmd: CommandLine) -> Iterable[str]:
  21. if isinstance(cmd, (str, PurePath)):
  22. yield str(cmd)
  23. elif isinstance(cmd, (int, float)):
  24. yield str(cmd)
  25. elif hasattr(cmd, '__iter__'):
  26. each = (flatten_cmd(arg) for arg in cmd) # type: ignore
  27. for item in each:
  28. yield from item
  29. else:
  30. assert False, f'Invalid command line element: {repr(cmd)}'
  31. def run(*cmd: CommandLine,
  32. cwd: Optional[Pathish] = None,
  33. check: bool = False,
  34. env: Optional[Mapping[str, str]] = None) -> ProcessResult:
  35. command = list(flatten_cmd(cmd))
  36. res = subprocess.run(command, cwd=cwd, check=False, env=env)
  37. if res.returncode and check:
  38. raise_error(res)
  39. return res
  40. def raise_error(proc: ProcessResult) -> NoReturn:
  41. raise subprocess.CalledProcessError(proc.returncode, proc.args, output=proc.stdout, stderr=proc.stderr)
  42. def check_run(*cmd: CommandLine,
  43. cwd: Optional[Pathish] = None,
  44. env: Optional[Mapping[str, str]] = None) -> ProcessResult:
  45. return run(cmd, cwd=cwd, check=True, env=env)