#! /usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (C) 2019, ARM Limited and contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import stat
import platform

def _get_abi():
    machine = platform.machine()
    return dict(
        x86_64='x86_64',
        aarch64='arm64',
        arm='arm',
    )[machine]

HOST_ABI = _get_abi()
"""
ABI of the machine that imported that module.
"""
del _get_abi

ASSETS_PATH = os.path.dirname(__file__)
"""
Path in which all assets the ``lisa`` package relies on are located in.
"""

def _get_abi_bin():
    def list_binaries(path):
        return {
            entry.name: os.path.abspath(entry.path)
            for entry in os.scandir(path)
            if entry.stat().st_mode & stat.S_IXUSR
        }

    bin_path = os.path.join(ASSETS_PATH, 'binaries')
    return {
        entry.name: list_binaries(entry.path)
        for entry in os.scandir(bin_path)
        if entry.is_dir()
    }

ABI_BINARIES = _get_abi_bin()
del _get_abi_bin

HOST_BINARIES = ABI_BINARIES[HOST_ABI]

HOST_PATH = ':'.join((
    os.path.join(ASSETS_PATH, 'binaries', HOST_ABI),
    os.path.join(ASSETS_PATH, 'scripts'),
    os.environ['PATH']
))
"""
Value to be used as the ``PATH`` env var on the host.
"""


# vim :set tabstop=4 shiftwidth=4 textwidth=80 expandtab

@@ -690,16 +690,55 @@
 platform
+%0Aimport shutil%0Afrom pathlib import Path
 %0A%0Adef _g
@@ -1594,31 +1594,80 @@
 I%5D%0A%0A
-HOST_PATH = ':'.join((%0A
+def _make_path(abi=None):%0A    abi = abi or HOST_ABI%0A%0A    compos = %5B%0A    
     
@@ -1708,23 +1708,22 @@
 s', 
-HOST_ABI
+abi
 ),%0A    
+ 
+   
 os.p
@@ -1764,92 +1764,1104 @@
     
-os.environ%5B'PATH'%5D%0A))%0A%22%22%22%0AValue to be used as the %60%60PATH%60%60 env var on the host.%0A%22%22%22%0A
+%5D%0A%0A    if abi == HOST_ABI:%0A        path = os.environ%5B'PATH'%5D%0A        use_system = bool(int(os.environ.get('LISA_USE_SYSTEM_BIN', 0)))%0A        if use_system:%0A            compos = %5Bpath%5D + compos%0A        else:%0A            compos = compos + %5Bpath%5D%0A%0A    return ':'.join(compos)%0A%0AHOST_PATH = _make_path(HOST_ABI)%0A%22%22%22%0AValue to be used as the %60%60PATH%60%60 env var on the host.%0A%22%22%22%0A%0Adef get_bin(name, abi=None):%0A    %22%22%22%0A    Return the path to a tool bundled in LISA.%0A%0A    :param abi: ABI of the binary. If %60%60abi%60%60 is not the host ABI,%0A        %60%60LISA_USE_SYSTEM_BIN%60%60 is ignored.%0A    :type abi: str or None%0A%0A    The result is influenced by the %60%60LISA_USE_SYSTEM_BIN%60%60 environment%0A    variable:%0A%0A        * If it is set to %60%600%60%60 or unset, it will give priority to the binaries%0A          bundled inside the :mod:%60lisa%60 package.%0A        * If it is set to %60%601%60%60, it will use the bundled binaries as a fallback%0A          only.%0A    %22%22%22%0A    path = shutil.which(name, path=_make_path(abi))%0A    if path:%0A        return Path(path).resolve()%0A    else:%0A        raise FileNotFoundError(f'Could not locate the tool: %7Bname%7D')
 %0A%0A# 

Make HOST_PATH depend on LISA_USE_SYSTEM_BIN