Commit 43a54e75 authored by Maurits van Rees's avatar Maurits van Rees Committed by Godefroid Chapelle

Add support for PEP 508 markers in section condition expressions.

For example: `[versions:python_version <= "3.9"]`.
Fixes https://github.com/buildout/buildout/issues/621
parent 5714afdd
Add support for PEP 508 markers in section condition expressions.
For example: ``[versions:python_version <= "3.9"]``.
[maurits]
...@@ -21,6 +21,15 @@ import re ...@@ -21,6 +21,15 @@ import re
import textwrap import textwrap
import logging import logging
try:
import packaging
except ImportError:
try:
from pip._vendor import packaging
except ImportError:
from pkg_resources import packaging
logger = logging.getLogger('zc.buildout') logger = logging.getLogger('zc.buildout')
class Error(Exception): class Error(Exception):
...@@ -179,6 +188,13 @@ def parse(fp, fpname, exp_globals=dict): ...@@ -179,6 +188,13 @@ def parse(fp, fpname, exp_globals=dict):
# un-escape literal # and ; . Do not use a # un-escape literal # and ; . Do not use a
# string-escape decode # string-escape decode
expr = expression.replace(r'\x23','#').replace(r'\x3b', ';') expr = expression.replace(r'\x23','#').replace(r'\x3b', ';')
try:
# new-style markers as used in pip constraints, e.g.:
# 'python_version < "3.11" and platform_system == "Windows"'
marker = packaging.markers.Marker(expr)
section_condition = marker.evaluate()
except packaging.markers.InvalidMarker:
# old style buildout expression
# rebuild a valid Python expression wrapped in a list # rebuild a valid Python expression wrapped in a list
expr = head + expr + tail expr = head + expr + tail
# lazily populate context only expression # lazily populate context only expression
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment