From 2bf55c9e0bce8095e60f808af7bdbfdc2fd57e0c Mon Sep 17 00:00:00 2001 From: zipperer <47086307+zipperer@users.noreply.github.com> Date: Thu, 19 Oct 2023 15:33:47 -0500 Subject: [PATCH] Edit run_onchange_03-install-packages.tmpl: reduce number of times `chezmoi data` executes Claims I believe (each of which could be false and thereby make this commit wrong): - each call to `get_config` returns the same information (i.e. for each call the return value is the same) - `filter(lambda p: matches_features(p, get_config()), to_install)`, calls get_config() for each entry in `to_install` - `get_packages` computes `data`, and `data` contains what `get_config` returns Based on those claims, I propose: - `get_packages` returns both packages and data - `filter(lambda p: matches_features(p, config), to_install)` where config was computed once before and reused for each package `p` --- home/run_onchange_03-install-packages.tmpl | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/home/run_onchange_03-install-packages.tmpl b/home/run_onchange_03-install-packages.tmpl index 9a09e78..b0d19c3 100755 --- a/home/run_onchange_03-install-packages.tmpl +++ b/home/run_onchange_03-install-packages.tmpl @@ -43,22 +43,18 @@ def install_packages(package_names): subprocess.run(command) -def get_config(): +def get_packages_and_config(): result = subprocess.run(["chezmoi", "data"], capture_output=True) data = json.loads(result.stdout) - return data["chezmoi"]["config"]["data"] - - -def get_packages(): - result = subprocess.run(["chezmoi", "data"], capture_output=True) - data = json.loads(result.stdout) - return data["packages"] + packages = data["packages"] + config = data["chezmoi"]["config"]["data"] + return packages, config def main(): - packages = get_packages() + packages, config = get_packages_and_config() to_install = filter(not_installed, packages) - to_install = list(filter(lambda p: matches_features(p, get_config()), to_install)) + to_install = list(filter(lambda p: matches_features(p, config), to_install)) if to_install: install_packages(list(map(lambda p: p["name"], to_install))) else: