]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding function caller_search_path() to pp_lib/common.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 23 Mar 2017 16:43:50 +0000 (17:43 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 23 Mar 2017 16:43:50 +0000 (17:43 +0100)
pp_lib/common.py

index 370d3e755528c7950edb1828303242ddeea9b385..fa02403196affef0c53de3e4c94f1bd4920df96a 100644 (file)
@@ -21,7 +21,7 @@ import six
 
 # Own modules
 
-__version__ = '0.3.1'
+__version__ = '0.4.1'
 
 LOG = logging.getLogger(__name__)
 
@@ -236,6 +236,57 @@ def to_str(obj, encoding='utf-8'):
         return to_unicode(obj, encoding)
 
 
+# =============================================================================
+def caller_search_path():
+    """
+    Builds a search path for executables from environment $PATH
+    including some standard paths.
+
+    @return: all existing search paths
+    @rtype: list
+    """
+
+    path_list = []
+    search_path = os.environ['PATH']
+    if not search_path:
+        search_path = os.defpath
+
+    search_path_list = [
+        '/opt/PPlocal/bin',
+    ]
+
+    for d in search_path.split(os.pathsep):
+        search_path_list.append(d)
+
+    default_path = [
+        '/bin',
+        '/usr/bin',
+        '/usr/local/bin',
+        '/sbin',
+        '/usr/sbin',
+        '/usr/local/sbin',
+        '/usr/ucb',
+        '/usr/sfw/bin',
+        '/opt/csw/bin',
+        '/usr/openwin/bin',
+        '/usr/ccs/bin',
+    ]
+
+    for d in default_path:
+        search_path_list.append(d)
+
+    for d in search_path_list:
+        if not os.path.exists(d):
+            continue
+        if not os.path.isdir(d):
+            continue
+        d_abs = os.path.realpath(d)
+        if d_abs not in path_list:
+            path_list.append(d_abs)
+
+    return path_list
+
+
 # =============================================================================
 
 if __name__ == "__main__":