import os
import subprocess as sp
from typing import List

from app.exceptions import SubprocessError


def subprocess_call(cmd: List[str]) -> None:
    """
    Call subprocess with given command.
    :param cmd: list of command parts
    :return:
    """
    popen_params = {"stdout": sp.DEVNULL, "stderr": sp.PIPE, "stdin": sp.DEVNULL}
    if os.name == "nt":
        popen_params["creationflags"] = 0x08000000
    proc = sp.Popen(cmd, **popen_params)
    proc.communicate()
    proc.stderr.close()
    if proc.returncode:
        raise SubprocessError(f"Error while calling subprocess with command {cmd}")
    del proc


def subprocess_call_windows(cmd: str) -> None:
    os.system(cmd)
