From 903ae85abcca3e2d84d05c1f53045742a5f67ef2 Mon Sep 17 00:00:00 2001 From: Midgard <2885-Midgard@users.noreply.framagit.org> Date: Sun, 18 Sep 2022 04:13:34 +0200 Subject: [PATCH] Auto-detect backend to use --- pyesearch.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pyesearch.py b/pyesearch.py index 48daa4a..391e77e 100755 --- a/pyesearch.py +++ b/pyesearch.py @@ -3,6 +3,7 @@ import sys import os import subprocess +import shutil from glob import glob import re @@ -78,28 +79,34 @@ def list_from_print0(data): ] +FIND_CMD = shutil.which("find") def find(query, paths, depth=None): depth_args = ["-mindepth", f"{depth}", "-maxdepth", f"{depth}"] if depth is not None else [] proc = subprocess.run( - ["find", *paths, *depth_args, "-iname", f"*{query}*.ebuild", "-print0"], + [FIND_CMD, *paths, *depth_args, "-iname", f"*{query}*.ebuild", "-print0"], check=True, stdout=subprocess.PIPE ) return list_from_print0(proc.stdout) +FD_CMD = shutil.which("fd") def fd(query, paths, depth): depth_args = [f"--exact-depth={depth}"] if depth is not None else [] proc = subprocess.run( - ["fd", *depth_args, "--print0", "--extension=ebuild", "--", query, *paths], + [FD_CMD, *depth_args, "--print0", "--extension=ebuild", "--", query, *paths], check=True, stdout=subprocess.PIPE ) return list_from_print0(proc.stdout) -# search_backend = find -search_backend = fd +if FD_CMD is not None: + search_backend = fd +elif FIND_CMD is not None: + search_backend = find +else: + raise Exception("Neither `fd` nor `find` found in PATH") # Ebuild reading