zturner created this revision.
zturner added a reviewer: tfiala.
zturner added a subscriber: lldb-commits.

Prepare_bindings had a hand-rolled function to search the `PATH` for an 
executable.  I need to use this same function in a new script, so I'm raising 
the function into lldbsuite.support so we can reuse it.

http://reviews.llvm.org/D14885

Files:
  packages/Python/lldbsuite/support/fs.py
  scripts/prepare_bindings.py

Index: scripts/prepare_bindings.py
===================================================================
--- scripts/prepare_bindings.py
+++ scripts/prepare_bindings.py
@@ -16,6 +16,10 @@
 import platform
 import sys
 
+# LLDB modules:
+import use_lldb_suite
+from lldbsuite.support import fs
+
 
 def prepare_binding_for_language(scripts_dir, script_lang, options):
     """Prepares the binding for a specific language.
@@ -178,63 +182,6 @@
     return options
 
 
-def find_file_in_paths(paths, exe_basename):
-    """Returns the full exe path for the first path match.
-
-    @params paths the list of directories to search for the exe_basename
-    executable
-    @params exe_basename the name of the file for which to search.
-    e.g. "swig" or "swig.exe".
-
-    @return the full path to the executable if found in one of the
-    given paths; otherwise, returns None.
-    """
-    for path in paths:
-        trial_exe_path = os.path.join(path, exe_basename)
-        if os.path.exists(trial_exe_path):
-            return os.path.normcase(trial_exe_path)
-    return None
-
-
-def find_swig_executable(options):
-    """Finds the swig executable in the PATH or known good locations.
-
-    Replaces options.swig_executable with the full swig executable path.
-    """
-    # Figure out what we're looking for.
-    if platform.system() == 'Windows':
-        exe_basename = "swig.exe"
-        extra_dirs = []
-    else:
-        exe_basename = "swig"
-        extra_dirs = ["/usr/local/bin"]
-
-    # Figure out what paths to check.
-    path_env = os.environ.get("PATH", None)
-    if path_env is not None:
-        paths_to_check = path_env.split(os.path.pathsep)
-    else:
-        paths_to_check = []
-
-    # Add in the extra dirs
-    paths_to_check.extend(extra_dirs)
-    if len(paths_to_check) < 1:
-        logging.error(
-            "swig executable was not specified, PATH has no "
-            "contents, and there are no extra directories to search")
-        sys.exit(-6)
-
-    # Find the swig executable
-    options.swig_executable = find_file_in_paths(paths_to_check, exe_basename)
-    if not options.swig_executable or len(options.swig_executable) < 1:
-        logging.error(
-            "failed to find exe='%s' in paths='%s'",
-            exe_basename,
-            paths_to_check)
-        sys.exit(-6)
-    logging.info("found swig executable: %s", options.swig_executable)
-
-
 def main(args):
     """Drives the main script preparation steps.
 
@@ -247,7 +194,11 @@
     # Ensure we have a swig executable.
     if not options.swig_executable or len(options.swig_executable) == 0:
         if options.find_swig:
-            find_swig_executable(options)
+            try:
+                options.swig_executable = fs.find_executable("swig")
+            except Exception as e:
+                logging.error("Unable to find swig executable: %s" % e.message)
+                sys.exit(-6)
         else:
             logging.error(
                 "The --find-swig option must be specified "
Index: packages/Python/lldbsuite/support/fs.py
===================================================================
--- /dev/null
+++ packages/Python/lldbsuite/support/fs.py
@@ -0,0 +1,64 @@
+"""
+                     The LLVM Compiler Infrastructure
+
+This file is distributed under the University of Illinois Open Source
+License. See LICENSE.TXT for details.
+
+Prepares language bindings for LLDB build process.  Run with --help
+to see a description of the supported command line arguments.
+"""
+
+# Python modules:
+import os
+import platform
+import sys
+
+
+def _find_file_in_paths(paths, exe_basename):
+    """Returns the full exe path for the first path match.
+
+    @params paths the list of directories to search for the exe_basename
+    executable
+    @params exe_basename the name of the file for which to search.
+    e.g. "swig" or "swig.exe".
+
+    @return the full path to the executable if found in one of the
+    given paths; otherwise, returns None.
+    """
+    for path in paths:
+        trial_exe_path = os.path.join(path, exe_basename)
+        if os.path.exists(trial_exe_path):
+            return os.path.normcase(trial_exe_path)
+    return None
+
+def find_executable(executable):
+    """Finds the specified executable in the PATH or known good locations."""
+
+    # Figure out what we're looking for.
+    if platform.system() == "Windows":
+        executable = executable + ".exe"
+        extra_dirs = []
+    else:
+        extra_dirs = ["/usr/local/bin"]
+
+    # Figure out what paths to check.
+    path_env = os.environ.get("PATH", None)
+    if path_env is not None:
+        paths_to_check = path_env.split(os.path.pathsep)
+    else:
+        paths_to_check = []
+
+    # Add in the extra dirs
+    paths_to_check.extend(extra_dirs)
+    if len(paths_to_check) < 1:
+        raise os.OSError(
+            "executable was not specified, PATH has no "
+            "contents, and there are no extra directories to search")
+
+    result = _find_file_in_paths(paths_to_check, executable)
+
+    if not result or len(result) < 1:
+        raise os.OSError(
+            "failed to find exe='%s' in paths='%s'" % (executable, paths_to_check))
+    return result
+
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to