From cfc8764b2c6908c024d8dbcf393668da53457e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Vask=C3=B3?= Date: Fri, 2 Aug 2019 16:34:08 +0200 Subject: [PATCH] command-not-found: look up in PATH instead of using hard-coded location Motivation: command-not-found is available on a different location under NixOS. --- news/command-not-found.rst | 23 +++++++++++++++++++++++ xonsh/tools.py | 13 ++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 news/command-not-found.rst diff --git a/news/command-not-found.rst b/news/command-not-found.rst new file mode 100644 index 000000000..dd2779b7b --- /dev/null +++ b/news/command-not-found.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* command-not-found: now works on non-Debian bansed distributions + +**Security:** + +* diff --git a/xonsh/tools.py b/xonsh/tools.py index 9fcfb5439..05c022fb3 100644 --- a/xonsh/tools.py +++ b/xonsh/tools.py @@ -830,14 +830,21 @@ def command_not_found(cmd): """Uses the debian/ubuntu command-not-found utility to suggest packages for a command that cannot currently be found. """ + import shutil + if not ON_LINUX: return "" - elif not os.path.isfile("/usr/lib/command-not-found"): + + cnf = shutil.which("command-not-found") + if cnf is None: # utility is not on PATH return "" - c = "/usr/lib/command-not-found {0}; exit 0" + c = "{0} {1}; exit 0" s = subprocess.check_output( - c.format(cmd), universal_newlines=True, stderr=subprocess.STDOUT, shell=True + c.format(cnf, cmd), + universal_newlines=True, + stderr=subprocess.STDOUT, + shell=True, ) s = "\n".join(s.rstrip().splitlines()).strip() return s