DavidSpickett created this revision.
Herald added a project: All.
DavidSpickett requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

In build.py we have our own find_executable that looks
a lot like the distutils one that I switched to shutil.which.

This find_executable isn't quite the same as shutil.which
so I've refactored it to call that in the correct way.

Note that the path passed to shutil.which is in the form that
PATH would be, meaning separators are allowed.

>>> shutil.which("gcc", path="/home/david.spickett:/bin")

'/bin/gcc'

We just need to make sure it doesn't ignore the existing PATH
and normalise the result if it does find the binary.

The .exe extension is automatically added to the binary name
if we are on Windows.

Depends on D124601 <https://reviews.llvm.org/D124601>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124604

Files:
  lldb/test/Shell/helper/build.py


Index: lldb/test/Shell/helper/build.py
===================================================================
--- lldb/test/Shell/helper/build.py
+++ lldb/test/Shell/helper/build.py
@@ -4,6 +4,7 @@
 
 import argparse
 import os
+import shutil
 import signal
 import subprocess
 import sys
@@ -170,16 +171,14 @@
         print('    {0} = {1}'.format(e, formatted_value))
 
 def find_executable(binary_name, search_paths):
-    if sys.platform == 'win32':
-        binary_name = binary_name + '.exe'
-
-    search_paths = os.pathsep.join(search_paths)
-    paths = search_paths + os.pathsep + os.environ.get('PATH', '')
-    for path in paths.split(os.pathsep):
-        p = os.path.join(path, binary_name)
-        if os.path.exists(p) and not os.path.isdir(p):
-            return os.path.normpath(p)
-    return None
+    # shutil.which will ignore PATH if given a path argument, we want to 
include it.
+    search_paths.append(os.environ.get('PATH', ''))
+    search_path = os.pathsep.join(search_paths)
+    binary_path = shutil.which(binary_name, path=search_path)
+    if binary_path is not None:
+        # So for example, we get '/bin/gcc' instead of '/usr/../bin/gcc'.
+        binary_path = os.path.normpath(binary_path)
+    return binary_path
 
 def find_toolchain(compiler, tools_dir):
     if compiler == 'msvc':


Index: lldb/test/Shell/helper/build.py
===================================================================
--- lldb/test/Shell/helper/build.py
+++ lldb/test/Shell/helper/build.py
@@ -4,6 +4,7 @@
 
 import argparse
 import os
+import shutil
 import signal
 import subprocess
 import sys
@@ -170,16 +171,14 @@
         print('    {0} = {1}'.format(e, formatted_value))
 
 def find_executable(binary_name, search_paths):
-    if sys.platform == 'win32':
-        binary_name = binary_name + '.exe'
-
-    search_paths = os.pathsep.join(search_paths)
-    paths = search_paths + os.pathsep + os.environ.get('PATH', '')
-    for path in paths.split(os.pathsep):
-        p = os.path.join(path, binary_name)
-        if os.path.exists(p) and not os.path.isdir(p):
-            return os.path.normpath(p)
-    return None
+    # shutil.which will ignore PATH if given a path argument, we want to include it.
+    search_paths.append(os.environ.get('PATH', ''))
+    search_path = os.pathsep.join(search_paths)
+    binary_path = shutil.which(binary_name, path=search_path)
+    if binary_path is not None:
+        # So for example, we get '/bin/gcc' instead of '/usr/../bin/gcc'.
+        binary_path = os.path.normpath(binary_path)
+    return binary_path
 
 def find_toolchain(compiler, tools_dir):
     if compiler == 'msvc':
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
  • [Lldb-commits] [PATCH] ... David Spickett via Phabricator via lldb-commits

Reply via email to