| 45 | |
| 46 | |
| 47 | class Sh: |
| 48 | def __init__(self, env=None, cwd=None): |
| 49 | self.env = os.environ.copy() |
| 50 | if env is not None: |
| 51 | self.env.update(env) |
| 52 | self.cwd = cwd |
| 53 | |
| 54 | def tee(self, cmd: str, **kwargs): |
| 55 | """ |
| 56 | Run 'cmd' in a shell then return the (process, stdout) as a tuple |
| 57 | """ |
| 58 | |
| 59 | logging.info(f"+ {cmd}") |
| 60 | |
| 61 | kwargs = { |
| 62 | **self._default_popen_flags(), |
| 63 | **kwargs, |
| 64 | "stdout": subprocess.PIPE, |
| 65 | } |
| 66 | proc = subprocess.Popen(cmd, **kwargs) |
| 67 | |
| 68 | stdout = [] |
| 69 | |
| 70 | def _tee_output(s): |
| 71 | stdout.append(s) |
| 72 | print(s, end="") |
| 73 | |
| 74 | while proc.poll() is None: |
| 75 | _tee_output(proc.stdout.readline()) |
| 76 | _tee_output(proc.stdout.read()) |
| 77 | |
| 78 | stdout = "".join(stdout) |
| 79 | if proc.returncode: |
| 80 | raise subprocess.CalledProcessError(proc.returncode, proc.args, stdout) |
| 81 | |
| 82 | return proc, stdout |
| 83 | |
| 84 | def run(self, cmd: str, **kwargs): |
| 85 | logging.info(f"+ {cmd}") |
| 86 | |
| 87 | kwargs = { |
| 88 | **self._default_popen_flags(), |
| 89 | "check": True, |
| 90 | **kwargs, |
| 91 | } |
| 92 | |
| 93 | return subprocess.run(cmd, **kwargs) |
| 94 | |
| 95 | def _default_popen_flags(self): |
| 96 | return { |
| 97 | "shell": True, |
| 98 | "env": self.env, |
| 99 | "encoding": "utf-8", |
| 100 | "cwd": self.cwd, |
| 101 | } |
| 102 | |
| 103 | |
| 104 | def tags_from_title(title: str) -> list[str]: |
no outgoing calls
no test coverage detected