32 Commits

Author SHA1 Message Date
Peter Dragun
806c93e08e change: fix issues reported by ruff 2025-03-27 12:30:23 +01:00
Frantisek Hrbata
3bad4348d0 fix(tools): handle packages with dots in their names during dependency checks
The `setuptools` package starting with `v70.1.0`[1] contains built-in
`bdist_wheel` command. Before this version `setuptools` relied on the
`bdist_wheel` command implementation from the `wheel` package. Starting with
`setuptools` `v75.8.1` the `PEP 491`[3] restrictions on the distribution name
of a wheel package are enforced[4], replacting also `.` with `_`.  Note that
`PEP 491` actually allows `.` in the distribution name, but for some reason the
latest packaging docs[10][11] does not, stating that `.` should be replaced
with `_`. This was discussion here[12].

Also the `wheel` package starting with `v0.45.0`[5] is using the `bdist_wheel`
command from `setuptools`.  This means that any package which has `.` in its
distribution name, like `ruamel.yaml.clib`, can have different wheel name,
depending on which version of the `bdist_wheel` command was used.

The `bdist_wheel` command from setuptools prior `v75.8.1` or `wheel` prior
`v0.45.0` will keep the dots in distribution name preserved.  For exaple the
`ruamel.yaml.clib` package will have distribution name
`ruamel.yaml.clib-0.2.12.dist-info. Newer versions will replace the dots with
`_` according to [10][11], creating distribution like
`ruamel_yaml_clib-0.2.12.dist-info`.

From packaging point of view `ruamel.yaml.clib-0.2.12.dist-info` and
`ruamel_yaml_clib-0.2.12.dist-info` are the same packages, but this is not
reflected in `importlib.metadata` prior python 3.10[9], which does not perform
name normalization prior the distribution search. This causes the `version`
from `importlib.metadata` to fail on python prior the 3.10 version if the
package with dots in distribution name was generated with normalized paths with
newer `setuptools`. Note that the distribution name normalization was
backported to some later 3.9 python version.

Let's demonstrate this behavior on a simple package with the
`my.minimal.package` name.

```
my_minimal_package/
├── pkg
│   └── __init__.py
└── setup.py

from setuptools import setup, find_packages

setup(
    name='my.minimal.package',
    version='0.1.0',
    packages=find_packages(),
    install_requires=[],
    entry_points={},
)
```

With python 3.9.0 search for `my.minimal.package` fails because
of the missing name normalization.
```
docker run --rm -it --platform linux/x86_64 python:3.9.0 bash
python -m venv venv
. venv/bin/activate
pip install setuptools==v75.8.1
python setup.py bdist_wheel
pip install dist/my_minimal_package-0.1.0-py3-none-any.whl
python
Python 3.9.0 (default, Nov 18 2020, 13:28:38)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from importlib.metadata import version as get_version
>>> get_version('my.minimal.package')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/importlib/metadata.py", line 551, in version
    return distribution(distribution_name).version
  File "/usr/local/lib/python3.9/importlib/metadata.py", line 524, in distribution
    return Distribution.from_name(distribution_name)
  File "/usr/local/lib/python3.9/importlib/metadata.py", line 187, in from_name
    raise PackageNotFoundError(name)
importlib.metadata.PackageNotFoundError: my.minimal.package
>>> get_version('my_minimal_package')
'0.1.0'
```

With python 3.10.0 search for both `my.minimal.package` and
`my_minimal_package` succeeds.
```
docker run --rm -it --platform linux/x86_64 python:3.10.0 bash
python
Python 3.10.0 (default, Dec  3 2021, 00:21:30) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from importlib.metadata import version as get_version
>>> get_version('my.minimal.package')
'0.1.0'
>>> get_version('my_minimal_package')
'0.1.0'
```

In our `tools/check_python_dependencies.py` we cannot relay on the default
distribution finder, used in the `version` function from `importlib.metadata`,
to do name normalization on older python versions.  To cope with this,
implement a fallback version search. If `version` fails with
`PackageNotFoundError`, do the name normalization according to [10][11] and try
again.

Note: There is also a `wheel`[6][7] `v0.43.0` package embeded in `setuptools`
along with the new implementation[8].  This one seems to be used if the
external `wheel` package is not available but imported. TBH this is all kinda
messy and I may have overlooked something.

* [1] https://setuptools.pypa.io/en/stable/history.html#v70-1-0
* [2] https://setuptools.pypa.io/en/stable/history.html#v75-8-1
* [3] https://peps.python.org/pep-0491/#escaping-and-unicode
* [4] https://github.com/pypa/setuptools/pull/4766/files
* [5] https://wheel.readthedocs.io/en/stable/news.html
* [6] https://github.com/pypa/setuptools/blob/main/setuptools/_vendor/wheel/__init__.py
* [7] https://github.com/pypa/setuptools/issues/1386
* [8] https://github.com/pypa/setuptools/blob/main/setuptools/command/bdist_wheel.py
* [9] c6ca368867
* [10] https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization
* [11] https://packaging.python.org/en/latest/specifications/binary-distribution-format/
       #escaping-and-unicode
* [12] https://github.com/pypa/setuptools/issues/3777

Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
2025-03-19 12:19:57 +01:00
Marek Fiala
2c814ef2fa feat(tools): Enforce utf-8 encoding with open() function 2024-12-27 17:12:21 +08:00
Peter Dragun
5f798eb78b fix(tools): catch more general errors in python dependency checker 2024-02-15 10:47:02 +01:00
Peter Dragun
a58be23118 bug(tools): dependency check catch exception when package not installed 2023-08-28 16:03:15 +02:00
Peter Dragun
e0abb5b414 fix(tools): check_python_version accept dev releases as requirement 2023-08-04 09:52:05 +02:00
Peter Dragun
c7da726b0b fix(tools/python_dep_check): replace deprecated pkg_resources with importlib
Closes https://github.com/espressif/esp-idf/issues/11712
2023-07-11 16:09:03 +02:00
Roland Dobai
b738953e90 Tools: Improve the message for missing setuptools in the Python checker 2022-07-18 11:56:32 +02:00
Roland Dobai
08cc2fd493 Tools: Fix the Python dependency checker by skipping not installed dependency sub-trees 2022-05-03 16:50:25 +02:00
Roland Dobai
b28d7e6850 Tools: Improve the Python package system
Introduce features into the Python package management system & manage
package versions outside of ESP-IDF repo.
2022-01-17 16:54:36 +01:00
Roland Dobai
a1d0d1ffbe Tools: Remove MSYS/Mingw support
MSYS/Mingw was deprecated since v4.0 and it is removed in v5.0. Please
follow the getting started guide of the documentation to set up a
Windows Command Line or Power Shell based environment.
2021-11-10 17:25:07 +01:00
Tomas Sebestik
d22795ea56 Update Dockerfile working on both x64 / ARM
Closes https://github.com/espressif/esp-idf/issues/6730
2021-10-08 17:11:59 +08:00
Fu Hanxi
0146f258d7 style: format python files with isort and double-quote-string-fixer 2021-01-26 10:49:01 +08:00
Angus Gratton
eef0e178a0 Merge branch 'feature/msys2_env_update' into 'master'
windows: Update MSYS2 pre-compiled legacy build environment

Closes IDF-1289 and IDF-1526

See merge request espressif/esp-idf!9034
2020-06-12 07:26:09 +08:00
Ivan Grokhotkov
1727306645 tools: fix diagnostic output in check_python_dependencies.py
If IDF_PYTHON_ENV_PATH was not set, an exception occurred:

Traceback (most recent call last):
  File "/home/user/esp/esp-idf/tools/check_python_dependencies.py", line 108, in <module>
    if idf_python_env_path not in sys.executable:
TypeError: 'in <string>' requires string as left operand, not NoneType

and the final line in the diagnostic message was not printed.
Fix to print the PATH if IDF_PYTHON_ENV_PATH is not set.
2020-06-08 18:14:39 +02:00
Angus Gratton
091ce8a124 msys2: Remove python version consideration in pacman commands 2020-06-04 18:42:52 +10:00
Ivan Grokhotkov
46e9aef6c9 tools/check_python_dependencies: print diagnostic info on failure
Helps collect more data for cases such as:
https://github.com/espressif/esp-idf/issues/5133
2020-04-16 11:58:16 +02:00
Angus Gratton
c7209b110e check_python_dependencies: If overriding requirements.txt path, provide a pip command line
Advice about install.sh/install.bat, etc only works for the default requirements.txt
2020-02-07 16:37:45 +11:00
Angus Gratton
a148d8e6ba docs: Refactor extensions into packages, update the add-ons-reference docs page
Includes converting some of the remaining standalone scripts into Sphinx extensions.

Make flake8 clean
2020-02-07 16:37:43 +11:00
Roland Dobai
70b6f5397f Fix Python requirement for setuptools 2019-11-04 16:18:37 +01:00
Roland Dobai
01887f71e7 Update kconfiglib to upstream version and replace mconf-idf
Special thanks to @ulfalizer for the helpful suggestions regarding
kconfiglib.

"rsource" option is available for relative path includes
Closes https://github.com/espressif/esp-idf/issues/4064
2019-10-29 10:40:04 +01:00
Ivan Grokhotkov
8d1a9c07a0 tools/check_python_dependencies: make aware of IDF_TOOLS_PATH 2019-05-24 17:04:23 +08:00
Sergei Silnov
0f61930872 python: Add check if current python is inside virtual environment 2019-01-08 12:21:33 +01:00
Roland Dobai
aca6e7b66a tools: Be more helpful to MSYS32 users with package installation 2019-01-02 07:53:57 +01:00
Roland Dobai
f7281c75c7 tools: correct the coding style of check_python_dependencies.py 2018-11-30 13:43:28 +01:00
Roland Dobai
98bc172f58 tools: correct printed path on MS Win 2018-11-30 13:43:28 +01:00
Shivani Tipnis
e1774cb6f9 Update minimum version for cryptography package required 2018-11-19 11:13:37 +05:30
Roland Dobai
f59358dad3 idf.py: Import from pyserial after packages have been checked
Closes https://github.com/espressif/esp-idf/issues/2573
2018-10-25 08:38:00 +02:00
Angus Gratton
246a608db0 windows: Special check for some MSYS2 Python packages
MSYS2 MINGW requires some particular MSYS2-specific packages.

Closes https://github.com/espressif/esp-idf/issues/2480
Closes https://github.com/espressif/esp-idf/issues/2474
Closes https://github.com/espressif/esp-idf/issues/2486
2018-10-01 17:21:48 +10:00
Roland Dobai
c3d99dd2e4 Encourage to install python packages without administrative rights
Using 'sudo pip install' can cause a lot of problems because usually
there are a couple of Python interpreters installed. This fix encourages
developers to use `pip install --user` instead which is safer.
2018-09-11 08:54:37 +02:00
Roland Dobai
7cfef29eb8 Docs: Check Python packages 2018-08-30 13:20:42 +02:00
Roland Dobai
62cad3a7d7 Add python-future by using python requirements file 2018-08-23 08:28:57 +02:00