Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_version(version: str, style: Style = Style.Pep440) -> None:
"""
Check if a version is valid for a style.
:param version: Version to check.
:param style: Style against which to check.
"""
name, pattern = {
Style.Pep440: ("PEP 440", _VALID_PEP440),
Style.SemVer: ("Semantic Versioning", _VALID_SEMVER),
Style.Pvp: ("PVP", _VALID_PVP),
}[style]
failure_message = "Version '{}' does not conform to the {} style".format(version, name)
if not re.search(pattern, version):
raise ValueError(failure_message)
if style == Style.SemVer:
parts = re.split(r"[.-]", version.split("+", 1)[0])
if any(re.search(r"^0[0-9]+$", x) for x in parts):
raise ValueError(failure_message)
if self.distance <= 0:
out = serialize_pep440(
self.base, stage=self.stage, revision=self.revision, metadata=meta_parts
)
else:
out = serialize_pep440(
self.base,
stage=self.stage,
revision=self.revision,
post=self.distance,
dev=0,
metadata=meta_parts,
)
elif style == Style.SemVer:
out = serialize_semver(self.base, pre=pre_parts, metadata=meta_parts)
elif style == Style.Pvp:
out = serialize_pvp(self.base, metadata=[*pre_parts, *meta_parts])
check_version(out, style)
return out
"""
Serialize a version based on the Haskell Package Versioning Policy.
Use this instead of `Version.serialize()` if you want more control
over how the version is mapped.
:param base: Version core, such as 0.1.0.
:param metadata: Version tag metadata.
:return: Serialized version.
"""
out = [base]
if metadata is not None and len(metadata) > 0:
out.extend(["-", "-".join(map(str, metadata))])
serialized = "".join(map(str, out))
check_version(serialized, Style.Pvp)
return serialized