mirror of
https://github.com/espressif/esp-idf.git
synced 2025-10-03 18:40:59 +02:00
change: fix linting errors in python files
This commit is contained in:
@@ -6,7 +6,6 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from packaging.requirements import Requirement
|
from packaging.requirements import Requirement
|
||||||
@@ -24,13 +23,6 @@ from importlib.metadata import PackageNotFoundError
|
|||||||
from importlib.metadata import requires as _requires
|
from importlib.metadata import requires as _requires
|
||||||
from importlib.metadata import version as _version
|
from importlib.metadata import version as _version
|
||||||
|
|
||||||
try:
|
|
||||||
from typing import Set
|
|
||||||
except ImportError:
|
|
||||||
# This is a script run during the early phase of setting up the environment. So try to avoid failure caused by
|
|
||||||
# Python version incompatibility. The supported Python version is checked elsewhere.
|
|
||||||
pass
|
|
||||||
|
|
||||||
PYTHON_PACKAGE_RE = re.compile(r'[^<>=~]+')
|
PYTHON_PACKAGE_RE = re.compile(r'[^<>=~]+')
|
||||||
|
|
||||||
|
|
||||||
@@ -44,7 +36,7 @@ def validate_requirement_list(file_path: str) -> str:
|
|||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
open(file_path, encoding='utf-8').close()
|
open(file_path, encoding='utf-8').close()
|
||||||
except IOError as e:
|
except OSError as e:
|
||||||
raise argparse.ArgumentTypeError(f'Cannot read requirement file {file_path}: {e}')
|
raise argparse.ArgumentTypeError(f'Cannot read requirement file {file_path}: {e}')
|
||||||
return file_path
|
return file_path
|
||||||
|
|
||||||
@@ -68,7 +60,7 @@ def get_version(name: str) -> str:
|
|||||||
return version
|
return version
|
||||||
|
|
||||||
|
|
||||||
def get_requires(name: str) -> Optional[list]:
|
def get_requires(name: str) -> list | None:
|
||||||
try:
|
try:
|
||||||
requires = _requires(name)
|
requires = _requires(name)
|
||||||
except PackageNotFoundError:
|
except PackageNotFoundError:
|
||||||
@@ -112,20 +104,20 @@ if __name__ == '__main__':
|
|||||||
elif con.startswith('-e') and '#egg=' in con:
|
elif con.startswith('-e') and '#egg=' in con:
|
||||||
con_m = re.search(r'#egg=([^\s]+)', con)
|
con_m = re.search(r'#egg=([^\s]+)', con)
|
||||||
if not con_m:
|
if not con_m:
|
||||||
print('Malformed input. Cannot find name in {}'.format(con))
|
print(f'Malformed input. Cannot find name in {con}')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
con = con_m[1]
|
con = con_m[1]
|
||||||
|
|
||||||
name_m = PYTHON_PACKAGE_RE.search(con)
|
name_m = PYTHON_PACKAGE_RE.search(con)
|
||||||
if not name_m:
|
if not name_m:
|
||||||
print('Malformed input. Cannot find name in {}'.format(con))
|
print(f'Malformed input. Cannot find name in {con}')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
constr_dict[name_m[0]] = con.partition(' #')[0] # remove comments
|
constr_dict[name_m[0]] = con.partition(' #')[0] # remove comments
|
||||||
|
|
||||||
not_satisfied = [] # in string form which will be printed
|
not_satisfied = [] # in string form which will be printed
|
||||||
|
|
||||||
# already_checked set is used in order to avoid circular checks which would cause looping.
|
# already_checked set is used in order to avoid circular checks which would cause looping.
|
||||||
already_checked: Set[Requirement] = set()
|
already_checked: set[Requirement] = set()
|
||||||
|
|
||||||
# required_set contains package names in string form without version constraints. If the package has a constraint
|
# required_set contains package names in string form without version constraints. If the package has a constraint
|
||||||
# specification (package name + version requirement) then use that instead. new_req_list is used to store
|
# specification (package name + version requirement) then use that instead. new_req_list is used to store
|
||||||
@@ -192,7 +184,7 @@ if __name__ == '__main__':
|
|||||||
# We are running inside a private virtual environment under IDF_TOOLS_PATH,
|
# We are running inside a private virtual environment under IDF_TOOLS_PATH,
|
||||||
# ask the user to run install.bat again.
|
# ask the user to run install.bat again.
|
||||||
install_script = 'install.bat' if sys.platform == 'win32' else 'install.sh'
|
install_script = 'install.bat' if sys.platform == 'win32' else 'install.sh'
|
||||||
print('To install the missing packages, please run "{}"'.format(install_script))
|
print(f'To install the missing packages, please run "{install_script}"')
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
'Please follow the instructions found in the "Set up the tools" section of '
|
'Please follow the instructions found in the "Set up the tools" section of '
|
||||||
@@ -202,7 +194,7 @@ if __name__ == '__main__':
|
|||||||
print('Diagnostic information:')
|
print('Diagnostic information:')
|
||||||
idf_python_env_path = os.environ.get('IDF_PYTHON_ENV_PATH')
|
idf_python_env_path = os.environ.get('IDF_PYTHON_ENV_PATH')
|
||||||
print(' IDF_PYTHON_ENV_PATH: {}'.format(idf_python_env_path or '(not set)'))
|
print(' IDF_PYTHON_ENV_PATH: {}'.format(idf_python_env_path or '(not set)'))
|
||||||
print(' Python interpreter used: {}'.format(sys.executable))
|
print(f' Python interpreter used: {sys.executable}')
|
||||||
if not idf_python_env_path or idf_python_env_path not in sys.executable:
|
if not idf_python_env_path or idf_python_env_path not in sys.executable:
|
||||||
print(' Warning: python interpreter not running from IDF_PYTHON_ENV_PATH')
|
print(' Warning: python interpreter not running from IDF_PYTHON_ENV_PATH')
|
||||||
print(' PATH: {}'.format(os.getenv('PATH')))
|
print(' PATH: {}'.format(os.getenv('PATH')))
|
||||||
|
@@ -1,39 +1,33 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
#
|
#
|
||||||
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
# SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
import argparse
|
import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
from sys import exit
|
import sys
|
||||||
|
|
||||||
try:
|
|
||||||
from typing import List
|
|
||||||
except ImportError:
|
|
||||||
# Only used for type annotations
|
|
||||||
pass
|
|
||||||
|
|
||||||
IGNORE_LIST_MYPY = 'tools/ci/mypy_ignore_list.txt'
|
IGNORE_LIST_MYPY = 'tools/ci/mypy_ignore_list.txt'
|
||||||
|
|
||||||
|
|
||||||
def types_valid_global_rules(file_name, ignorelisted): # type: (str, bool) -> bool
|
def types_valid_global_rules(file_name: str, ignorelisted: bool) -> bool:
|
||||||
"""
|
"""
|
||||||
Run Mypy check with global rules on the given file, return TRUE if Mypy check passes
|
Run Mypy check with global rules on the given file, return TRUE if Mypy check passes
|
||||||
"""
|
"""
|
||||||
output = subprocess.DEVNULL if ignorelisted else None
|
output = subprocess.DEVNULL if ignorelisted else None
|
||||||
mypy_exit_code = subprocess.call('mypy {}'.format(file_name), shell=True, stdout=output)
|
mypy_exit_code = subprocess.call(f'mypy {file_name}', shell=True, stdout=output)
|
||||||
|
|
||||||
return not bool(mypy_exit_code)
|
return not bool(mypy_exit_code)
|
||||||
|
|
||||||
|
|
||||||
def types_valid_ignored_rules(file_name): # type: (str) -> bool
|
def types_valid_ignored_rules(file_name: str) -> bool:
|
||||||
"""
|
"""
|
||||||
Run Mypy check with rules for ignore list on the given file, return TRUE if Mypy check passes
|
Run Mypy check with rules for ignore list on the given file, return TRUE if Mypy check passes
|
||||||
"""
|
"""
|
||||||
mypy_exit_code = subprocess.call('mypy {} --python-version 3.10 --allow-untyped-defs'.format(file_name), shell=True)
|
mypy_exit_code = subprocess.call(f'mypy {file_name} --python-version 3.10 --allow-untyped-defs', shell=True)
|
||||||
return not bool(mypy_exit_code)
|
return not bool(mypy_exit_code)
|
||||||
|
|
||||||
|
|
||||||
def check_files(files): # type: (List[str]) -> List[str]
|
def check_files(files: list[str]) -> list[str]:
|
||||||
"""
|
"""
|
||||||
Check files for type annotatins:
|
Check files for type annotatins:
|
||||||
- new python file -> run Mypy check with global rules
|
- new python file -> run Mypy check with global rules
|
||||||
@@ -45,7 +39,7 @@ def check_files(files): # type: (List[str]) -> List[str]
|
|||||||
"""
|
"""
|
||||||
type_issues = []
|
type_issues = []
|
||||||
|
|
||||||
with open(IGNORE_LIST_MYPY, 'r') as f:
|
with open(IGNORE_LIST_MYPY) as f:
|
||||||
ignore_list = [item.strip() for item in f.readlines()]
|
ignore_list = [item.strip() for item in f.readlines()]
|
||||||
updated_ignore_list = ignore_list.copy()
|
updated_ignore_list = ignore_list.copy()
|
||||||
|
|
||||||
@@ -53,7 +47,7 @@ def check_files(files): # type: (List[str]) -> List[str]
|
|||||||
if file_name in ignore_list:
|
if file_name in ignore_list:
|
||||||
if types_valid_global_rules(file_name, ignorelisted=True):
|
if types_valid_global_rules(file_name, ignorelisted=True):
|
||||||
updated_ignore_list.remove(file_name)
|
updated_ignore_list.remove(file_name)
|
||||||
print('\33[93m\n File {} removed from ignore list - run commit again! \n\33[0m'.format(file_name))
|
print(f'\33[93m\n File {file_name} removed from ignore list - run commit again! \n\33[0m')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if types_valid_ignored_rules(file_name):
|
if types_valid_ignored_rules(file_name):
|
||||||
@@ -68,12 +62,12 @@ def check_files(files): # type: (List[str]) -> List[str]
|
|||||||
if updated_ignore_list != ignore_list:
|
if updated_ignore_list != ignore_list:
|
||||||
with open(IGNORE_LIST_MYPY, 'w') as f:
|
with open(IGNORE_LIST_MYPY, 'w') as f:
|
||||||
for item in updated_ignore_list:
|
for item in updated_ignore_list:
|
||||||
f.write('{}\n'.format(item))
|
f.write(f'{item}\n')
|
||||||
|
|
||||||
return type_issues
|
return type_issues
|
||||||
|
|
||||||
|
|
||||||
def main(): # type: () -> None
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@@ -84,7 +78,7 @@ def main(): # type: () -> None
|
|||||||
print('mypy check failed for:')
|
print('mypy check failed for:')
|
||||||
for file_name in type_issues:
|
for file_name in type_issues:
|
||||||
print('\t', file_name)
|
print('\t', file_name)
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
#
|
#
|
||||||
# SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
# SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
@@ -11,32 +11,24 @@
|
|||||||
# (python3.10, python3.11, ...) cannot be hardcoded there and at the end, the user is responsible to set-up a system
|
# (python3.10, python3.11, ...) cannot be hardcoded there and at the end, the user is responsible to set-up a system
|
||||||
# where "python" or "python3" of compatible version is available.
|
# where "python" or "python3" of compatible version is available.
|
||||||
import sys
|
import sys
|
||||||
|
from collections.abc import Iterable
|
||||||
try:
|
|
||||||
# Python 2 is not supported anymore but still the old way of typing is used here in order to give a nice Python
|
|
||||||
# version failure and not a typing exception.
|
|
||||||
from typing import Iterable
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
OLDEST_PYTHON_SUPPORTED = (3, 10) # keep it as tuple for comparison with sys.version_info
|
OLDEST_PYTHON_SUPPORTED = (3, 10) # keep it as tuple for comparison with sys.version_info
|
||||||
|
|
||||||
|
|
||||||
def _ver_to_str(it): # type: (Iterable) -> str
|
def _ver_to_str(it: Iterable) -> str:
|
||||||
return '.'.join(str(x) for x in it)
|
return '.'.join(str(x) for x in it)
|
||||||
|
|
||||||
|
|
||||||
def is_supported(): # type: () -> bool
|
def is_supported() -> bool:
|
||||||
return sys.version_info[:2] >= OLDEST_PYTHON_SUPPORTED[:2]
|
return sys.version_info[:2] >= OLDEST_PYTHON_SUPPORTED[:2]
|
||||||
|
|
||||||
|
|
||||||
def check(): # type: () -> None
|
def check() -> None:
|
||||||
if not is_supported():
|
if not is_supported():
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
'ESP-IDF supports Python {} or newer but you are using Python {}. Please upgrade your '
|
f'ESP-IDF supports Python {_ver_to_str(OLDEST_PYTHON_SUPPORTED)} or newer but you are using Python '
|
||||||
'installation as described in the documentation.'.format(
|
f'{_ver_to_str(sys.version_info[:3])}. Please upgrade your installation as described in the documentation.'
|
||||||
_ver_to_str(OLDEST_PYTHON_SUPPORTED), _ver_to_str(sys.version_info[:3])
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user