Skip to content

Commit fc13050

Browse files
committed
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
1 parent e04a55f commit fc13050

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

tools/toolchains/arm.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@
2424
from os import makedirs, write, remove
2525
from tempfile import mkstemp
2626
from shutil import rmtree
27-
from distutils.version import LooseVersion
27+
from sys import version_info
2828

2929
from tools.toolchains.mbed_toolchain import (
3030
mbedToolchain, TOOLCHAIN_PATHS, should_replace_small_c_lib
3131
)
3232
from tools.utils import mkdir, NotSupportedException, run_cmd
3333
from tools.resources import FileRef
3434

35+
if version_info >= (3,10):
36+
from packaging.version import Version
37+
else:
38+
from distutils.version import LooseVersion as Version
39+
3540
ARMC5_MIGRATION_WARNING = (
3641
"Warning: Arm Compiler 5 is no longer supported as of Mbed 6. "
3742
"Please upgrade your environment to Arm Compiler 6 "
@@ -59,7 +64,7 @@ class ARM(mbedToolchain):
5964
"Cortex-M0", "Cortex-M0+", "Cortex-M3", "Cortex-M4", "Cortex-M4F",
6065
"Cortex-M7", "Cortex-M7F", "Cortex-M7FD", "Cortex-A5", "Cortex-A9"
6166
]
62-
ARMCC_RANGE = (LooseVersion("5.06"), LooseVersion("5.07"))
67+
ARMCC_RANGE = (Version("5.06"), Version("5.07"))
6368
ARMCC_PRODUCT_RE = re.compile(b"Product: (.*)")
6469
ARMCC_VERSION_RE = re.compile(b"Component: ARM Compiler (\d+\.\d+)")
6570

@@ -142,7 +147,7 @@ def version_check(self):
142147
output = stdout.encode("utf-8")
143148
match = self.ARMCC_VERSION_RE.search(output)
144149
if match:
145-
found_version = LooseVersion(match.group(1).decode("utf-8"))
150+
found_version = Version(match.group(1).decode("utf-8"))
146151
else:
147152
found_version = None
148153
min_ver, max_ver = self.ARMCC_RANGE
@@ -546,7 +551,7 @@ class ARMC6(ARM_STD):
546551
"Cortex-M33-NS", "Cortex-M33F-NS", "Cortex-M33FE-NS", "Cortex-M33FE",
547552
"Cortex-A5", "Cortex-A9"
548553
]
549-
ARMCC_RANGE = (LooseVersion("6.10"), LooseVersion("7.0"))
554+
ARMCC_RANGE = (Version("6.10"), Version("7.0"))
550555
LD_DIAGNOSTIC_PATTERN = re.compile(
551556
'(?P<severity>Warning|Error): (?P<message>.+)'
552557
)

tools/toolchains/gcc.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@
1919
import fnmatch
2020
from os.path import join, basename, splitext, dirname, exists
2121
from os import getcwd, getenv
22-
from distutils.spawn import find_executable
23-
from distutils.version import LooseVersion
22+
from sys import version_info
2423

2524
from tools.toolchains.mbed_toolchain import (
2625
mbedToolchain, TOOLCHAIN_PATHS, should_replace_small_c_lib
2726
)
2827
from tools.utils import run_cmd
2928

29+
if version_info >= (3,10):
30+
from shutil import which
31+
from packaging.version import Version
32+
else:
33+
from distutils.spawn import find_executable as which
34+
from distutils.version import LooseVersion as Version
35+
3036

3137
class GCC(mbedToolchain):
3238
OFFICIALLY_SUPPORTED = True
@@ -36,7 +42,7 @@ class GCC(mbedToolchain):
3642
STD_LIB_NAME = "lib%s.a"
3743
DIAGNOSTIC_PATTERN = re.compile('((?P<file>[^:]+):(?P<line>\d+):)(?P<col>\d+):? (?P<severity>warning|[eE]rror|fatal error): (?P<message>.+)')
3844

39-
GCC_RANGE = (LooseVersion("9.0.0"), LooseVersion("10.0.0"))
45+
GCC_RANGE = (Version("9.0.0"), Version("10.0.0"))
4046
GCC_VERSION_RE = re.compile(b"\d+\.\d+\.\d+")
4147
DWARF_PRODUCER_RE = re.compile(r'(DW_AT_producer)(.*:\s*)(?P<producer>.*)')
4248

@@ -183,7 +189,7 @@ def version_check(self):
183189
msg = None
184190
match = self.GCC_VERSION_RE.search(stdout.encode("utf-8"))
185191
if match:
186-
found_version = LooseVersion(match.group(0).decode('utf-8'))
192+
found_version = Version(match.group(0).decode('utf-8'))
187193
else:
188194
found_version = None
189195
min_ver, max_ver = self.GCC_RANGE
@@ -395,7 +401,7 @@ def check_executable():
395401
not TOOLCHAIN_PATHS['GCC_ARM'] or
396402
not exists(TOOLCHAIN_PATHS['GCC_ARM'])
397403
):
398-
if find_executable('arm-none-eabi-gcc'):
404+
if which('arm-none-eabi-gcc'):
399405
TOOLCHAIN_PATHS['GCC_ARM'] = ''
400406
return True
401407
else:

tools/toolchains/iar.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
import re
1919
from os import remove
2020
from os.path import join, splitext, exists
21-
from distutils.version import LooseVersion
22-
21+
from sys import version_info
2322
from tools.toolchains.mbed_toolchain import (
2423
mbedToolchain, TOOLCHAIN_PATHS, should_replace_small_c_lib
2524
)
2625
from tools.utils import run_cmd
2726

27+
if version_info >= (3,10):
28+
from packaging.version import Version
29+
else:
30+
from distutils.version import LooseVersion as Version
31+
2832
class IAR(mbedToolchain):
2933
OFFICIALLY_SUPPORTED = True
3034
LIBRARY_EXT = '.a'
@@ -34,7 +38,7 @@ class IAR(mbedToolchain):
3438
DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)",(?P<line>[\d]+)\s+(?P<severity>Warning|Error|Fatal error)(?P<message>.+)')
3539
INDEX_PATTERN = re.compile('(?P<col>\s*)\^')
3640
IAR_VERSION_RE = re.compile(b"IAR ANSI C/C\+\+ Compiler V(\d+\.\d+)")
37-
IAR_VERSION = LooseVersion("8.32")
41+
IAR_VERSION = Version("8.32")
3842

3943
@staticmethod
4044
def check_executable():
@@ -123,7 +127,7 @@ def version_check(self):
123127
msg = None
124128
match = self.IAR_VERSION_RE.search(stdout.encode("utf-8"))
125129
found_version = match.group(1).decode("utf-8") if match else None
126-
if found_version and LooseVersion(found_version) != self.IAR_VERSION:
130+
if found_version and Version(found_version) != self.IAR_VERSION:
127131
msg = "Compiler version mismatch: Have {}; expected {}".format(
128132
found_version, self.IAR_VERSION)
129133
elif not match or len(match.groups()) != 1:

tools/toolchains/mbed_toolchain.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
from copy import deepcopy
3030
from collections import namedtuple
3131
from abc import ABCMeta, abstractmethod
32-
from distutils.spawn import find_executable
3332
from multiprocessing import Pool, cpu_count
3433
from hashlib import md5
34+
from sys import version_info
3535

3636
from ..utils import (
3737
run_cmd,
@@ -52,6 +52,11 @@
5252
from ..settings import ARM_PATH, ARMC6_PATH, GCC_ARM_PATH, IAR_PATH
5353
from future.utils import with_metaclass
5454

55+
if version_info >= (3,10):
56+
from shutil import which
57+
else:
58+
from distutils.spawn import find_executable as which
59+
5560

5661
TOOLCHAIN_PATHS = {
5762
'ARM': ARM_PATH,
@@ -1143,7 +1148,7 @@ def generic_check_executable(tool_key, executable_name, levels_up,
11431148
"""
11441149
if (not TOOLCHAIN_PATHS[tool_key] or
11451150
not exists(TOOLCHAIN_PATHS[tool_key])):
1146-
exe = find_executable(executable_name)
1151+
exe = which(executable_name)
11471152
if not exe:
11481153
return False
11491154
for level in range(levels_up):

0 commit comments

Comments
 (0)