Project Name: saltstack+salt
Language: Python
Statement Type: API

Source Code:
def which(exe=None):
    """
    Python clone of /usr/bin/which
    """

    if not exe:
        log.error("No executable was passed to be searched by salt.utils.path.which()")
        return None

    ## define some utilities (we use closures here because our predecessor used them)
    def is_executable_common(path):
        """
        This returns truth if posixy semantics (which python simulates on
        windows) states that this is executable.
        """
        return os.path.isfile(path) and os.access(path, os.X_OK)

    def resolve(path):
        """
        This will take a path and recursively follow the link until we get to a
        real file.
        """
        while os.path.islink(path):
            res = readlink(path)

            # if the link points to a relative target, then convert it to an
            # absolute path relative to the original path
            if not os.path.isabs(res):
                directory, _ = os.path.split(path)
                res = join(directory, res)
            path = res
        return path

    # windows-only
    def has_executable_ext(path, ext_membership):
        """
        Extract the extension from the specified path, lowercase it so we
        can be insensitive, and then check it against the available exts.
        """
        p, ext = os.path.splitext(path)
        return ext.lower() in ext_membership

    ## prepare related variables from the environment
    res = salt.utils.stringutils.to_unicode(os.environ.get("PATH", ""))
    system_path = res.split(os.pathsep)

    # add some reasonable defaults in case someone's PATH is busted
    if not salt.utils.platform.is_windows():
        res = set(system_path)
        extended_path = [
            "/sbin",
            "/bin",
            "/usr/sbin",
            "/usr/bin",
            "/usr/local/sbin",
            "/usr/local/bin",
        ]
        system_path.extend([p for p in extended_path if p not in res])

    ## now to define the semantics of what's considered executable on a given platform
    if salt.utils.platform.is_windows():
        # executable semantics on windows requires us to search PATHEXT
        res = salt.utils.stringutils.to_str(os.environ.get("PATHEXT", ".EXE"))

        # generate two variables, one of them for O(n) searches (but ordered)
        # and another for O(1) searches. the previous guy was trying to use
        # memoization with a function that has no arguments, this provides
        # the exact same benefit
        pathext = res.split(os.pathsep)
        res = {ext.lower() for ext in pathext}

        # check if our caller already specified a valid extension as then we don't need to match it
        _, ext = os.path.splitext(exe)
        if ext.lower() in res:
            pathext = [""]

            is_executable = is_executable_common

        # The specified extension isn't valid, so we just assume it's part of the
        # filename and proceed to walk the pathext list
        else:
            is_executable = lambda path, membership=res: is_executable_common(
                path
            ) and has_executable_ext(path, membership)

    else:
        # in posix, there's no such thing as file extensions..only zuul
        pathext = [""]

        # executable semantics are pretty simple on reasonable platforms...
        is_executable = is_executable_common

    ## search for the executable

    # check to see if the full path was specified as then we don't need
    # to actually walk the system_path for any reason
    if is_executable(exe):
        return exe

    # now to search through our system_path
    for path in system_path:
        p = join(path, exe)

        # iterate through all extensions to see which one is executable
        for ext in pathext:
            pext = p + ext
            rp = resolve(pext)
            if is_executable(rp):
                return p + ext
            continue
        continue

    ## if something was executable, we should've found it already...
    log.trace(
        "'%s' could not be found in the following search path: '%s'", exe, system_path
    )
    return None

which(exe='true')

Selected Statement: res = set(system_path)

Function Input:
exe: 'true'

Variable Values Before Statement:
system_path: /home/jinjun/.gdrive-downloader
/local/arise/jinjun/miniforge3/bin
/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin
/home/jinjun/.gvm/gos/go1.19.1/bin
/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin
/home/jinjun/.gvm/bin
/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin
/local/rcs/jinjun/miniforge3/condabin
/home/jinjun/.gdrive-downloader
/local/arise/jinjun/miniforge3/bin
/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/snap/bin
/home/jinjun/.local/bin
/home/jinjun/.local/bin

Value After Statement Execution: {'/usr/games', '/sbin', '/home/jinjun/.gvm/gos/go1.19.1/bin', '/home/jinjun/.gvm/bin', '/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin', '/home/jinjun/.local/bin', '/snap/bin', '/usr/bin', '/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli', '/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin', '/bin', '/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin', '/usr/local/games', '/usr/local/sbin', '/home/jinjun/.gdrive-downloader', '/local/rcs/jinjun/miniforge3/condabin', '/local/arise/jinjun/miniforge3/bin', '/usr/sbin', '/usr/local/bin'}

Variable States During Runtime (line, value):
exe: 1
'true'
is_executable_common: 11
<function which.<locals>.is_executable_common at 0x7f27d3207c10>
resolve: 18
<function which.<locals>.resolve at 0x7f27d3207820>
has_executable_ext: 35
<function which.<locals>.has_executable_ext at 0x7f27d31c20d0>
res: 44
'/home/jinjun/.gdrive-downloader:/local/arise/jinjun/miniforge3/bin:/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin:/home/jinjun/.gvm/gos/go1.19.1/bin:/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin:/home/jinjun/.gvm/bin:/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin:/local/rcs/jinjun/miniforge3/condabin:/home/jinjun/.gdrive-downloader:/local/arise/jinjun/miniforge3/bin:/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/jinjun/.local/bin:/home/jinjun/.local/bin'
49
{'/usr/games', '/sbin', '/home/jinjun/.gvm/gos/go1.19.1/bin', '/home/jinjun/.gvm/bin', '/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin', '/home/jinjun/.local/bin', '/snap/bin', '/usr/bin', '/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli', '/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin', '/bin', '/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin', '/usr/local/games', '/usr/local/sbin', '/home/jinjun/.gdrive-downloader', '/local/rcs/jinjun/miniforge3/condabin', '/local/arise/jinjun/miniforge3/bin', '/usr/sbin', '/usr/local/bin'}
system_path: 45
/home/jinjun/.gdrive-downloader
/local/arise/jinjun/miniforge3/bin
/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin
/home/jinjun/.gvm/gos/go1.19.1/bin
/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin
/home/jinjun/.gvm/bin
/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin
/local/rcs/jinjun/miniforge3/condabin
/home/jinjun/.gdrive-downloader
/local/arise/jinjun/miniforge3/bin
/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/snap/bin
/home/jinjun/.local/bin
/home/jinjun/.local/bin
extended_path: 50
/sbin, /bin, /usr/sbin, /usr/bin, /usr/local/sbin, /usr/local/bin
pathext: 88
is_executable: 91
<function which.<locals>.is_executable_common at 0x7f27d3207c10>
path: 101
'/home/jinjun/.gdrive-downloader'
101
'/local/arise/jinjun/miniforge3/bin'
101
'/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin'
101
'/home/jinjun/.gvm/gos/go1.19.1/bin'
101
'/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin'
101
'/home/jinjun/.gvm/bin'
101
'/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin'
101
'/local/rcs/jinjun/miniforge3/condabin'
101
'/home/jinjun/.gdrive-downloader'
101
'/local/arise/jinjun/miniforge3/bin'
101
'/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli'
101
'/usr/local/sbin'
101
'/usr/local/bin'
101
'/usr/sbin'
101
'/usr/bin'
p: 102
'/home/jinjun/.gdrive-downloader/true'
102
'/local/arise/jinjun/miniforge3/bin/true'
102
'/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin/true'
102
'/home/jinjun/.gvm/gos/go1.19.1/bin/true'
102
'/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin/true'
102
'/home/jinjun/.gvm/bin/true'
102
'/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin/true'
102
'/local/rcs/jinjun/miniforge3/condabin/true'
102
'/home/jinjun/.gdrive-downloader/true'
102
'/local/arise/jinjun/miniforge3/bin/true'
102
'/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli/true'
102
'/usr/local/sbin/true'
102
'/usr/local/bin/true'
102
'/usr/sbin/true'
102
'/usr/bin/true'
ext: 105
''
pext: 106
'/home/jinjun/.gdrive-downloader/true'
106
'/local/arise/jinjun/miniforge3/bin/true'
106
'/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin/true'
106
'/home/jinjun/.gvm/gos/go1.19.1/bin/true'
106
'/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin/true'
106
'/home/jinjun/.gvm/bin/true'
106
'/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin/true'
106
'/local/rcs/jinjun/miniforge3/condabin/true'
106
'/home/jinjun/.gdrive-downloader/true'
106
'/local/arise/jinjun/miniforge3/bin/true'
106
'/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli/true'
106
'/usr/local/sbin/true'
106
'/usr/local/bin/true'
106
'/usr/sbin/true'
106
'/usr/bin/true'
rp: 107
'/home/jinjun/.gdrive-downloader/true'
107
'/local/arise/jinjun/miniforge3/bin/true'
107
'/home/jinjun/.gvm/pkgsets/go1.19.1/global/bin/true'
107
'/home/jinjun/.gvm/gos/go1.19.1/bin/true'
107
'/home/jinjun/.gvm/pkgsets/go1.19.1/global/overlay/bin/true'
107
'/home/jinjun/.gvm/bin/true'
107
'/local/rcs/jinjun/miniforge3/envs/saltstack+salt/bin/true'
107
'/local/rcs/jinjun/miniforge3/condabin/true'
107
'/home/jinjun/.gdrive-downloader/true'
107
'/local/arise/jinjun/miniforge3/bin/true'
107
'/home/jinjun/.vscode-server/cli/servers/Stable-31c37ee8f63491495ac49e43b8544550fbae4533/server/bin/remote-cli/true'
107
'/usr/local/sbin/true'
107
'/usr/local/bin/true'
107
'/usr/sbin/true'
107
'/usr/bin/true'