Skip to content

Commit

Permalink
fix: [OSM-2189] refactor sys_platform selector (#257)
Browse files Browse the repository at this point in the history
* fix: refactor system marker regex

* fix: PEP508 support of multiple sys_platforms

* fix: refactor sys_platform match logic
  • Loading branch information
DOlufemi authored Jan 21, 2025
1 parent 0b99485 commit 9e05d2c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pysrc/pip_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
PYTHON_MARKER_REGEX = re.compile(r'python_version\s*('
r'?P<operator>==|<=|>=|>|<)\s*[\'"]('
r'?P<python_version>.+?)[\'"]')
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'
Expand Down Expand Up @@ -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


Expand Down
8 changes: 8 additions & 0 deletions pysrc/test_pip_resolve_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand Down

0 comments on commit 9e05d2c

Please sign in to comment.