mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-05 19:47:12 +00:00
uv_python module: return commands' stderr and return code as a variable of stdout
This commit is contained in:
parent
4659d3f544
commit
afbe833c02
2 changed files with 36 additions and 32 deletions
|
|
@ -87,7 +87,7 @@ class UV:
|
|||
"""
|
||||
rc, out, _ = self._find_python("--show-version")
|
||||
if rc == 0:
|
||||
return False, out.split()[0]
|
||||
return False, out.split()[0], "", rc
|
||||
if self.module.check_mode:
|
||||
_, versions_available, _ = self._list_python()
|
||||
# when uv does not find any available patch version the install command will fail
|
||||
|
|
@ -96,11 +96,11 @@ class UV:
|
|||
self.module.fail_json(msg=(f"Version {self.python_version_str} is not available."))
|
||||
except json.decoder.JSONDecodeError as e:
|
||||
self.module.fail_json(msg=f"Failed to parse 'uv python list' output with error {str(e)}")
|
||||
return True, self.python_version_str
|
||||
return True, self.python_version_str, "", 0
|
||||
|
||||
cmd = [self.module.get_bin_path("uv", required=True), self.subcommand, "install", self.python_version_str]
|
||||
self.module.run_command(cmd, check_rc=True)
|
||||
return True, self.python_version_str
|
||||
rc, _, err = self.module.run_command(cmd, check_rc=True)
|
||||
return True, self.python_version_str, err, rc
|
||||
|
||||
def uninstall_python(self):
|
||||
"""
|
||||
|
|
@ -110,16 +110,18 @@ class UV:
|
|||
- boolean to indicate if method changed state
|
||||
- removed version
|
||||
"""
|
||||
rc, _, _ = self._find_python("--show-version")
|
||||
# if "uv python find" fails, it means specified version does not exist
|
||||
if rc != 0:
|
||||
return False, ""
|
||||
rc, _, err = self._find_python("--show-version")
|
||||
# if "uv python find" fails with return code 2, it means specified version is not installed or is not available
|
||||
if rc == 2:
|
||||
return False, "", "", 0
|
||||
elif rc != 0:
|
||||
self.module.fail_json(msg=err)
|
||||
if self.module.check_mode:
|
||||
return True, self.python_version_str
|
||||
return True, self.python_version_str, "", 0
|
||||
|
||||
cmd = [self.module.get_bin_path("uv", required=True), self.subcommand, "uninstall", self.python_version_str]
|
||||
self.module.run_command(cmd, check_rc=True)
|
||||
return True, self.python_version_str
|
||||
rc, _, err = self.module.run_command(cmd, check_rc=True)
|
||||
return True, self.python_version_str, err, rc
|
||||
|
||||
def upgrade_python(self):
|
||||
"""
|
||||
|
|
@ -131,15 +133,15 @@ class UV:
|
|||
rc, out, _ = self._find_python("--show-version")
|
||||
latest_version = self._get_latest_patch_release()
|
||||
if not latest_version:
|
||||
self.module.fail_json(msg=(f"Version {self.python_version_str} is not available."))
|
||||
self.module.fail_json(msg=f"Version {self.python_version_str} is not available.")
|
||||
if rc == 0 and out.split()[0] == latest_version:
|
||||
return False, latest_version
|
||||
return False, latest_version, "", rc
|
||||
if self.module.check_mode:
|
||||
return True, latest_version
|
||||
return True, latest_version, "", 0
|
||||
|
||||
cmd = [self.module.get_bin_path("uv", required=True), self.subcommand, "install", latest_version]
|
||||
self.module.run_command(cmd, check_rc=True)
|
||||
return True, latest_version
|
||||
rc, _, err = self.module.run_command(cmd, check_rc=True)
|
||||
return True, latest_version, err, rc
|
||||
|
||||
def _find_python(self, *args):
|
||||
"""
|
||||
|
|
@ -194,18 +196,20 @@ def main():
|
|||
|
||||
result = dict(
|
||||
changed=False,
|
||||
msg="",
|
||||
stdout="",
|
||||
stderr="",
|
||||
rc=0,
|
||||
failed=False
|
||||
)
|
||||
state = module.params["state"]
|
||||
|
||||
uv = UV(module)
|
||||
if state == "present":
|
||||
result["changed"], result["msg"] = uv.install_python()
|
||||
result["changed"], result["stdout"], result["stderr"], result["rc"] = uv.install_python()
|
||||
elif state == "absent":
|
||||
result["changed"], result["msg"] = uv.uninstall_python()
|
||||
result["changed"], result["stdout"], result["stderr"], result["rc"] = uv.uninstall_python()
|
||||
elif state == "latest":
|
||||
result["changed"], result["msg"] = uv.upgrade_python()
|
||||
result["changed"], result["stdout"], result["stderr"], result["rc"] = uv.upgrade_python()
|
||||
|
||||
module.exit_json(**result)
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@
|
|||
that:
|
||||
- install_check_mode.changed is true
|
||||
- install_check_mode.failed is false
|
||||
- '"3.14" in install_check_mode.msg'
|
||||
- '"3.14" in install_check_mode.stdout'
|
||||
|
||||
- name: Install python 3.14
|
||||
uv_python:
|
||||
|
|
@ -52,7 +52,7 @@
|
|||
that:
|
||||
- install_python.changed is true
|
||||
- install_python.failed is false
|
||||
- '"3.14" in install_python.msg'
|
||||
- '"3.14" in install_python.stdout'
|
||||
|
||||
- name: Re-install python 3.14
|
||||
uv_python:
|
||||
|
|
@ -65,7 +65,7 @@
|
|||
that:
|
||||
- reinstall_python.changed is false
|
||||
- reinstall_python.failed is false
|
||||
- '"3.14" in reinstall_python.msg'
|
||||
- '"3.14" in reinstall_python.stdout'
|
||||
|
||||
- name: Install latest python 3.15 # installs latest patch version for 3.15
|
||||
uv_python:
|
||||
|
|
@ -78,7 +78,7 @@
|
|||
that:
|
||||
- install_latest_python.changed is true
|
||||
- install_latest_python.failed is false
|
||||
- '"3.15" in install_latest_python.msg'
|
||||
- '"3.15" in install_latest_python.stdout'
|
||||
|
||||
- name: Install python 3.13.5
|
||||
uv_python:
|
||||
|
|
@ -91,7 +91,7 @@
|
|||
that:
|
||||
- install_python.changed is true
|
||||
- install_python.failed is false
|
||||
- '"3.13.5" in install_python.msg'
|
||||
- '"3.13.5" in install_python.stdout'
|
||||
|
||||
- name: Re-install python 3.13.5
|
||||
uv_python:
|
||||
|
|
@ -104,7 +104,7 @@
|
|||
that:
|
||||
- reinstall_python.changed is false
|
||||
- reinstall_python.failed is false
|
||||
- '"3.13.5" in reinstall_python.msg'
|
||||
- '"3.13.5" in reinstall_python.stdout'
|
||||
|
||||
- name: Remove unexisting python 3.10 # removes latest patch version for 3.10 if exists
|
||||
uv_python:
|
||||
|
|
@ -117,7 +117,7 @@
|
|||
that:
|
||||
- remove_python.changed is false
|
||||
- remove_python.failed is false
|
||||
- 'remove_python.msg == ""'
|
||||
- 'remove_python.stdout == ""'
|
||||
|
||||
- name: Remove python 3.13.5 in check mode
|
||||
uv_python:
|
||||
|
|
@ -131,7 +131,7 @@
|
|||
that:
|
||||
- remove_python_in_check_mode.changed is true
|
||||
- remove_python_in_check_mode.failed is false
|
||||
- '"3.13.5" in remove_python_in_check_mode.msg'
|
||||
- '"3.13.5" in remove_python_in_check_mode.stdout'
|
||||
|
||||
- name: Remove python 3.13.5
|
||||
uv_python:
|
||||
|
|
@ -144,7 +144,7 @@
|
|||
that:
|
||||
- remove_python.changed is true
|
||||
- remove_python.failed is false
|
||||
- '"3.13.5" in remove_python_in_check_mode.msg'
|
||||
- '"3.13.5" in remove_python_in_check_mode.stdout'
|
||||
|
||||
- name: Remove python 3.13.5 again
|
||||
uv_python:
|
||||
|
|
@ -157,7 +157,7 @@
|
|||
that:
|
||||
- remove_python.changed is false
|
||||
- remove_python.failed is false
|
||||
- 'remove_python.msg == ""'
|
||||
- 'remove_python.stdout == ""'
|
||||
|
||||
- name: Install python 3
|
||||
uv_python:
|
||||
|
|
@ -183,7 +183,7 @@
|
|||
that:
|
||||
- upgrade_python.changed is true
|
||||
- upgrade_python.failed is false
|
||||
- '"3.13" in upgrade_python.msg'
|
||||
- '"3.13" in upgrade_python.stdout'
|
||||
|
||||
- name: Upgrade python 3.13 in check mode
|
||||
uv_python:
|
||||
|
|
@ -197,7 +197,7 @@
|
|||
that:
|
||||
- upgrade_python.changed is false
|
||||
- upgrade_python.failed is false
|
||||
- '"3.13" in upgrade_python.msg'
|
||||
- '"3.13" in upgrade_python.stdout'
|
||||
|
||||
- name: Install unexisting python version in check mode
|
||||
uv_python:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue