Skip to content

Commit

Permalink
Remove distutils for newer python versions
Browse files Browse the repository at this point in the history
Due to PEP 632 distutils is deprecated in Python3.10 and Python3.11 and is removed with Python3.12
  • Loading branch information
tobiasjaster committed May 27, 2024
1 parent e04a55f commit f7317cc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
13 changes: 9 additions & 4 deletions tools/toolchains/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@
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
)
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 "
Expand Down Expand Up @@ -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+)")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
'(?P<severity>Warning|Error): (?P<message>.+)'
)
Expand Down
16 changes: 11 additions & 5 deletions tools/toolchains/gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +42,7 @@ class GCC(mbedToolchain):
STD_LIB_NAME = "lib%s.a"
DIAGNOSTIC_PATTERN = re.compile('((?P<file>[^:]+):(?P<line>\d+):)(?P<col>\d+):? (?P<severity>warning|[eE]rror|fatal error): (?P<message>.+)')

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<producer>.*)')

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 8 additions & 4 deletions tools/toolchains/iar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -34,7 +38,7 @@ class IAR(mbedToolchain):
DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)",(?P<line>[\d]+)\s+(?P<severity>Warning|Error|Fatal error)(?P<message>.+)')
INDEX_PATTERN = re.compile('(?P<col>\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():
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions tools/toolchains/mbed_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f7317cc

Please sign in to comment.