From 75b95f0259de06aacb6e0bc585d734a5ec907aa2 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 11 Jan 2020 01:39:20 -0800 Subject: [PATCH] Add winwhich.py. --- voussoirkit/winwhich.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 voussoirkit/winwhich.py diff --git a/voussoirkit/winwhich.py b/voussoirkit/winwhich.py new file mode 100644 index 0000000..25f7ebd --- /dev/null +++ b/voussoirkit/winwhich.py @@ -0,0 +1,26 @@ +''' +Instead of adding every program's directory to my system PATH, I prefer having +a single directory on my PATH which contains Windows shortcuts (lnk files) +pointing to each program. +Windows lnks are different from softlinks because they maintain the program's +real directory necessary for loading nearby dlls etc. +However, this breaks `shutil.which` --> `subprocess.run` because subprocess.run +with shell=False does not interpret lnks, and needs a direct path to the exe. + +So, this module provides a function `which` that if it finds an lnk file, will +return the exe path, otherwise behaves the same as normal shutil which. +''' +import os +import shutil +import winshell + +def which(cmd, *args, **kwargs): + path = shutil.which(cmd, *args, **kwargs) + + if path is None: + return None + + if os.name == 'nt' and path.endswith('.lnk'): + path = winshell.Shortcut(path).path + + return path