We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
import subprocess import time from types import SimpleNamespace import platform this_pc_os = platform.system() """terminal color""" TC = SimpleNamespace( **{ "YELLOW": "\033[33m", "GREEN": "\033[92m", "RED": "\033[91m", "BLUE": "\033[34m", "RESET": "\033[0m", } ) class Cmd: def __new__( self, cmd: str, cwd="./", timeout_duration=None, suppress=True ) -> tuple[int, str, str]: self.cmd = cmd self.cwd = cwd self.returncode = 0 self.has_err = True if not suppress: print(f"{self.cmd}", end="", flush=True) cwd_not_cur = f" in {self.cwd}" if self.cwd != "./" else "" """ process setup """ process = None enc = "" if this_pc_os == "Windows": enc = "cp950" process = subprocess.Popen( ["powershell", self.cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, # executable="bash", cwd=self.cwd, ) elif this_pc_os == "Linux": enc = "utf8" process = subprocess.Popen( self.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, executable="bash", cwd=self.cwd, ) else: raise RuntimeError(f"`{platform.system()}` is not supported") """ execution """ out = bytearray() err = bytearray() timeStarted = time.time() try: _out, _err = process.communicate() out = _out if _out is not None else out err = _err if _err is not None else err self.returncode = process.returncode if process.returncode != 0: raise RuntimeError( f"returncode is not 0 but {process.returncode}. " + str(out + err, encoding=enc) ) except: if not suppress: print(f"{cwd_not_cur} {TC.RED}[failed]{TC.RESET}") return self.returncode, str(out, encoding=enc), str(err, encoding=enc) timeDelta = time.time() - timeStarted if not suppress: print(f"{cwd_not_cur} {TC.GREEN}[passed]{TC.RESET} ({timeDelta:.3f}s)") self.has_err = False return self.returncode, str(out, encoding=enc), str(err, encoding=enc)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: