Skip to content

Commit

Permalink
feat: added python version option to yaml files and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-kocka committed Dec 8, 2023
1 parent cffd3a6 commit 658b1b8
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 25 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ The opposite logic of exclude_list is handled by the function itself, which mean

For every `package_name` there are options:
* `version`
- supports all logic operators defined by PEP508 for versions (<, >, !=, etc.)
- supports all logic operators defined by [PEP508](https://peps.python.org/pep-0508/) for versions (<, >, !=, etc.)
* `platform`
* `python`

which could be a string or a list of strings.

Expand All @@ -45,26 +46,28 @@ exclude_list template:
- package_name: '<name_of_package>'
version: '<package_version_with_operator>' / ['<package_version_with_operator>', '<package_version_with_operator>'] # optional
platform: '<platform>' / ['<platform>', '<platform>', '<platform>'] # optional
python: '<python_version_with_operator>' / ['<python_version>', '<python_version>', '<python_version>'] # optional

The syntax can be converted into a sentence: "From assembled **main requirements** exclude `package_name` with `version` on `platform`".
The syntax can be converted into a sentence: "From assembled **main requirements** exclude `package_name` with `version` on `platform` for `python` version".

example:

- package_name: 'pyserial'
version: ['>=3.3', '<3.6']
platform: ['win32', 'linux', 'darwin']
python: '>=3.9'

This would mean: "From assembled **main requirements** exclude `pyserial` with version `>=3.3` and `<3.6` on platform `win32`, `linux`, `darwin`".
This would mean: "From assembled **main requirements** exclude `pyserial` with version `>=3.3` and `<3.6` on platform `win32`, `linux`, `darwin` for `python` version `>=3.9`".

From the example above is clear that the `platform` could be left out (because all main platforms are specified) so the options `platform` or `version` are optional, one of them or both can be not specified and the key can be erased. When only `package_name` is given the package will be excluded from **main requirements**.
From the example above is clear that the `platform` could be left out (because all main platforms are specified) so the options `platform` or `version` or `python` are optional, one of them or both can be not specified and the key can be erased. When only `package_name` is given the package will be excluded from **main requirements**.


### include_list.yaml
File for additional Python packages to the **main requirements** list. Built separately to not restrict the **main requirements** list.

This YAML file uses the same mechanism such as **exclude_list** but without the opposite logic.

The syntax can be also converted into a sentence: "For assembled **main requirements** additionally include `package_name` with `version` on `platform`".
The syntax can be also converted into a sentence: "For assembled **main requirements** additionally include `package_name` with `version` on `platform` for `python` version".


### build_requirements.txt
Expand Down
103 changes: 83 additions & 20 deletions build_wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,27 @@ def assemble_requirements(idf_branches: List[str], idf_constraints: List[str], m


# --- exclude_list and include_list ---
def _change_specifier_logic(specifier: str) -> str:
def _change_specifier_logic(spec_with_text: str) -> tuple:
"""Change specifier logic to opposite
- e.g. "<3" will be ">3"
e.g. "<3" will be ">3"
- return (new_specifier, text_after, old_specifier)
"""
new_spec = specifier
pattern = re.compile(r'(===|==|!=|~=|<=?|>=?|===?)\s*(.*)')
str_match = pattern.findall(spec_with_text)
specifier, text = str_match[0]
replacements = (('<', '>'),
('>', '<'),
('!', '='),
('~', '!'),
('==', '!='))
for old, new in replacements:
if old in specifier and '===' not in specifier:
if old in specifier and specifier != '===':
new_spec = specifier.replace(old, new)
return new_spec
break
elif specifier == '===':
new_spec = '==='
break
return (new_spec, text, specifier)


def yaml_to_requirement(yaml_file:str, exclude: bool = False) -> set:
Expand All @@ -227,23 +235,30 @@ def yaml_to_requirement(yaml_file:str, exclude: bool = False) -> set:

if 'version' in package:
if not isinstance(package['version'], list):
new_spec, ver, old_spec = _change_specifier_logic(package['version'])
requirement_str_list.append(
_change_specifier_logic(package['version']) if exclude else package['version']
f'{new_spec}{ver}' if exclude else f'{old_spec}{ver}'
)

else:
version_list = (
[f'{_change_specifier_logic(ver)}' if exclude else f'{ver}' for ver in package['version']]
)
version_list = []
for elem in package['version']:
new_spec, ver, old_spec = _change_specifier_logic(elem)
if exclude:
version_list.append(f'{new_spec}{ver}')
else:
version_list.append(f'{old_spec}{ver}')

requirement_str_list.append(','.join(version_list))

if 'platform' in package or 'python' in package:
requirement_str_list.append('; ')

if 'platform' in package and 'version' not in package:
if not isinstance(package['platform'], list):
requirement_str_list.append((
f"; sys_platform != '{package['platform']}'" if exclude
else f"; sys_platform == '{package['platform']}'"
f"sys_platform != '{package['platform']}'" if exclude
else f"sys_platform == '{package['platform']}'"
))

else:
Expand All @@ -252,28 +267,76 @@ def yaml_to_requirement(yaml_file:str, exclude: bool = False) -> set:
else f"sys_platform == '{plf}'" for plf in package['platform']]
)

requirement_str_list.append('; ' + ' or '.join(platform_list))
requirement_str_list.append(' or '.join(platform_list))

if ('platform' in package or 'python' in package) and 'version' in package and exclude:
requirement_old_str_list = [f"{package['package_name']}; "]

if 'platform' in package and 'version' in package:
requirement_old_str_list = [f"{package['package_name']}"]

if not isinstance(package['platform'], list):
requirement_str_list.append(f"; sys_platform == '{package['platform']}'")
requirement_str_list.append(f"sys_platform == '{package['platform']}'")

if exclude:
requirement_old_str_list.append(f"; sys_platform != '{package['platform']}'")
requirements_set.add(Requirement(''.join(requirement_old_str_list)))
requirement_old_str_list.append(f"sys_platform != '{package['platform']}'")

else:
platform_list = [f"sys_platform == '{plf}'" for plf in package['platform']]
requirement_str_list.append('; ' + ' or '.join(platform_list))
requirement_str_list.append(' or '.join(platform_list))

if exclude:
platform_list_old = [f"sys_platform != '{plf}'" for plf in package['platform']]
requirement_old_str_list.append('; ' + ' or '.join(platform_list_old))
requirements_set.add(Requirement(''.join(requirement_old_str_list)))
requirement_old_str_list.append(' or '.join(platform_list_old))

if 'platform' in package and 'python' in package:
requirement_str_list.append(' and ')

if ('platform' in package and 'python' in package) and 'version' in package and exclude:
requirement_old_str_list.append(' and ')

if 'python' in package and 'version' not in package:
if not isinstance(package['python'], list):
new_spec, text_after, old_spec = _change_specifier_logic(package['python'])
requirement_str_list.append((
f"python_version {new_spec} '{text_after}'" if exclude
else f"python_version {old_spec} '{text_after}'"
))

else:
python_list = []
for elem in package['python']:
new_spec, text_after, old_spec = _change_specifier_logic(elem)
if exclude:
python_list.append(f"python_version {new_spec} '{text_after}'")
else:
python_list.append(f"python_version {old_spec} '{text_after}'")

requirement_str_list.append(' and '.join(python_list))

if 'python' in package and 'version' in package:

if not isinstance(package['python'], list):
new_spec, text_after, old_spec = _change_specifier_logic(package['python'])
requirement_str_list.append(f"python_version {old_spec} '{text_after}'")

if exclude:
requirement_old_str_list.append(f"python_version {new_spec} '{text_after}'")

else:
python_list = []
python_list_old = []
for elem in package['python']:
new_spec, text_after, old_spec = _change_specifier_logic(elem)

python_list.append(f"python_version {old_spec} '{text_after}'")
if exclude:
python_list_old.append(f"python_version {new_spec} '{text_after}'")
requirement_str_list.append('' + ' and '.join(python_list))

if exclude:
requirement_old_str_list.append(' and '.join(python_list_old))

if ('platform' in package or 'python' in package) and 'version' in package and exclude:
requirements_set.add(Requirement(''.join(requirement_old_str_list)))

requirements_set.add(Requirement(''.join(requirement_str_list)))
return requirements_set
Expand Down
1 change: 1 addition & 0 deletions exclude_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#- package_name: '<name_of_package>'
# version: '<package_version_with_operator>' / ['<package_version_with_operator>', '<package_version_with_operator>'] # optional
# platform: '<platform>' / ['<platform>', '<platform>', '<platform>'] # optional
# python: '<python_version_with_operator>' / ['<python_version>', '<python_version>', '<python_version>'] # optional

# dbus-python can not be build on Windows
- package_name: 'dbus-python'
Expand Down
1 change: 1 addition & 0 deletions include_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#- package_name: '<name_of_package>'
# version: '<package_version_with_operator>' / ['<package_version_with_operator>', '<package_version_with_operator>'] # optional
# platform: '<platform>' / ['<platform>', '<platform>', '<platform>'] # optional
# python: '<python_version_with_operator>' / ['<python_version>', '<python_version>', '<python_version>'] # optional

# lxml is missing as a dependency of pytest-embedded
# https://github.com/espressif/pytest-embedded/blob/main/pyproject.toml
Expand Down
107 changes: 107 additions & 0 deletions test/test_list.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# List of Python packages for testing
# platform
- package_name: 'platform'
platform: 'win32'

- package_name: 'platform'
platform: ['win32', 'linux']
# version
- package_name: 'version'
version: '<42'

- package_name: 'version'
version: ['<42', '>50']
# Python
- package_name: 'python'
python: '>3.10'

- package_name: 'python'
python: ['>3.10', '!=3.8']
# version and platform
- package_name: 'version-platform'
version: '<=0.9.0.2'
platform: 'win32'

- package_name: 'version-platform'
version: ['<=0.9.0.2', '>0.9.1']
platform: 'win32'

- package_name: 'version-platform'
version: '<=0.9.0.2'
platform: ['win32', 'linux']

- package_name: 'version-platform'
version: ['<=0.9.0.2', '>0.9.1']
platform: ['win32', 'linux']
# version and Python
- package_name: 'version-python'
version: '<=0.9.0.2'
python: '<3.8'

- package_name: 'version-python'
version: ['<=0.9.0.2', '>0.9.1']
python: '<3.8'

- package_name: 'version-python'
version: '<=0.9.0.2'
python: ['<3.8', '>3.11']

- package_name: 'version-python'
version: ['<=0.9.0.2', '>0.9.1']
python: ['<3.8', '>3.11']
# platform and Python
- package_name: 'platform-python'
platform: 'win32'
python: '<3.8'

- package_name: 'platform-python'
platform: ['win32', 'linux']
python: '<3.8'

- package_name: 'platform-python'
platform: 'win32'
python: ['<3.8', '>3.11']

- package_name: 'platform-python'
platform: ['win32', 'linux']
python: ['<3.8', '>3.11']
# version and platform and Python
- package_name: 'version-platform-python'
version: '<=0.9.0.2'
platform: 'win32'
python: '<3.8'

- package_name: 'version-platform-python'
version: ['<=0.9.0.2', '>0.9.1']
platform: 'win32'
python: '<3.8'

- package_name: 'version-platform-python'
version: ['<=0.9.0.2', '>0.9.1']
platform: ['win32', 'linux']
python: '<3.8'

- package_name: 'version-platform-python'
version: '<=0.9.0.2'
platform: ['win32', 'linux']
python: '<3.8'

- package_name: 'version-platform-python'
version: '<=0.9.0.2'
platform: 'win32'
python: ['<3.8', '>3.11']

- package_name: 'version-platform-python'
version: ['<=0.9.0.2', '>0.9.1']
platform: 'win32'
python: ['<3.8', '>3.11']

- package_name: 'version-platform-python'
version: ['<=0.9.0.2', '>0.9.1']
platform: ['win32', 'linux']
python: ['<3.8', '>3.11']

- package_name: 'version-platform-python'
version: '<=0.9.0.2'
platform: ['win32', 'linux']
python: ['<3.8', '>3.11']
Loading

0 comments on commit 658b1b8

Please sign in to comment.