Auto-detect backend to use

This commit is contained in:
Midgard 2022-09-18 04:13:34 +02:00
parent 72504309cc
commit 903ae85abc
Signed by untrusted user who does not match committer: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -3,6 +3,7 @@
import sys import sys
import os import os
import subprocess import subprocess
import shutil
from glob import glob from glob import glob
import re import re
@ -78,28 +79,34 @@ def list_from_print0(data):
] ]
FIND_CMD = shutil.which("find")
def find(query, paths, depth=None): def find(query, paths, depth=None):
depth_args = ["-mindepth", f"{depth}", "-maxdepth", f"{depth}"] if depth is not None else [] depth_args = ["-mindepth", f"{depth}", "-maxdepth", f"{depth}"] if depth is not None else []
proc = subprocess.run( 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 check=True, stdout=subprocess.PIPE
) )
return list_from_print0(proc.stdout) return list_from_print0(proc.stdout)
FD_CMD = shutil.which("fd")
def fd(query, paths, depth): def fd(query, paths, depth):
depth_args = [f"--exact-depth={depth}"] if depth is not None else [] depth_args = [f"--exact-depth={depth}"] if depth is not None else []
proc = subprocess.run( 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 check=True, stdout=subprocess.PIPE
) )
return list_from_print0(proc.stdout) return list_from_print0(proc.stdout)
# search_backend = find if FD_CMD is not None:
search_backend = fd search_backend = fd
elif FIND_CMD is not None:
search_backend = find
else:
raise Exception("Neither `fd` nor `find` found in PATH")
# Ebuild reading # Ebuild reading