How to use the packaging.markers function in packaging

To help you get started, we’ve selected a few packaging examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sarugaku / resolvelib / tests / functional / python / test_resolvers_python.py View on Github external
def _eval_marker(marker, extras=(None,)):
    if not marker:
        return True
    if not isinstance(marker, packaging.markers.Marker):
        marker = packaging.markers.Marker(marker)
    return any(marker.evaluate({"extra": extra}) for extra in extras)
github pypa / pipenv / pipenv / vendor / requirementslib / models / project.py View on Github external
def _are_pipfile_entries_equal(a, b):
    a = {k: v for k, v in a.items() if k not in ("markers", "hashes", "hash")}
    b = {k: v for k, v in b.items() if k not in ("markers", "hashes", "hash")}
    if a != b:
        return False
    try:
        marker_eval_a = packaging.markers.Marker(a["markers"]).evaluate()
    except (AttributeError, KeyError, TypeError, ValueError):
        marker_eval_a = True
    try:
        marker_eval_b = packaging.markers.Marker(b["markers"]).evaluate()
    except (AttributeError, KeyError, TypeError, ValueError):
        marker_eval_b = True
    return marker_eval_a == marker_eval_b
github sarugaku / requirementslib / src / requirementslib / models / project.py View on Github external
def _are_pipfile_entries_equal(a, b):
    a = {k: v for k, v in a.items() if k not in ("markers", "hashes", "hash")}
    b = {k: v for k, v in b.items() if k not in ("markers", "hashes", "hash")}
    if a != b:
        return False
    try:
        marker_eval_a = packaging.markers.Marker(a["markers"]).evaluate()
    except (AttributeError, KeyError, TypeError, ValueError):
        marker_eval_a = True
    try:
        marker_eval_b = packaging.markers.Marker(b["markers"]).evaluate()
    except (AttributeError, KeyError, TypeError, ValueError):
        marker_eval_b = True
    return marker_eval_a == marker_eval_b
github openstack / requirements / openstack_requirements / check.py View on Github external
if constraint_setting.markers == req.markers:
                return constraint_setting
            if not constraint_setting.markers:
                # There is no point in performing the complex
                # comparison for a constraint that has no markers, so
                # we skip it here. If we find no closer match then the
                # loop at the end of the function will look for a
                # constraint without a marker and use that.
                continue
            # NOTE(dhellmann): This is a very naive attempt to check
            # marker compatibility that relies on internal
            # implementation details of the packaging library.  The
            # best way to ensure the constraint and requirements match
            # is to use the same marker string in the corresponding
            # lines.
            c_markers = markers.Marker(constraint_setting.markers)
            env = {
                str(var): str(val)
                for var, op, val in c_markers._markers  # WARNING: internals
            }
            if req_markers.evaluate(env):
                return constraint_setting
    # Try looking for a constraint without any markers.
    for constraint_setting, _ in constraints:
        if not constraint_setting.markers:
            return constraint_setting
    return None
github openstack / requirements / openstack_requirements / check.py View on Github external
def _get_python3_reqs(reqs):
    """Filters out the reqs that are less than our minimum version."""
    results = []
    for req in reqs:
        if not req.markers:
            results.append(req)
        else:
            req_markers = markers.Marker(req.markers)
            if req_markers.evaluate({
                'python_version': MIN_PY_VERSION,
            }):
                results.append(req)
    return results
github dephell / dephell / dephell / markers / markers.py View on Github external
def _parse(markers: Union[list, str, 'Markers', packaging.Marker]):
        if isinstance(markers, list):
            return markers

        if isinstance(markers, str):
            # https://github.com/pypa/packaging/blob/master/packaging/markers.py
            try:
                return packaging._coerce_parse_result(packaging.MARKER.parseString(markers))
            except packaging.ParseException as e:
                err_str = "invalid marker: {0!r}, parse error at {1!r}".format(
                    markers,
                    markers[e.loc:e.loc + 8],
                )
                raise packaging.InvalidMarker(err_str)

        if hasattr(markers, '_markers'):
            return markers._markers

        if hasattr(markers, '_marker'):
            return markers._marker

        raise ValueError('invalid marker')
github dephell / dephell / dephell / models / marker.py View on Github external
extra=('extra', ),
)


ALIASES = {
    'os.name': 'os_name',
    'sys.platform': 'sys_platform',
    'platform.version': 'platform_version',
    'platform.machine': 'platform_machine',
    'platform.python_implementation': 'platform_python_implementation',
    'python_implementation': 'python_implementation',
}


# https://github.com/pypa/packaging/blob/master/packaging/markers.py
class Markers(packaging.Marker):
    def __init__(self, markers: Union[list, str, 'Markers', packaging.Marker]):
        self._markers = self._parse(markers)

    def _parse(self, markers: Union[list, str, 'Markers', packaging.Marker]) -> list:
        if isinstance(markers, list):
            return markers
        if isinstance(markers, str):
            try:
                return packaging._coerce_parse_result(packaging.MARKER.parseString(markers))
            except packaging.ParseException as e:
                err_str = "invalid marker: {0!r}, parse error at {1!r}".format(
                    markers,
                    markers[e.loc:e.loc + 8],
                )
                raise packaging.InvalidMarker(err_str)
        return markers._markers
github sarugaku / passa / src / passa / models / projects.py View on Github external
def _are_pipfile_entries_equal(a, b):
    a = {k: v for k, v in a.items() if k not in ("markers", "hashes", "hash")}
    b = {k: v for k, v in b.items() if k not in ("markers", "hashes", "hash")}
    if a != b:
        return False
    try:
        marker_eval_a = packaging.markers.Marker(a["markers"]).evaluate()
    except (AttributeError, KeyError, TypeError, ValueError):
        marker_eval_a = True
    try:
        marker_eval_b = packaging.markers.Marker(b["markers"]).evaluate()
    except (AttributeError, KeyError, TypeError, ValueError):
        marker_eval_b = True
    return marker_eval_a == marker_eval_b
github dephell / dephell / dephell / markers / markers.py View on Github external
def _parse(markers: Union[list, str, 'Markers', packaging.Marker]):
        if isinstance(markers, list):
            return markers

        if isinstance(markers, str):
            # https://github.com/pypa/packaging/blob/master/packaging/markers.py
            try:
                return packaging._coerce_parse_result(packaging.MARKER.parseString(markers))
            except packaging.ParseException as e:
                err_str = "invalid marker: {0!r}, parse error at {1!r}".format(
                    markers,
                    markers[e.loc:e.loc + 8],
                )
                raise packaging.InvalidMarker(err_str)

        if hasattr(markers, '_markers'):
            return markers._markers

        if hasattr(markers, '_marker'):
            return markers._marker

        raise ValueError('invalid marker')