From 25d126f26894cd93e5b58e4147d2e48d90782d4f Mon Sep 17 00:00:00 2001 From: Tobias Jaster Date: Fri, 24 May 2024 22:25:24 +0200 Subject: [PATCH] Remove distutils for newer python versions Due to PEP 632 distutils is deprecated in Python3.10 and Python3.11 and is removed with Python3.12 --- tools/test/toolchains/api_test.py | 2 +- tools/toolchains/arm.py | 13 +++++++++---- tools/toolchains/gcc.py | 16 +++++++++++----- tools/toolchains/iar.py | 12 ++++++++---- tools/toolchains/mbed_toolchain.py | 9 +++++++-- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/tools/test/toolchains/api_test.py b/tools/test/toolchains/api_test.py index fde4a1180fe..5e7cd6a58c0 100644 --- a/tools/test/toolchains/api_test.py +++ b/tools/test/toolchains/api_test.py @@ -351,7 +351,7 @@ def test_detect_duplicates(filenames): @settings(max_examples=20) def test_path_specified_gcc(gcc_loc, exists_at_loc, exists_in_path): with patch('tools.toolchains.gcc.exists') as _exists: - with patch('tools.toolchains.gcc.find_executable') as _find: + with patch('tools.toolchains.gcc.which') as _find: _exists.return_value = exists_at_loc _find.return_value = exists_in_path TOOLCHAIN_PATHS['GCC_ARM'] = gcc_loc diff --git a/tools/toolchains/arm.py b/tools/toolchains/arm.py index bc85658d06b..3d26662b2bc 100644 --- a/tools/toolchains/arm.py +++ b/tools/toolchains/arm.py @@ -24,7 +24,7 @@ from os import makedirs, write, remove from tempfile import mkstemp from shutil import rmtree -from distutils.version import LooseVersion +from sys import version_info from tools.toolchains.mbed_toolchain import ( mbedToolchain, TOOLCHAIN_PATHS, should_replace_small_c_lib @@ -32,6 +32,11 @@ from tools.utils import mkdir, NotSupportedException, run_cmd from tools.resources import FileRef +if version_info >= (3,10): + from packaging.version import Version +else: + from distutils.version import LooseVersion as Version + ARMC5_MIGRATION_WARNING = ( "Warning: Arm Compiler 5 is no longer supported as of Mbed 6. " "Please upgrade your environment to Arm Compiler 6 " @@ -59,7 +64,7 @@ class ARM(mbedToolchain): "Cortex-M0", "Cortex-M0+", "Cortex-M3", "Cortex-M4", "Cortex-M4F", "Cortex-M7", "Cortex-M7F", "Cortex-M7FD", "Cortex-A5", "Cortex-A9" ] - ARMCC_RANGE = (LooseVersion("5.06"), LooseVersion("5.07")) + ARMCC_RANGE = (Version("5.06"), Version("5.07")) ARMCC_PRODUCT_RE = re.compile(b"Product: (.*)") ARMCC_VERSION_RE = re.compile(b"Component: ARM Compiler (\d+\.\d+)") @@ -142,7 +147,7 @@ def version_check(self): output = stdout.encode("utf-8") match = self.ARMCC_VERSION_RE.search(output) if match: - found_version = LooseVersion(match.group(1).decode("utf-8")) + found_version = Version(match.group(1).decode("utf-8")) else: found_version = None min_ver, max_ver = self.ARMCC_RANGE @@ -546,7 +551,7 @@ class ARMC6(ARM_STD): "Cortex-M33-NS", "Cortex-M33F-NS", "Cortex-M33FE-NS", "Cortex-M33FE", "Cortex-A5", "Cortex-A9" ] - ARMCC_RANGE = (LooseVersion("6.10"), LooseVersion("7.0")) + ARMCC_RANGE = (Version("6.10"), Version("7.0")) LD_DIAGNOSTIC_PATTERN = re.compile( '(?PWarning|Error): (?P.+)' ) diff --git a/tools/toolchains/gcc.py b/tools/toolchains/gcc.py index 762f4eecb7b..901e62a5f1e 100644 --- a/tools/toolchains/gcc.py +++ b/tools/toolchains/gcc.py @@ -19,14 +19,20 @@ import fnmatch from os.path import join, basename, splitext, dirname, exists from os import getcwd, getenv -from distutils.spawn import find_executable -from distutils.version import LooseVersion +from sys import version_info from tools.toolchains.mbed_toolchain import ( mbedToolchain, TOOLCHAIN_PATHS, should_replace_small_c_lib ) from tools.utils import run_cmd +if version_info >= (3,10): + from shutil import which + from packaging.version import Version +else: + from distutils.spawn import find_executable as which + from distutils.version import LooseVersion as Version + class GCC(mbedToolchain): OFFICIALLY_SUPPORTED = True @@ -36,7 +42,7 @@ class GCC(mbedToolchain): STD_LIB_NAME = "lib%s.a" DIAGNOSTIC_PATTERN = re.compile('((?P[^:]+):(?P\d+):)(?P\d+):? (?Pwarning|[eE]rror|fatal error): (?P.+)') - GCC_RANGE = (LooseVersion("9.0.0"), LooseVersion("10.0.0")) + GCC_RANGE = (Version("9.0.0"), Version("10.0.0")) GCC_VERSION_RE = re.compile(b"\d+\.\d+\.\d+") DWARF_PRODUCER_RE = re.compile(r'(DW_AT_producer)(.*:\s*)(?P.*)') @@ -183,7 +189,7 @@ def version_check(self): msg = None match = self.GCC_VERSION_RE.search(stdout.encode("utf-8")) if match: - found_version = LooseVersion(match.group(0).decode('utf-8')) + found_version = Version(match.group(0).decode('utf-8')) else: found_version = None min_ver, max_ver = self.GCC_RANGE @@ -395,7 +401,7 @@ def check_executable(): not TOOLCHAIN_PATHS['GCC_ARM'] or not exists(TOOLCHAIN_PATHS['GCC_ARM']) ): - if find_executable('arm-none-eabi-gcc'): + if which('arm-none-eabi-gcc'): TOOLCHAIN_PATHS['GCC_ARM'] = '' return True else: diff --git a/tools/toolchains/iar.py b/tools/toolchains/iar.py index cc7764840c5..d070cc8221d 100644 --- a/tools/toolchains/iar.py +++ b/tools/toolchains/iar.py @@ -18,13 +18,17 @@ import re from os import remove from os.path import join, splitext, exists -from distutils.version import LooseVersion - +from sys import version_info from tools.toolchains.mbed_toolchain import ( mbedToolchain, TOOLCHAIN_PATHS, should_replace_small_c_lib ) from tools.utils import run_cmd +if version_info >= (3,10): + from packaging.version import Version +else: + from distutils.version import LooseVersion as Version + class IAR(mbedToolchain): OFFICIALLY_SUPPORTED = True LIBRARY_EXT = '.a' @@ -34,7 +38,7 @@ class IAR(mbedToolchain): DIAGNOSTIC_PATTERN = re.compile('"(?P[^"]+)",(?P[\d]+)\s+(?PWarning|Error|Fatal error)(?P.+)') INDEX_PATTERN = re.compile('(?P\s*)\^') IAR_VERSION_RE = re.compile(b"IAR ANSI C/C\+\+ Compiler V(\d+\.\d+)") - IAR_VERSION = LooseVersion("8.32") + IAR_VERSION = Version("8.32") @staticmethod def check_executable(): @@ -123,7 +127,7 @@ def version_check(self): msg = None match = self.IAR_VERSION_RE.search(stdout.encode("utf-8")) found_version = match.group(1).decode("utf-8") if match else None - if found_version and LooseVersion(found_version) != self.IAR_VERSION: + if found_version and Version(found_version) != self.IAR_VERSION: msg = "Compiler version mismatch: Have {}; expected {}".format( found_version, self.IAR_VERSION) elif not match or len(match.groups()) != 1: diff --git a/tools/toolchains/mbed_toolchain.py b/tools/toolchains/mbed_toolchain.py index 51429c00128..99ecc501508 100755 --- a/tools/toolchains/mbed_toolchain.py +++ b/tools/toolchains/mbed_toolchain.py @@ -29,9 +29,9 @@ from copy import deepcopy from collections import namedtuple from abc import ABCMeta, abstractmethod -from distutils.spawn import find_executable from multiprocessing import Pool, cpu_count from hashlib import md5 +from sys import version_info from ..utils import ( run_cmd, @@ -52,6 +52,11 @@ from ..settings import ARM_PATH, ARMC6_PATH, GCC_ARM_PATH, IAR_PATH from future.utils import with_metaclass +if version_info >= (3,10): + from shutil import which +else: + from distutils.spawn import find_executable as which + TOOLCHAIN_PATHS = { 'ARM': ARM_PATH, @@ -1143,7 +1148,7 @@ def generic_check_executable(tool_key, executable_name, levels_up, """ if (not TOOLCHAIN_PATHS[tool_key] or not exists(TOOLCHAIN_PATHS[tool_key])): - exe = find_executable(executable_name) + exe = which(executable_name) if not exe: return False for level in range(levels_up):