From 27e7fe9a10d964b4b7a94c165b397fa3d159d36c Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Sat, 20 Aug 2022 02:20:47 +0200 Subject: [PATCH] utils.programs_available(): add cache --- utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/utils.py b/utils.py index 7ddece8..ce3a427 100644 --- a/utils.py +++ b/utils.py @@ -8,12 +8,18 @@ from typing import Optional, Union, Sequence from exec.cmd import run_cmd, run_root_cmd +_programs_available = dict[str, bool]() -def programs_available(programs: Union[str, Sequence[str]]) -> bool: + +def programs_available(programs: Union[str, Sequence[str]], lazy: bool = True) -> bool: + global _programs_available if type(programs) is str: programs = [programs] for program in programs: - if not which(program): + if program not in _programs_available or not lazy: + avail = bool(which(program)) + _programs_available[program] = avail + if not _programs_available[program]: return False return True