-
Notifications
You must be signed in to change notification settings - Fork 8
/
_helper_functions.py
45 lines (38 loc) · 1.69 KB
/
_helper_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#
# SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
#
# SPDX-License-Identifier: Apache-2.0
#
from colorama import Fore
from colorama import Style
from packaging.requirements import Requirement
def print_color(text:str, color:str = Fore.BLUE):
"""Print colored text specified by color argument based on colorama
- default color BLUE
"""
print(f'{color}', f'{text}', Style.RESET_ALL)
def merge_requirements(requirement:Requirement, another_req:Requirement) -> Requirement:
"""Merges two requirements into one requirement."""
new_ver_specifier = ''
new_markers = ''
if requirement.specifier and another_req.specifier:
if not another_req.marker and ('==' not in str(requirement.specifier)
and '!=' not in str(requirement.specifier)):
new_ver_specifier = f'{requirement.specifier},{another_req.specifier}'
else:
new_ver_specifier = another_req.specifier
elif requirement.specifier and not another_req.specifier:
new_ver_specifier = requirement.specifier
elif not requirement.specifier and another_req.specifier:
new_ver_specifier = another_req.specifier
if requirement.marker and another_req.marker:
new_markers = f'({requirement.marker}) and ({another_req.marker})'
elif requirement.marker and not another_req.marker:
new_markers = requirement.marker
elif not requirement.marker and another_req.marker:
new_markers = another_req.marker
# construct new requirement
new_requirement = Requirement(
f'{requirement.name}{new_ver_specifier}' + (f'; {new_markers}' if new_markers else '')
)
return new_requirement