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,13 +188,20 @@ def parse(fp, fpname, exp_globals=dict): ...@@ -179,13 +188,20 @@ 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', ';')
# rebuild a valid Python expression wrapped in a list try:
expr = head + expr + tail # new-style markers as used in pip constraints, e.g.:
# lazily populate context only expression # 'python_version < "3.11" and platform_system == "Windows"'
if not context: marker = packaging.markers.Marker(expr)
context = exp_globals() section_condition = marker.evaluate()
# evaluated expression is in list: get first element except packaging.markers.InvalidMarker:
section_condition = eval(expr, context)[0] # old style buildout expression
# rebuild a valid Python expression wrapped in a list
expr = head + expr + tail
# lazily populate context only expression
if not context:
context = exp_globals()
# evaluated expression is in list: get first element
section_condition = eval(expr, context)[0]
# finally, ignore section when an expression # finally, ignore section when an expression
# evaluates to false # evaluates to false
if not section_condition: if not section_condition:
......
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