import logging import subprocess import sys class SudoChecker: def __init__(self): self.log = logging.getLogger( "sudo" ) def check(self): self.log.info( "Checking sudo..." ) try: result = subprocess.run( [ "sudo", "-n", "-v" ] ) if result.returncode != 0: result = subprocess.run( [ "sudo", "-v" ] ) except KeyboardInterrupt: self.log.error( "Sudo authentication cancelled." ) raise SystemExit(1) if result.returncode != 0: self.log.error( "Sudo authentication is not available." ) raise SystemExit(1) self.log.info( "Sudo: OK" ) def refresh(self): try: result = subprocess.run( [ "sudo", "-n", "-v" ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) except Exception: return False return result.returncode == 0