From 9e05d2c82e69dbb16687634f3850b77eb1306579 Mon Sep 17 00:00:00 2001 From: Damilola Olufemi Date: Tue, 21 Jan 2025 11:26:44 +0000 Subject: [PATCH] fix: [OSM-2189] refactor sys_platform selector (#257) * fix: refactor system marker regex * fix: PEP508 support of multiple sys_platforms * fix: refactor sys_platform match logic --- pysrc/pip_resolve.py | 9 +++++---- pysrc/test_pip_resolve_py3.py | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pysrc/pip_resolve.py b/pysrc/pip_resolve.py index 145a6404..539d0421 100644 --- a/pysrc/pip_resolve.py +++ b/pysrc/pip_resolve.py @@ -16,7 +16,7 @@ PYTHON_MARKER_REGEX = re.compile(r'python_version\s*(' r'?P==|<=|>=|>|<)\s*[\'"](' r'?P.+?)[\'"]') -SYSTEM_MARKER_REGEX = re.compile(r'sys_platform\s*==\s*[\'"](.+)[\'"]') +SYSTEM_MARKER_REGEX = re.compile(r'sys_platform\s*==\s*[\'"]([\w.-]+)[\'"]') DEPENDENCIES = 'dependencies' VERSION = 'version' NAME = 'name' @@ -271,9 +271,10 @@ def matches_environment(requirement): sys_platform = sys.platform.lower() markers_text = get_markers_text(requirement) if markers_text and 'sys_platform' in markers_text: - match = SYSTEM_MARKER_REGEX.findall(markers_text) - if len(match) > 0: - return match[0].lower() == sys_platform + matches = SYSTEM_MARKER_REGEX.findall(markers_text) + lowercase_matches = [match.lower() for match in matches] + if lowercase_matches: + return sys_platform.lower() in lowercase_matches return True diff --git a/pysrc/test_pip_resolve_py3.py b/pysrc/test_pip_resolve_py3.py index db0203aa..5cbc4cda 100644 --- a/pysrc/test_pip_resolve_py3.py +++ b/pysrc/test_pip_resolve_py3.py @@ -58,6 +58,14 @@ def test_matches_environment(self): req.line = "futures==3.2.0; sys_platform == 'linux2'" self.assertFalse (matches_environment(req)) + mock_sys.platform = "linux" + req.line = "jinja2==3.1.4 ; sys_platform == 'darwin' or sys_platform == 'linux'" + self.assertTrue(matches_environment(req)) + + mock_sys.platform = "darwin" + req.line = "jinja2==3.1.4 ; sys_platform == 'darwin' or sys_platform == 'linux'" + self.assertTrue(matches_environment(req)) + # BUG: Only == operator is supported in the moment # mock_sys.platform = "linux2" # req.line = "futures==3.2.0; sys_platform != 'linux2'"