subprocess check

This commit is contained in:
JerryXiao 2021-01-18 15:47:07 +08:00
parent 008d4817f8
commit aa23067b07
Signed by: Jerry
GPG key ID: 9D9CE43650FF2BAA

View file

@ -44,7 +44,8 @@ def sync() -> None:
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
encoding='utf-8', encoding='utf-8',
timeout=TIMEOUT timeout=TIMEOUT,
check=True
) )
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
if sync_err_is_net(e.output): if sync_err_is_net(e.output):
@ -166,9 +167,13 @@ def has_previous_error() -> Exception:
def is_system_failed() -> str: def is_system_failed() -> str:
try: try:
p = subprocess.run(["systemctl", "is-system-running"], p = subprocess.run(["systemctl", "is-system-running"],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL, stdin=subprocess.DEVNULL,
timeout=20, encoding='utf-8') timeout=20,
encoding='utf-8',
check=False
)
except Exception: except Exception:
ret = "exec fail" ret = "exec fail"
else: else:
@ -185,7 +190,9 @@ def main() -> None:
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
encoding='utf-8', encoding='utf-8',
timeout=20) timeout=20,
check=True
)
locales = [l.lower() for l in p.stdout.strip().split('\n')] locales = [l.lower() for l in p.stdout.strip().split('\n')]
preferred = ['en_US.UTF-8', 'C.UTF-8'] preferred = ['en_US.UTF-8', 'C.UTF-8']
for l in preferred: for l in preferred:
@ -240,7 +247,8 @@ def main() -> None:
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
encoding='utf-8', encoding='utf-8',
timeout=TIMEOUT timeout=TIMEOUT,
check=True
) )
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
logger.error(f'needrestart failed with {e.returncode=} {e.output=}') logger.error(f'needrestart failed with {e.returncode=} {e.output=}')
@ -279,12 +287,13 @@ def main() -> None:
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
encoding='utf-8', encoding='utf-8',
timeout=20 timeout=20,
check=True
) )
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
pass pass
else: else:
subprocess.run(["systemctl", "reset-failed", "pacroller"], timeout=20) subprocess.run(["systemctl", "reset-failed", "pacroller"], timeout=20, check=True)
if SYSTEMD: if SYSTEMD:
if _s := is_system_failed(): if _s := is_system_failed():
logger.error(f'systemd is in {_s} state, refused') logger.error(f'systemd is in {_s} state, refused')