Files
sigma-rules/detection_rules/semver.py
T
Justin Ibarra 2ee5a185c7 Add test command to verify version collisions do not occur (#2272)
* Add test command to verify version collisions do not occur
* add max_allowable_version to schema and lock flow
* add max_allowable_version to all entries in version.lock
* add test-version-lock command
* use min supported stack if > locked min stack
* share lock conversion code with rule and lock to fix M.m bug
2022-09-19 09:53:30 -06:00

37 lines
1.2 KiB
Python

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
"""Helper functionality for comparing semantic versions."""
import re
from typing import Iterable, Union
class Version(tuple):
def __new__(cls, version: Union[str, Iterable]) -> 'Version':
if isinstance(version, (int, list, tuple)):
version_class = tuple.__new__(cls, version)
else:
version_tuple = tuple(int(a) if a.isdigit() else a for a in re.split(r'[.-]', version))
version_class = tuple.__new__(cls, version_tuple)
return version_class
def __str__(self):
"""Convert back to a string."""
recovered_str = str(self[0])
for additional in self[1:]:
if isinstance(additional, str):
recovered_str += "-" + additional
else:
recovered_str += "." + str(additional)
return recovered_str
def max_versions(*versions: str) -> str:
"""Return the max versioned string."""
return str(max([Version(v) for v in versions]))