/usr/lib/python3/dist-packages/subuserlib/executablePath.py is in subuser 0.6.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # -*- coding: utf-8 -*-
"""
This module provides the usefull function C{which} which allows us to find the full path of a given executable and determine if an executable is present on the given system.
"""
#external imports
import os
#internal imports
#import ...
def isExecutable(fpath):
"""
Returns true if the given filepath points to an executable file.
"""
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
programs = {}
# Origonally taken from: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def which(program,excludeDir=None):
"""
@type program: string
@param program: The short name of the executable. Ex: "vim"
@rtype: str or None
@return: Returns the full path of a given executable. Or None, of the executable is not present on the given system.
"""
global programs
try:
return programs[(program,excludeDir)]
except KeyError:
pass
fname = program
def matchesImage(path):
fpath,fname = os.path.split(path)
return program == fname and not fpath == excludeDir
program = queryPATH(matchesImage)
programs[(program,excludeDir)] = program
return program
def queryPATH(test,list=False):
"""
Search the PATH for an executable.
"""
paths = []
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
if os.path.exists(path):
for fileInPath in os.listdir(path):
exeFile = os.path.join(path, fileInPath)
if isExecutable(exeFile):
if test(exeFile):
if list:
paths.append(exeFile)
else:
return exeFile
if list:
return paths
else:
return None
|