2018-01-23 01:08:28 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2021-09-16 10:48:03 -04:00
|
|
|
# SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
|
2021-05-26 04:44:20 -04:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
2018-01-23 01:08:28 -05:00
|
|
|
# 'idf.py' is a top-level config/build command line tool for ESP-IDF
|
|
|
|
#
|
|
|
|
# You don't have to use idf.py, you can use cmake directly
|
|
|
|
# (or use cmake in an IDE)
|
2018-09-03 06:24:17 -04:00
|
|
|
|
2018-10-25 02:16:30 -04:00
|
|
|
# WARNING: we don't check for Python build-time dependencies until
|
2018-09-03 06:24:17 -04:00
|
|
|
# check_environment() function below. If possible, avoid importing
|
|
|
|
# any external libraries here - put in external script, or import in
|
|
|
|
# their specific function instead.
|
2022-06-03 08:46:56 -04:00
|
|
|
from __future__ import annotations
|
2021-01-25 21:49:01 -05:00
|
|
|
|
2019-04-10 12:06:52 -04:00
|
|
|
import codecs
|
|
|
|
import json
|
|
|
|
import locale
|
2018-01-23 01:08:28 -05:00
|
|
|
import os
|
|
|
|
import os.path
|
2021-04-19 05:03:43 -04:00
|
|
|
import signal
|
2019-04-10 12:06:52 -04:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2022-06-03 08:46:56 -04:00
|
|
|
from collections import Counter, OrderedDict, _OrderedDictKeysView
|
2019-10-03 12:26:44 -04:00
|
|
|
from importlib import import_module
|
|
|
|
from pkgutil import iter_modules
|
2022-06-03 08:46:56 -04:00
|
|
|
from types import FrameType
|
2022-09-09 16:59:45 -04:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, Union
|
2018-01-23 01:08:28 -05:00
|
|
|
|
2020-05-19 10:43:29 -04:00
|
|
|
# pyc files remain in the filesystem when switching between branches which might raise errors for incompatible
|
2020-11-02 08:11:14 -05:00
|
|
|
# idf.py extensions. Therefore, pyc file generation is turned off:
|
2020-05-19 10:43:29 -04:00
|
|
|
sys.dont_write_bytecode = True
|
|
|
|
|
2021-05-26 04:44:20 -04:00
|
|
|
import python_version_checker # noqa: E402
|
2022-02-10 10:58:16 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
from idf_py_actions.errors import FatalError # noqa: E402
|
2022-09-09 16:59:45 -04:00
|
|
|
from idf_py_actions.tools import (PROG, SHELL_COMPLETE_RUN, SHELL_COMPLETE_VAR, PropertyDict, # noqa: E402
|
2023-01-17 14:51:46 -05:00
|
|
|
debug_print_idf_version, get_target, merge_action_lists, print_warning)
|
2022-08-08 03:43:31 -04:00
|
|
|
if os.getenv('IDF_COMPONENT_MANAGER') != '0':
|
|
|
|
from idf_component_manager import idf_extensions
|
2023-08-30 02:14:50 -04:00
|
|
|
except ImportError as e:
|
2022-02-10 10:58:16 -05:00
|
|
|
# For example, importing click could cause this.
|
2023-08-30 02:14:50 -04:00
|
|
|
print((f'Cannot import module "{e.name}". This usually means that "idf.py" was not '
|
|
|
|
f'spawned within an ESP-IDF shell environment or the python virtual '
|
|
|
|
f'environment used by "idf.py" is corrupted.\n'
|
|
|
|
f'Please use idf.py only in an ESP-IDF shell environment. If problem persists, '
|
|
|
|
f'please try to install ESP-IDF tools again as described in the Get Started guide.'),
|
|
|
|
file=sys.stderr)
|
|
|
|
|
2022-02-10 10:58:16 -05:00
|
|
|
sys.exit(1)
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-01-23 01:08:28 -05:00
|
|
|
# Use this Python interpreter for any subprocesses we launch
|
2018-12-04 07:46:48 -05:00
|
|
|
PYTHON = sys.executable
|
2018-01-23 01:08:28 -05:00
|
|
|
|
|
|
|
# note: os.environ changes don't automatically propagate to child processes,
|
2018-06-15 00:59:45 -04:00
|
|
|
# you have to pass env=os.environ explicitly anywhere that we create a process
|
2021-01-25 21:49:01 -05:00
|
|
|
os.environ['PYTHON'] = sys.executable
|
2018-01-23 01:08:28 -05:00
|
|
|
|
2020-11-07 06:45:54 -05:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def check_environment() -> List:
|
2018-01-23 01:08:28 -05:00
|
|
|
"""
|
|
|
|
Verify the environment contains the top-level tools we need to operate
|
|
|
|
|
|
|
|
(cmake will check a lot of other things)
|
|
|
|
"""
|
2019-08-15 09:42:15 -04:00
|
|
|
checks_output = []
|
|
|
|
|
|
|
|
# verify that IDF_PATH env variable is set
|
2018-01-23 01:08:28 -05:00
|
|
|
# find the directory idf.py is in, then the parent directory of this, and assume this is IDF_PATH
|
2023-01-17 14:51:46 -05:00
|
|
|
detected_idf_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
|
2021-01-25 21:49:01 -05:00
|
|
|
if 'IDF_PATH' in os.environ:
|
2023-01-17 14:51:46 -05:00
|
|
|
set_idf_path = os.path.realpath(os.environ['IDF_PATH'])
|
2018-01-23 01:08:28 -05:00
|
|
|
if set_idf_path != detected_idf_path:
|
2020-11-07 06:45:54 -05:00
|
|
|
print_warning(
|
2021-01-25 21:49:01 -05:00
|
|
|
'WARNING: IDF_PATH environment variable is set to %s but %s path indicates IDF directory %s. '
|
|
|
|
'Using the environment variable directory, but results may be unexpected...' %
|
2019-11-08 10:46:02 -05:00
|
|
|
(set_idf_path, PROG, detected_idf_path))
|
2018-01-23 01:08:28 -05:00
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
print_warning('Setting IDF_PATH environment variable: %s' % detected_idf_path)
|
|
|
|
os.environ['IDF_PATH'] = detected_idf_path
|
2018-01-23 01:08:28 -05:00
|
|
|
|
2021-05-26 04:44:20 -04:00
|
|
|
try:
|
|
|
|
# The Python compatibility check could have been done earlier (tools/detect_python.{sh,fish}) but PATH is
|
|
|
|
# not set for import at that time. Even if the check would be done before, the same check needs to be done
|
|
|
|
# here as well (for example one can call idf.py from a not properly set-up environment).
|
|
|
|
python_version_checker.check()
|
|
|
|
except RuntimeError as e:
|
|
|
|
raise FatalError(e)
|
2020-11-04 17:32:40 -05:00
|
|
|
|
2022-01-24 08:53:37 -05:00
|
|
|
# check Python dependencies
|
|
|
|
checks_output.append('Checking Python dependencies...')
|
|
|
|
try:
|
|
|
|
out = subprocess.check_output(
|
|
|
|
[
|
|
|
|
os.environ['PYTHON'],
|
|
|
|
os.path.join(os.environ['IDF_PATH'], 'tools', 'idf_tools.py'),
|
|
|
|
'check-python-dependencies',
|
|
|
|
],
|
|
|
|
env=os.environ,
|
|
|
|
)
|
|
|
|
|
|
|
|
checks_output.append(out.decode('utf-8', 'ignore').strip())
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print_warning(e.output.decode('utf-8', 'ignore'), stream=sys.stderr)
|
|
|
|
debug_print_idf_version()
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
2019-08-15 09:42:15 -04:00
|
|
|
return checks_output
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def _safe_relpath(path: str, start: Optional[str]=None) -> str:
|
2019-03-01 00:12:03 -05:00
|
|
|
""" Return a relative path, same as os.path.relpath, but only if this is possible.
|
|
|
|
|
|
|
|
It is not possible on Windows, if the start directory and the path are on different drives.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return os.path.relpath(path, os.curdir if start is None else start)
|
|
|
|
except ValueError:
|
|
|
|
return os.path.abspath(path)
|
|
|
|
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def init_cli(verbose_output: List=None) -> Any:
|
2019-04-10 12:06:52 -04:00
|
|
|
# Click is imported here to run it after check_environment()
|
|
|
|
import click
|
|
|
|
|
2019-11-14 07:48:24 -05:00
|
|
|
class Deprecation(object):
|
2019-07-02 14:33:32 -04:00
|
|
|
"""Construct deprecation notice for help messages"""
|
2022-06-03 08:46:56 -04:00
|
|
|
def __init__(self, deprecated: Union[Dict, str, bool]=False) -> None:
|
2019-07-02 14:33:32 -04:00
|
|
|
self.deprecated = deprecated
|
|
|
|
self.since = None
|
|
|
|
self.removed = None
|
2019-11-14 07:48:24 -05:00
|
|
|
self.exit_with_error = None
|
2021-01-25 21:49:01 -05:00
|
|
|
self.custom_message = ''
|
2019-07-02 14:33:32 -04:00
|
|
|
|
|
|
|
if isinstance(deprecated, dict):
|
2021-01-25 21:49:01 -05:00
|
|
|
self.custom_message = deprecated.get('message', '')
|
|
|
|
self.since = deprecated.get('since', None)
|
|
|
|
self.removed = deprecated.get('removed', None)
|
|
|
|
self.exit_with_error = deprecated.get('exit_with_error', None)
|
2019-07-02 14:33:32 -04:00
|
|
|
elif isinstance(deprecated, str):
|
|
|
|
self.custom_message = deprecated
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def full_message(self, type: str='Option') -> str:
|
2019-11-14 07:48:24 -05:00
|
|
|
if self.exit_with_error:
|
2021-01-25 21:49:01 -05:00
|
|
|
return '%s is deprecated %sand was removed%s.%s' % (
|
2019-11-14 07:48:24 -05:00
|
|
|
type,
|
2021-01-25 21:49:01 -05:00
|
|
|
'since %s ' % self.since if self.since else '',
|
|
|
|
' in %s' % self.removed if self.removed else '',
|
|
|
|
' %s' % self.custom_message if self.custom_message else '',
|
2019-11-14 07:48:24 -05:00
|
|
|
)
|
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
return '%s is deprecated %sand will be removed in%s.%s' % (
|
2019-11-14 07:48:24 -05:00
|
|
|
type,
|
2021-01-25 21:49:01 -05:00
|
|
|
'since %s ' % self.since if self.since else '',
|
|
|
|
' %s' % self.removed if self.removed else ' future versions',
|
|
|
|
' %s' % self.custom_message if self.custom_message else '',
|
2019-11-14 07:48:24 -05:00
|
|
|
)
|
2019-07-02 14:33:32 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def help(self, text: str, type: str='Option', separator: str=' ') -> str:
|
2021-01-25 21:49:01 -05:00
|
|
|
text = text or ''
|
2019-07-02 14:33:32 -04:00
|
|
|
return self.full_message(type) + separator + text if self.deprecated else text
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def short_help(self, text: str) -> str:
|
2021-01-25 21:49:01 -05:00
|
|
|
text = text or ''
|
|
|
|
return ('Deprecated! ' + text) if self.deprecated else text
|
2019-07-02 14:33:32 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def check_deprecation(ctx: click.core.Context) -> None:
|
2020-11-02 08:11:14 -05:00
|
|
|
"""Prints deprecation warnings for arguments in given context"""
|
2019-07-02 14:33:32 -04:00
|
|
|
for option in ctx.command.params:
|
|
|
|
default = () if option.multiple else option.default
|
|
|
|
if isinstance(option, Option) and option.deprecated and ctx.params[option.name] != default:
|
2019-11-14 07:48:24 -05:00
|
|
|
deprecation = Deprecation(option.deprecated)
|
|
|
|
if deprecation.exit_with_error:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise FatalError('Error: %s' % deprecation.full_message('Option "%s"' % option.name))
|
2019-11-14 07:48:24 -05:00
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
print_warning('Warning: %s' % deprecation.full_message('Option "%s"' % option.name))
|
2019-07-02 14:33:32 -04:00
|
|
|
|
2019-04-10 12:06:52 -04:00
|
|
|
class Task(object):
|
2022-06-03 08:46:56 -04:00
|
|
|
def __init__(self, callback: Callable, name: str, aliases: List, dependencies: Optional[List],
|
|
|
|
order_dependencies: Optional[List], action_args: Dict) -> None:
|
2019-04-10 12:06:52 -04:00
|
|
|
self.callback = callback
|
|
|
|
self.name = name
|
|
|
|
self.dependencies = dependencies
|
|
|
|
self.order_dependencies = order_dependencies
|
|
|
|
self.action_args = action_args
|
|
|
|
self.aliases = aliases
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def __call__(self, context: click.core.Context, global_args: PropertyDict, action_args: Dict=None) -> None:
|
2019-06-12 13:10:16 -04:00
|
|
|
if action_args is None:
|
|
|
|
action_args = self.action_args
|
|
|
|
|
|
|
|
self.callback(self.name, context, global_args, **action_args)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
|
|
|
class Action(click.Command):
|
2019-11-08 10:46:02 -05:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-06-03 08:46:56 -04:00
|
|
|
name: Optional[str]=None,
|
|
|
|
aliases: Optional[List]=None,
|
|
|
|
deprecated: Union[Dict, str, bool]=False,
|
|
|
|
dependencies: Optional[List]=None,
|
|
|
|
order_dependencies: Optional[List]=None,
|
|
|
|
hidden: bool=False,
|
|
|
|
**kwargs: Any) -> None:
|
2019-04-10 12:06:52 -04:00
|
|
|
super(Action, self).__init__(name, **kwargs)
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
self.name: str = self.name or self.callback.__name__
|
|
|
|
self.deprecated: Union[Dict, str, bool] = deprecated
|
|
|
|
self.hidden: bool = hidden
|
2019-04-10 12:06:52 -04:00
|
|
|
|
|
|
|
if aliases is None:
|
|
|
|
aliases = []
|
|
|
|
self.aliases = aliases
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
self.help: str = self.help or self.callback.__doc__ or ''
|
2019-04-10 12:06:52 -04:00
|
|
|
|
|
|
|
if dependencies is None:
|
|
|
|
dependencies = []
|
|
|
|
|
|
|
|
if order_dependencies is None:
|
|
|
|
order_dependencies = []
|
|
|
|
|
|
|
|
# Show first line of help if short help is missing
|
2022-06-03 08:46:56 -04:00
|
|
|
self.short_help: str = self.short_help or self.help.split('\n')[0]
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-07-02 14:33:32 -04:00
|
|
|
if deprecated:
|
2019-11-14 07:48:24 -05:00
|
|
|
deprecation = Deprecation(deprecated)
|
2019-07-02 14:33:32 -04:00
|
|
|
self.short_help = deprecation.short_help(self.short_help)
|
2021-01-25 21:49:01 -05:00
|
|
|
self.help = deprecation.help(self.help, type='Command', separator='\n')
|
2019-07-02 14:33:32 -04:00
|
|
|
|
2019-04-10 12:06:52 -04:00
|
|
|
# Add aliases to help string
|
|
|
|
if aliases:
|
2021-01-25 21:49:01 -05:00
|
|
|
aliases_help = 'Aliases: %s.' % ', '.join(aliases)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
self.help = '\n'.join([self.help, aliases_help])
|
|
|
|
self.short_help = ' '.join([aliases_help, self.short_help])
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-11-08 10:46:02 -05:00
|
|
|
self.unwrapped_callback = self.callback
|
2019-04-10 12:06:52 -04:00
|
|
|
if self.callback is not None:
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def wrapped_callback(**action_args: Any) -> Task:
|
2019-04-10 12:06:52 -04:00
|
|
|
return Task(
|
2019-11-08 10:46:02 -05:00
|
|
|
callback=self.unwrapped_callback,
|
2019-04-10 12:06:52 -04:00
|
|
|
name=self.name,
|
|
|
|
dependencies=dependencies,
|
|
|
|
order_dependencies=order_dependencies,
|
|
|
|
action_args=action_args,
|
|
|
|
aliases=self.aliases,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.callback = wrapped_callback
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def invoke(self, ctx: click.core.Context) -> click.core.Context:
|
2019-07-02 14:33:32 -04:00
|
|
|
if self.deprecated:
|
2019-11-14 07:48:24 -05:00
|
|
|
deprecation = Deprecation(self.deprecated)
|
|
|
|
message = deprecation.full_message('Command "%s"' % self.name)
|
|
|
|
|
|
|
|
if deprecation.exit_with_error:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise FatalError('Error: %s' % message)
|
2019-11-14 07:48:24 -05:00
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
print_warning('Warning: %s' % message)
|
2019-11-14 07:48:24 -05:00
|
|
|
|
2019-07-02 14:33:32 -04:00
|
|
|
self.deprecated = False # disable Click's built-in deprecation handling
|
|
|
|
|
|
|
|
# Print warnings for options
|
2019-11-14 07:48:24 -05:00
|
|
|
check_deprecation(ctx)
|
2019-07-02 14:33:32 -04:00
|
|
|
return super(Action, self).invoke(ctx)
|
|
|
|
|
2019-06-10 10:52:04 -04:00
|
|
|
class Argument(click.Argument):
|
2019-07-02 14:33:32 -04:00
|
|
|
"""
|
|
|
|
Positional argument
|
|
|
|
|
|
|
|
names - alias of 'param_decls'
|
|
|
|
"""
|
2022-06-03 08:46:56 -04:00
|
|
|
def __init__(self, **kwargs: str):
|
2021-01-25 21:49:01 -05:00
|
|
|
names = kwargs.pop('names')
|
2019-06-10 10:52:04 -04:00
|
|
|
super(Argument, self).__init__(names, **kwargs)
|
|
|
|
|
2019-06-12 13:10:16 -04:00
|
|
|
class Scope(object):
|
|
|
|
"""
|
|
|
|
Scope for sub-command option.
|
|
|
|
possible values:
|
|
|
|
- default - only available on defined level (global/action)
|
|
|
|
- global - When defined for action, also available as global
|
|
|
|
- shared - Opposite to 'global': when defined in global scope, also available for all actions
|
|
|
|
"""
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
SCOPES = ('default', 'global', 'shared')
|
2019-06-12 13:10:16 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def __init__(self, scope: Union['Scope', str]=None) -> None:
|
2019-06-12 13:10:16 -04:00
|
|
|
if scope is None:
|
2021-01-25 21:49:01 -05:00
|
|
|
self._scope = 'default'
|
2019-06-12 13:10:16 -04:00
|
|
|
elif isinstance(scope, str) and scope in self.SCOPES:
|
|
|
|
self._scope = scope
|
|
|
|
elif isinstance(scope, Scope):
|
|
|
|
self._scope = str(scope)
|
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise FatalError('Unknown scope for option: %s' % scope)
|
2019-06-12 13:10:16 -04:00
|
|
|
|
|
|
|
@property
|
2022-06-03 08:46:56 -04:00
|
|
|
def is_global(self) -> bool:
|
2021-01-25 21:49:01 -05:00
|
|
|
return self._scope == 'global'
|
2019-06-12 13:10:16 -04:00
|
|
|
|
|
|
|
@property
|
2022-06-03 08:46:56 -04:00
|
|
|
def is_shared(self) -> bool:
|
2021-01-25 21:49:01 -05:00
|
|
|
return self._scope == 'shared'
|
2019-06-12 13:10:16 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def __str__(self) -> str:
|
2019-06-12 13:10:16 -04:00
|
|
|
return self._scope
|
|
|
|
|
|
|
|
class Option(click.Option):
|
|
|
|
"""Option that knows whether it should be global"""
|
2022-06-03 08:46:56 -04:00
|
|
|
def __init__(self, scope: Union[Scope, str]=None, deprecated: Union[Dict, str, bool]=False, hidden: bool=False, **kwargs: str) -> None:
|
2019-07-02 14:33:32 -04:00
|
|
|
"""
|
|
|
|
Keyword arguments additional to Click's Option class:
|
|
|
|
|
|
|
|
names - alias of 'param_decls'
|
|
|
|
deprecated - marks option as deprecated. May be boolean, string (with custom deprecation message)
|
|
|
|
or dict with optional keys:
|
|
|
|
since: version of deprecation
|
|
|
|
removed: version when option will be removed
|
|
|
|
custom_message: Additional text to deprecation warning
|
|
|
|
"""
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
kwargs['param_decls'] = kwargs.pop('names')
|
2019-06-12 13:10:16 -04:00
|
|
|
super(Option, self).__init__(**kwargs)
|
|
|
|
|
2019-07-02 14:33:32 -04:00
|
|
|
self.deprecated = deprecated
|
2019-06-12 13:10:16 -04:00
|
|
|
self.scope = Scope(scope)
|
2019-08-21 08:24:14 -04:00
|
|
|
self.hidden = hidden
|
2019-06-12 13:10:16 -04:00
|
|
|
|
2019-07-02 14:33:32 -04:00
|
|
|
if deprecated:
|
2019-11-14 07:48:24 -05:00
|
|
|
deprecation = Deprecation(deprecated)
|
2022-06-03 08:46:56 -04:00
|
|
|
self.help: str = deprecation.help(self.help)
|
2019-07-02 14:33:32 -04:00
|
|
|
|
2020-06-26 11:18:25 -04:00
|
|
|
if self.envvar:
|
2021-01-25 21:49:01 -05:00
|
|
|
self.help += ' The default value can be set with the %s environment variable.' % self.envvar
|
2020-06-26 11:18:25 -04:00
|
|
|
|
2019-06-12 13:10:16 -04:00
|
|
|
if self.scope.is_global:
|
2021-01-25 21:49:01 -05:00
|
|
|
self.help += ' This option can be used at most once either globally, or for one subcommand.'
|
2019-06-12 13:10:16 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def get_help_record(self, ctx: click.core.Context) -> Any:
|
2019-08-21 08:24:14 -04:00
|
|
|
# Backport "hidden" parameter to click 5.0
|
|
|
|
if self.hidden:
|
2022-06-03 08:46:56 -04:00
|
|
|
return None
|
2019-08-21 08:24:14 -04:00
|
|
|
|
|
|
|
return super(Option, self).get_help_record(ctx)
|
|
|
|
|
2019-04-10 12:06:52 -04:00
|
|
|
class CLI(click.MultiCommand):
|
|
|
|
"""Action list contains all actions with options available for CLI"""
|
2022-06-03 08:46:56 -04:00
|
|
|
def __init__(self, all_actions: Dict=None, verbose_output: List=None, help: str=None) -> None:
|
2019-04-10 12:06:52 -04:00
|
|
|
super(CLI, self).__init__(
|
|
|
|
chain=True,
|
|
|
|
invoke_without_command=True,
|
|
|
|
result_callback=self.execute_tasks,
|
2022-06-30 06:10:41 -04:00
|
|
|
no_args_is_help=True,
|
2021-01-25 21:49:01 -05:00
|
|
|
context_settings={'max_content_width': 140},
|
2019-04-10 12:06:52 -04:00
|
|
|
help=help,
|
|
|
|
)
|
|
|
|
self._actions = {}
|
|
|
|
self.global_action_callbacks = []
|
|
|
|
self.commands_with_aliases = {}
|
|
|
|
|
2019-08-15 09:42:15 -04:00
|
|
|
if verbose_output is None:
|
|
|
|
verbose_output = []
|
|
|
|
|
|
|
|
self.verbose_output = verbose_output
|
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
if all_actions is None:
|
|
|
|
all_actions = {}
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-06-12 13:10:16 -04:00
|
|
|
shared_options = []
|
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
# Global options
|
2021-01-25 21:49:01 -05:00
|
|
|
for option_args in all_actions.get('global_options', []):
|
2019-10-03 12:26:44 -04:00
|
|
|
option = Option(**option_args)
|
|
|
|
self.params.append(option)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
if option.scope.is_shared:
|
|
|
|
shared_options.append(option)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
# Global options validators
|
2021-01-25 21:49:01 -05:00
|
|
|
self.global_action_callbacks = all_actions.get('global_action_callbacks', [])
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
# Actions
|
2021-01-25 21:49:01 -05:00
|
|
|
for name, action in all_actions.get('actions', {}).items():
|
|
|
|
arguments = action.pop('arguments', [])
|
|
|
|
options = action.pop('options', [])
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
if arguments is None:
|
|
|
|
arguments = []
|
2019-06-10 10:52:04 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
if options is None:
|
|
|
|
options = []
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
self._actions[name] = Action(name=name, **action)
|
2021-01-25 21:49:01 -05:00
|
|
|
for alias in [name] + action.get('aliases', []):
|
2019-10-03 12:26:44 -04:00
|
|
|
self.commands_with_aliases[alias] = name
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
for argument_args in arguments:
|
|
|
|
self._actions[name].params.append(Argument(**argument_args))
|
2019-06-10 10:52:04 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
# Add all shared options
|
|
|
|
for option in shared_options:
|
|
|
|
self._actions[name].params.append(option)
|
2019-06-12 13:10:16 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
for option_args in options:
|
|
|
|
option = Option(**option_args)
|
2019-06-12 13:10:16 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
if option.scope.is_shared:
|
2019-11-08 10:46:02 -05:00
|
|
|
raise FatalError(
|
|
|
|
'"%s" is defined for action "%s". '
|
|
|
|
' "shared" options can be declared only on global level' % (option.name, name))
|
2019-06-12 13:10:16 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
# Promote options to global if see for the first time
|
|
|
|
if option.scope.is_global and option.name not in [o.name for o in self.params]:
|
|
|
|
self.params.append(option)
|
2019-06-12 13:10:16 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
self._actions[name].params.append(option)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def list_commands(self, ctx: click.core.Context) -> List:
|
2019-11-08 11:54:10 -05:00
|
|
|
return sorted(filter(lambda name: not self._actions[name].hidden, self._actions))
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def get_command(self, ctx: click.core.Context, name: str) -> Optional[Action]:
|
2019-11-08 10:46:02 -05:00
|
|
|
if name in self.commands_with_aliases:
|
|
|
|
return self._actions.get(self.commands_with_aliases.get(name))
|
|
|
|
|
|
|
|
# Trying fallback to build target (from "all" action) if command is not known
|
|
|
|
else:
|
2022-06-03 08:46:56 -04:00
|
|
|
callback = self._actions.get('fallback')
|
|
|
|
if callback:
|
|
|
|
return Action(name=name, callback=callback.unwrapped_callback)
|
|
|
|
return None
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def _print_closing_message(self, args: PropertyDict, actions: _OrderedDictKeysView) -> None:
|
2019-04-10 12:06:52 -04:00
|
|
|
# print a closing message of some kind
|
|
|
|
#
|
2021-01-25 21:49:01 -05:00
|
|
|
if any(t in str(actions) for t in ('flash', 'dfu', 'uf2', 'uf2-app')):
|
|
|
|
print('Done')
|
2019-07-22 10:04:03 -04:00
|
|
|
return
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if not os.path.exists(os.path.join(args.build_dir, 'flasher_args.json')):
|
|
|
|
print('Done')
|
2019-04-10 12:06:52 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# Otherwise, if we built any binaries print a message about
|
|
|
|
# how to flash them
|
2022-06-03 08:46:56 -04:00
|
|
|
def print_flashing_message(title: str, key: str) -> None:
|
|
|
|
with open(os.path.join(args.build_dir, 'flasher_args.json')) as file:
|
|
|
|
flasher_args: Dict[str, Any] = json.load(file)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-07-26 22:34:21 -04:00
|
|
|
def flasher_path(f: Union[str, 'os.PathLike[str]']) -> str:
|
2022-06-03 08:46:56 -04:00
|
|
|
if type(args.build_dir) is bytes:
|
|
|
|
args.build_dir = args.build_dir.decode()
|
2019-04-10 12:06:52 -04:00
|
|
|
return _safe_relpath(os.path.join(args.build_dir, f))
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if key != 'project': # flashing a single item
|
2020-07-23 01:37:27 -04:00
|
|
|
if key not in flasher_args:
|
|
|
|
# This is the case for 'idf.py bootloader' if Secure Boot is on, need to follow manual flashing steps
|
2021-01-25 21:49:01 -05:00
|
|
|
print('\n%s build complete.' % title)
|
2020-07-23 01:37:27 -04:00
|
|
|
return
|
2021-01-25 21:49:01 -05:00
|
|
|
cmd = ''
|
|
|
|
if (key == 'bootloader'): # bootloader needs --flash-mode, etc to be passed in
|
|
|
|
cmd = ' '.join(flasher_args['write_flash_args']) + ' '
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
cmd += flasher_args[key]['offset'] + ' '
|
|
|
|
cmd += flasher_path(flasher_args[key]['file'])
|
2019-04-10 12:06:52 -04:00
|
|
|
else: # flashing the whole project
|
2021-01-25 21:49:01 -05:00
|
|
|
cmd = ' '.join(flasher_args['write_flash_args']) + ' '
|
2019-04-10 12:06:52 -04:00
|
|
|
flash_items = sorted(
|
2021-01-25 21:49:01 -05:00
|
|
|
((o, f) for (o, f) in flasher_args['flash_files'].items() if len(o) > 0),
|
2019-04-10 12:06:52 -04:00
|
|
|
key=lambda x: int(x[0], 0),
|
|
|
|
)
|
|
|
|
for o, f in flash_items:
|
2021-01-25 21:49:01 -05:00
|
|
|
cmd += o + ' ' + flasher_path(f) + ' '
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
print('\n%s build complete. To flash, run this command:' % title)
|
2020-07-23 01:37:27 -04:00
|
|
|
|
2019-11-08 10:46:02 -05:00
|
|
|
print(
|
2021-01-25 21:49:01 -05:00
|
|
|
'%s %s -p %s -b %s --before %s --after %s --chip %s %s write_flash %s' % (
|
2019-11-08 10:46:02 -05:00
|
|
|
PYTHON,
|
2021-01-25 21:49:01 -05:00
|
|
|
_safe_relpath('%s/components/esptool_py/esptool/esptool.py' % os.environ['IDF_PATH']),
|
|
|
|
args.port or '(PORT)',
|
2019-11-08 10:46:02 -05:00
|
|
|
args.baud,
|
2021-01-25 21:49:01 -05:00
|
|
|
flasher_args['extra_esptool_args']['before'],
|
|
|
|
flasher_args['extra_esptool_args']['after'],
|
|
|
|
flasher_args['extra_esptool_args']['chip'],
|
|
|
|
'--no-stub' if not flasher_args['extra_esptool_args']['stub'] else '',
|
2019-11-08 10:46:02 -05:00
|
|
|
cmd.strip(),
|
|
|
|
))
|
|
|
|
print(
|
|
|
|
"or run 'idf.py -p %s %s'" % (
|
2021-01-25 21:49:01 -05:00
|
|
|
args.port or '(PORT)',
|
|
|
|
key + '-flash' if key != 'project' else 'flash',
|
2019-11-08 10:46:02 -05:00
|
|
|
))
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if 'all' in actions or 'build' in actions:
|
|
|
|
print_flashing_message('Project', 'project')
|
2019-04-10 12:06:52 -04:00
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
if 'app' in actions:
|
|
|
|
print_flashing_message('App', 'app')
|
2021-08-11 09:24:17 -04:00
|
|
|
if 'partition-table' in actions:
|
|
|
|
print_flashing_message('Partition Table', 'partition-table')
|
2021-01-25 21:49:01 -05:00
|
|
|
if 'bootloader' in actions:
|
|
|
|
print_flashing_message('Bootloader', 'bootloader')
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def execute_tasks(self, tasks: List, **kwargs: str) -> OrderedDict:
|
2019-04-10 12:06:52 -04:00
|
|
|
ctx = click.get_current_context()
|
2019-08-21 08:24:14 -04:00
|
|
|
global_args = PropertyDict(kwargs)
|
|
|
|
|
|
|
|
# Show warning if some tasks are present several times in the list
|
2019-10-03 12:26:44 -04:00
|
|
|
dupplicated_tasks = sorted(
|
|
|
|
[item for item, count in Counter(task.name for task in tasks).items() if count > 1])
|
2019-08-21 08:24:14 -04:00
|
|
|
if dupplicated_tasks:
|
2021-01-25 21:49:01 -05:00
|
|
|
dupes = ', '.join('"%s"' % t for t in dupplicated_tasks)
|
2020-11-07 06:45:54 -05:00
|
|
|
|
|
|
|
print_warning(
|
2021-01-25 21:49:01 -05:00
|
|
|
'WARNING: Command%s found in the list of commands more than once. ' %
|
|
|
|
('s %s are' % dupes if len(dupplicated_tasks) > 1 else ' %s is' % dupes) +
|
|
|
|
'Only first occurrence will be executed.')
|
2019-08-21 08:24:14 -04:00
|
|
|
|
2019-06-12 13:10:16 -04:00
|
|
|
for task in tasks:
|
2020-07-14 11:07:32 -04:00
|
|
|
# Set propagated global options.
|
|
|
|
# These options may be set on one subcommand, but available in the list of global arguments
|
2019-06-12 13:10:16 -04:00
|
|
|
for key in list(task.action_args):
|
|
|
|
option = next((o for o in ctx.command.params if o.name == key), None)
|
2019-07-02 14:33:32 -04:00
|
|
|
|
2019-06-12 13:10:16 -04:00
|
|
|
if option and (option.scope.is_global or option.scope.is_shared):
|
|
|
|
local_value = task.action_args.pop(key)
|
|
|
|
global_value = global_args[key]
|
|
|
|
default = () if option.multiple else option.default
|
|
|
|
|
|
|
|
if global_value != default and local_value != default and global_value != local_value:
|
2023-01-03 12:03:51 -05:00
|
|
|
if hasattr(option, 'envvar') and option.envvar and os.getenv(option.envvar) != default:
|
|
|
|
msg = (f'This option cannot be set in command line if the {option.envvar} '
|
|
|
|
'environment variable is set to a different value.')
|
|
|
|
else:
|
|
|
|
msg = 'This option can appear at most once in the command line.'
|
|
|
|
|
|
|
|
raise FatalError(f'Option "{key}" provided for "{task.name}" is already defined to '
|
|
|
|
f'a different value. {msg}')
|
2019-06-12 13:10:16 -04:00
|
|
|
if local_value != default:
|
|
|
|
global_args[key] = local_value
|
|
|
|
|
2019-07-02 14:33:32 -04:00
|
|
|
# Show warnings about global arguments
|
2019-11-14 07:48:24 -05:00
|
|
|
check_deprecation(ctx)
|
2019-07-02 14:33:32 -04:00
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
# Make sure that define_cache_entry is mutable list and can be modified in callbacks
|
|
|
|
global_args.define_cache_entry = list(global_args.define_cache_entry)
|
|
|
|
|
|
|
|
# Execute all global action callback - first from idf.py itself, then from extensions
|
2019-04-10 12:06:52 -04:00
|
|
|
for action_callback in ctx.command.global_action_callbacks:
|
|
|
|
action_callback(ctx, global_args, tasks)
|
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
# Build full list of tasks to and deal with dependencies and order dependencies
|
2022-06-03 08:46:56 -04:00
|
|
|
tasks_to_run: OrderedDict = OrderedDict()
|
2019-08-14 09:19:06 -04:00
|
|
|
while tasks:
|
|
|
|
task = tasks[0]
|
|
|
|
tasks_dict = dict([(t.name, t) for t in tasks])
|
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
dependecies_processed = True
|
2019-08-14 09:19:06 -04:00
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
# If task have some dependecies they have to be executed before the task.
|
|
|
|
for dep in task.dependencies:
|
|
|
|
if dep not in tasks_to_run.keys():
|
|
|
|
# If dependent task is in the list of unprocessed tasks move to the front of the list
|
|
|
|
if dep in tasks_dict.keys():
|
|
|
|
dep_task = tasks.pop(tasks.index(tasks_dict[dep]))
|
|
|
|
# Otherwise invoke it with default set of options
|
|
|
|
# and put to the front of the list of unprocessed tasks
|
|
|
|
else:
|
2019-11-08 10:46:02 -05:00
|
|
|
print(
|
|
|
|
'Adding "%s"\'s dependency "%s" to list of commands with default set of options.' %
|
|
|
|
(task.name, dep))
|
2019-08-21 08:24:14 -04:00
|
|
|
dep_task = ctx.invoke(ctx.command.get_command(ctx, dep))
|
|
|
|
|
2020-11-07 06:45:54 -05:00
|
|
|
# Remove options with global scope from invoke tasks because they are already in global_args
|
2019-08-21 08:24:14 -04:00
|
|
|
for key in list(dep_task.action_args):
|
|
|
|
option = next((o for o in ctx.command.params if o.name == key), None)
|
|
|
|
if option and (option.scope.is_global or option.scope.is_shared):
|
|
|
|
dep_task.action_args.pop(key)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
tasks.insert(0, dep_task)
|
|
|
|
dependecies_processed = False
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
# Order only dependencies are moved to the front of the queue if they present in command list
|
2019-04-10 12:06:52 -04:00
|
|
|
for dep in task.order_dependencies:
|
2019-08-21 08:24:14 -04:00
|
|
|
if dep in tasks_dict.keys() and dep not in tasks_to_run.keys():
|
2019-04-10 12:06:52 -04:00
|
|
|
tasks.insert(0, tasks.pop(tasks.index(tasks_dict[dep])))
|
2019-08-21 08:24:14 -04:00
|
|
|
dependecies_processed = False
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
if dependecies_processed:
|
|
|
|
# Remove task from list of unprocessed tasks
|
2019-04-10 12:06:52 -04:00
|
|
|
tasks.pop(0)
|
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
# And add to the queue
|
|
|
|
if task.name not in tasks_to_run.keys():
|
|
|
|
tasks_to_run.update([(task.name, task)])
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
# Run all tasks in the queue
|
2019-10-03 12:26:44 -04:00
|
|
|
# when global_args.dry_run is true idf.py works in idle mode and skips actual task execution
|
|
|
|
if not global_args.dry_run:
|
2019-08-21 08:24:14 -04:00
|
|
|
for task in tasks_to_run.values():
|
|
|
|
name_with_aliases = task.name
|
|
|
|
if task.aliases:
|
2021-01-25 21:49:01 -05:00
|
|
|
name_with_aliases += ' (aliases: %s)' % ', '.join(task.aliases)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-06-30 06:10:41 -04:00
|
|
|
# When machine-readable json format for help is printed, don't show info about executing action so the output is deserializable
|
|
|
|
if name_with_aliases != 'help' or not task.action_args.get('json_option', False):
|
|
|
|
print('Executing action: %s' % name_with_aliases)
|
2020-05-12 12:50:00 -04:00
|
|
|
task(ctx, global_args, task.action_args)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
self._print_closing_message(global_args, tasks_to_run.keys())
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2019-08-21 08:24:14 -04:00
|
|
|
return tasks_to_run
|
2019-04-10 12:06:52 -04:00
|
|
|
|
|
|
|
# That's a tiny parser that parse project-dir even before constructing
|
|
|
|
# fully featured click parser to be sure that extensions are loaded from the right place
|
|
|
|
@click.command(
|
|
|
|
add_help_option=False,
|
2019-10-03 12:26:44 -04:00
|
|
|
context_settings={
|
2021-01-25 21:49:01 -05:00
|
|
|
'allow_extra_args': True,
|
|
|
|
'ignore_unknown_options': True
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
2019-04-10 12:06:52 -04:00
|
|
|
)
|
2021-01-25 21:49:01 -05:00
|
|
|
@click.option('-C', '--project-dir', default=os.getcwd(), type=click.Path())
|
2022-06-03 08:46:56 -04:00
|
|
|
def parse_project_dir(project_dir: str) -> Any:
|
2023-01-17 14:51:46 -05:00
|
|
|
return os.path.realpath(project_dir)
|
2022-04-08 11:58:05 -04:00
|
|
|
|
2020-11-02 08:11:14 -05:00
|
|
|
# Set `complete_var` to not existing environment variable name to prevent early cmd completion
|
2021-01-25 21:49:01 -05:00
|
|
|
project_dir = parse_project_dir(standalone_mode=False, complete_var='_IDF.PY_COMPLETE_NOT_EXISTING')
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
all_actions: Dict = {}
|
2019-10-03 12:26:44 -04:00
|
|
|
# Load extensions from components dir
|
2021-01-25 21:49:01 -05:00
|
|
|
idf_py_extensions_path = os.path.join(os.environ['IDF_PATH'], 'tools', 'idf_py_actions')
|
2023-01-17 14:51:46 -05:00
|
|
|
extension_dirs = [os.path.realpath(idf_py_extensions_path)]
|
2021-01-25 21:49:01 -05:00
|
|
|
extra_paths = os.environ.get('IDF_EXTRA_ACTIONS_PATH')
|
2020-05-13 18:13:32 -04:00
|
|
|
if extra_paths is not None:
|
|
|
|
for path in extra_paths.split(';'):
|
2023-01-17 14:51:46 -05:00
|
|
|
path = os.path.realpath(path)
|
2020-05-13 18:13:32 -04:00
|
|
|
if path not in extension_dirs:
|
|
|
|
extension_dirs.append(path)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2021-03-09 14:41:03 -05:00
|
|
|
extensions = []
|
2019-10-03 12:26:44 -04:00
|
|
|
for directory in extension_dirs:
|
|
|
|
if directory and not os.path.exists(directory):
|
2020-11-07 06:45:54 -05:00
|
|
|
print_warning('WARNING: Directory with idf.py extensions doesn\'t exist:\n %s' % directory)
|
2019-10-03 12:26:44 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
sys.path.append(directory)
|
|
|
|
for _finder, name, _ispkg in sorted(iter_modules([directory])):
|
2019-10-24 07:20:25 -04:00
|
|
|
if name.endswith('_ext'):
|
2021-03-09 14:41:03 -05:00
|
|
|
extensions.append((name, import_module(name)))
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-04-08 11:58:05 -04:00
|
|
|
# Load component manager idf.py extensions if not explicitly disabled
|
|
|
|
if os.getenv('IDF_COMPONENT_MANAGER') != '0':
|
|
|
|
extensions.append(('component_manager_ext', idf_extensions))
|
2019-11-18 11:34:33 -05:00
|
|
|
|
2021-11-09 02:30:09 -05:00
|
|
|
# Optional load `pyclang` for additional clang-tidy related functionalities
|
|
|
|
try:
|
|
|
|
from pyclang import idf_extension
|
|
|
|
|
|
|
|
extensions.append(('idf_clang_tidy_ext', idf_extension))
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2021-03-09 14:41:03 -05:00
|
|
|
for name, extension in extensions:
|
2019-10-03 12:26:44 -04:00
|
|
|
try:
|
|
|
|
all_actions = merge_action_lists(all_actions, extension.action_extensions(all_actions, project_dir))
|
|
|
|
except AttributeError:
|
2020-11-07 06:45:54 -05:00
|
|
|
print_warning('WARNING: Cannot load idf.py extension "%s"' % name)
|
2019-10-03 12:26:44 -04:00
|
|
|
|
|
|
|
# Load extensions from project dir
|
2021-01-25 21:49:01 -05:00
|
|
|
if os.path.exists(os.path.join(project_dir, 'idf_ext.py')):
|
2019-04-10 12:06:52 -04:00
|
|
|
sys.path.append(project_dir)
|
|
|
|
try:
|
|
|
|
from idf_ext import action_extensions
|
|
|
|
except ImportError:
|
2021-01-25 21:49:01 -05:00
|
|
|
print_warning('Error importing extension file idf_ext.py. Skipping.')
|
2022-04-08 11:58:05 -04:00
|
|
|
print_warning(
|
|
|
|
"Please make sure that it contains implementation (even if it's empty) of add_action_extensions")
|
2018-05-11 05:20:27 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
try:
|
|
|
|
all_actions = merge_action_lists(all_actions, action_extensions(all_actions, project_dir))
|
|
|
|
except NameError:
|
|
|
|
pass
|
2018-08-26 22:48:16 -04:00
|
|
|
|
2019-11-08 10:46:02 -05:00
|
|
|
cli_help = (
|
2021-01-25 21:49:01 -05:00
|
|
|
'ESP-IDF CLI build management tool. '
|
2022-06-30 06:10:41 -04:00
|
|
|
'For commands that are not known to idf.py an attempt to execute it as a build system target will be made. '
|
|
|
|
'Selected target: {}'.format(get_target(project_dir)))
|
2019-11-08 10:46:02 -05:00
|
|
|
|
|
|
|
return CLI(help=cli_help, verbose_output=verbose_output, all_actions=all_actions)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2018-01-23 01:08:28 -05:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def signal_handler(_signal: int, _frame: Optional[FrameType]) -> None:
|
2021-04-15 03:54:15 -04:00
|
|
|
# The Ctrl+C processed by other threads inside
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def main() -> None:
|
2021-04-15 03:54:15 -04:00
|
|
|
# Processing of Ctrl+C event for all threads made by main()
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
2022-02-10 10:58:16 -05:00
|
|
|
# Check the environment only when idf.py is invoked regularly from command line.
|
|
|
|
checks_output = None if SHELL_COMPLETE_RUN else check_environment()
|
|
|
|
|
2023-06-05 11:54:05 -04:00
|
|
|
# Check existance of the current working directory to prevent exceptions from click cli.
|
|
|
|
try:
|
|
|
|
os.getcwd()
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
raise FatalError(f'ERROR: {e}. Working directory cannot be established. Check its existence.')
|
|
|
|
|
2022-02-10 10:58:16 -05:00
|
|
|
try:
|
|
|
|
cli = init_cli(verbose_output=checks_output)
|
|
|
|
except ImportError:
|
|
|
|
if SHELL_COMPLETE_RUN:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
cli(sys.argv[1:], prog_name=PROG, complete_var=SHELL_COMPLETE_VAR)
|
2018-01-23 01:08:28 -05:00
|
|
|
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def _valid_unicode_config() -> Union[codecs.CodecInfo, bool]:
|
2019-04-10 12:06:52 -04:00
|
|
|
# Python 2 is always good
|
|
|
|
if sys.version_info[0] == 2:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# With python 3 unicode environment is required
|
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
return codecs.lookup(locale.getpreferredencoding()).name != 'ascii'
|
2019-04-10 12:06:52 -04:00
|
|
|
except Exception:
|
|
|
|
return False
|
2018-01-23 01:08:28 -05:00
|
|
|
|
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
def _find_usable_locale() -> str:
|
2019-04-10 12:06:52 -04:00
|
|
|
try:
|
2022-06-03 08:46:56 -04:00
|
|
|
locales = subprocess.Popen(['locale', '-a'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('ascii', 'replace')
|
2019-04-10 12:06:52 -04:00
|
|
|
except OSError:
|
2021-01-25 21:49:01 -05:00
|
|
|
locales = ''
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2022-06-03 08:46:56 -04:00
|
|
|
usable_locales: List[str] = []
|
2019-04-10 12:06:52 -04:00
|
|
|
for line in locales.splitlines():
|
|
|
|
locale = line.strip()
|
2021-01-25 21:49:01 -05:00
|
|
|
locale_name = locale.lower().replace('-', '')
|
2019-04-10 12:06:52 -04:00
|
|
|
|
|
|
|
# C.UTF-8 is the best option, if supported
|
2021-01-25 21:49:01 -05:00
|
|
|
if locale_name == 'c.utf8':
|
2019-04-10 12:06:52 -04:00
|
|
|
return locale
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if locale_name.endswith('.utf8'):
|
2019-04-10 12:06:52 -04:00
|
|
|
# Make a preference of english locales
|
2021-01-25 21:49:01 -05:00
|
|
|
if locale.startswith('en_'):
|
2019-04-10 12:06:52 -04:00
|
|
|
usable_locales.insert(0, locale)
|
|
|
|
else:
|
|
|
|
usable_locales.append(locale)
|
|
|
|
|
|
|
|
if not usable_locales:
|
2019-06-12 13:10:16 -04:00
|
|
|
raise FatalError(
|
2021-01-25 21:49:01 -05:00
|
|
|
'Support for Unicode filenames is required, but no suitable UTF-8 locale was found on your system.'
|
|
|
|
' Please refer to the manual for your operating system for details on locale reconfiguration.')
|
2019-04-10 12:06:52 -04:00
|
|
|
|
|
|
|
return usable_locales[0]
|
2018-01-23 01:08:28 -05:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if __name__ == '__main__':
|
2018-02-15 23:32:08 -05:00
|
|
|
try:
|
2021-11-09 07:10:56 -05:00
|
|
|
if 'MSYSTEM' in os.environ:
|
2022-04-08 11:58:05 -04:00
|
|
|
print_warning(
|
|
|
|
'MSys/Mingw is no longer supported. Please follow the getting started guide of the '
|
|
|
|
'documentation in order to set up a suitiable environment, or continue at your own risk.')
|
2021-01-25 21:49:01 -05:00
|
|
|
elif os.name == 'posix' and not _valid_unicode_config():
|
2019-04-10 12:06:52 -04:00
|
|
|
# Trying to find best utf-8 locale available on the system and restart python with it
|
|
|
|
best_locale = _find_usable_locale()
|
|
|
|
|
2020-11-07 06:45:54 -05:00
|
|
|
print_warning(
|
2021-01-25 21:49:01 -05:00
|
|
|
'Your environment is not configured to handle unicode filenames outside of ASCII range.'
|
|
|
|
' Environment variable LC_ALL is temporary set to %s for unicode support.' % best_locale)
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
os.environ['LC_ALL'] = best_locale
|
2019-04-10 12:06:52 -04:00
|
|
|
ret = subprocess.call([sys.executable] + sys.argv, env=os.environ)
|
|
|
|
if ret:
|
|
|
|
raise SystemExit(ret)
|
|
|
|
|
2019-02-22 10:07:26 -05:00
|
|
|
else:
|
|
|
|
main()
|
2019-04-10 12:06:52 -04:00
|
|
|
|
2018-02-15 23:32:08 -05:00
|
|
|
except FatalError as e:
|
2020-11-07 06:45:54 -05:00
|
|
|
print(e, file=sys.stderr)
|
2018-02-15 23:32:08 -05:00
|
|
|
sys.exit(2)
|