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
import textwrap
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')
class Error(Exception):
......@@ -179,13 +188,20 @@ def parse(fp, fpname, exp_globals=dict):
# un-escape literal # and ; . Do not use a
# string-escape decode
expr = expression.replace(r'\x23','#').replace(r'\x3b', ';')
# 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]
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
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
# evaluates to false
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