From 20198b9618d21e394d0b1599adefe46ec1c03102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20L=C3=B3pez?= Date: Wed, 4 Jan 2023 17:57:28 -0300 Subject: [PATCH] slither-doctor: fix `is_relative_to` on Python 3.8 --- slither/tools/doctor/checks/paths.py | 33 ++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/slither/tools/doctor/checks/paths.py b/slither/tools/doctor/checks/paths.py index 53fae78ad..4adf3b159 100644 --- a/slither/tools/doctor/checks/paths.py +++ b/slither/tools/doctor/checks/paths.py @@ -1,12 +1,41 @@ from pathlib import Path from typing import List, Optional, Tuple import shutil +import sys import sysconfig from slither.utils.colors import yellow, green, red +def path_is_relative_to(path: Path, relative_to: Path) -> bool: + """ + Check if a path is relative to another one. + + Compatibility wrapper for Path.is_relative_to + """ + if sys.version_info >= (3, 9, 0): + return path.is_relative_to(relative_to) + + path_parts = path.resolve().parts + relative_to_parts = relative_to.resolve().parts + + if len(path_parts) < len(relative_to_parts): + return False + + for (a, b) in zip(path_parts, relative_to_parts): + if a != b: + return False + + return True + + def check_path_config(name: str) -> Tuple[bool, Optional[Path], List[Path]]: + """ + Check if a given Python binary/script is in PATH. + :return: Returns if the binary on PATH corresponds to this installation, + its path (if present), and a list of possible paths where this + binary might be found. + """ binary_path = shutil.which(name) possible_paths = [] @@ -21,9 +50,9 @@ def check_path_config(name: str) -> Tuple[bool, Optional[Path], List[Path]]: if binary_path is not None: binary_path = Path(binary_path) this_code = Path(__file__) - this_binary = list(filter(lambda x: this_code.is_relative_to(x[1]), possible_paths)) + this_binary = list(filter(lambda x: path_is_relative_to(this_code, x[1]), possible_paths)) binary_here = len(this_binary) > 0 and all( - binary_path.is_relative_to(script) for script, _ in this_binary + path_is_relative_to(binary_path, script) for script, _ in this_binary ) return binary_here, binary_path, list(set(script for script, _ in possible_paths))