mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-22 13:19:13 +00:00
uv_python module: restrict accepted version formats to X.Y and X.Y.Z
This commit is contained in:
parent
21646442ce
commit
21c6d762cc
1 changed files with 25 additions and 3 deletions
|
|
@ -52,24 +52,46 @@ python:
|
|||
'''
|
||||
|
||||
import re
|
||||
from enum import Enum
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
|
||||
MINOR_RELEASE = re.compile(r"^\d+\.\d+$")
|
||||
PATCH_RELEASE = re.compile(r"^\d+\.\d+\.\d+$")
|
||||
|
||||
class Release(Enum):
|
||||
PATCH = 1
|
||||
MINOR = 2
|
||||
|
||||
def extract_python_version(version: str, module):
|
||||
if PATCH_RELEASE.match(version):
|
||||
return Release.PATCH, version
|
||||
elif MINOR_RELEASE.match(version):
|
||||
return Release.MINOR, version
|
||||
else:
|
||||
module.fail_json(
|
||||
msg=(
|
||||
f"Invalid version '{version}'. "
|
||||
"Expected X.Y or X.Y.Z."
|
||||
)
|
||||
)
|
||||
return None, None
|
||||
|
||||
class UV:
|
||||
"""
|
||||
Documentation for uv python https://docs.astral.sh/uv/concepts/python-versions/#installing-a-python-version
|
||||
"""
|
||||
def __init__(self, module, **kwargs):
|
||||
self.module = module
|
||||
self.python_version = module.params["version"]
|
||||
self.python_version_type, self.python_version = extract_python_version(module.params["version"], module)
|
||||
|
||||
def install_python(self):
|
||||
rc, out, _ = self._find_python()
|
||||
rc, out, _ = self._find_python()
|
||||
if rc == 0:
|
||||
return False, out
|
||||
if self.module.check_mode:
|
||||
return True, ""
|
||||
|
||||
|
||||
cmd = [self.module.get_bin_path("uv", required=True), "python", "install", self.python_version]
|
||||
_, out, _ = self.module.run_command(cmd, check_rc=True)
|
||||
return True, out
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue