forked from elastic/detection-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemver.py
26 lines (20 loc) · 934 Bytes
/
semver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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[Iterable, str]) -> 'Version':
if not isinstance(version, (int, list, tuple)):
version = tuple(int(a) if a.isdigit() else a for a in re.split(r'[.-]', version))
return version if isinstance(version, int) else tuple.__new__(cls, version)
def bump(self):
"""Increment the version."""
versions = list(self)
versions[-1] += 1
return Version(versions)
def __str__(self):
"""Convert back to a string."""
return ".".join(str(dig) for dig in self)