From f2af41d842756e60016e34348750dd2b199a0d9f Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sat, 9 May 2020 01:03:04 -0700 Subject: [PATCH] flatpak: Change use of Popen to module.run_command() (#274) * flatpak: Change use of Popen to module.run_command() * Update changelogs/fragments/274-flatpak-run-command.yaml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein --- changelogs/fragments/274-flatpak-run-command.yaml | 2 ++ plugins/modules/packaging/os/flatpak.py | 15 ++++----------- plugins/modules/packaging/os/flatpak_remote.py | 14 ++++---------- tests/sanity/ignore-2.10.txt | 2 -- 4 files changed, 10 insertions(+), 23 deletions(-) create mode 100644 changelogs/fragments/274-flatpak-run-command.yaml diff --git a/changelogs/fragments/274-flatpak-run-command.yaml b/changelogs/fragments/274-flatpak-run-command.yaml new file mode 100644 index 0000000000..1ab4b9ae44 --- /dev/null +++ b/changelogs/fragments/274-flatpak-run-command.yaml @@ -0,0 +1,2 @@ +minor_changes: +- flatpak and flatpak_remote - use ``module.run_command()`` instead of ``subprocess.Popen()``. diff --git a/plugins/modules/packaging/os/flatpak.py b/plugins/modules/packaging/os/flatpak.py index 8fc35f876f..ce9e6fc66f 100644 --- a/plugins/modules/packaging/os/flatpak.py +++ b/plugins/modules/packaging/os/flatpak.py @@ -149,10 +149,8 @@ stdout: sample: "org.gnome.Calendar/x86_64/stable\tcurrent\norg.gnome.gitg/x86_64/stable\tcurrent\n" ''' -import subprocess from ansible.module_utils.six.moves.urllib.parse import urlparse from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils._text import to_native OUTDATED_FLATPAK_VERSION_ERROR_MESSAGE = "Unknown option --columns=application" @@ -249,16 +247,11 @@ def _flatpak_command(module, noop, command, ignore_failure=False): result['command'] = command return "" - process = subprocess.Popen( - command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout_data, stderr_data = process.communicate() - result['rc'] = process.returncode + result['rc'], result['stdout'], result['stderr'] = module.run_command( + command.split(), check_rc=not ignore_failure + ) result['command'] = command - result['stdout'] = to_native(stdout_data) - result['stderr'] = to_native(stderr_data) - if result['rc'] != 0 and not ignore_failure: - module.fail_json(msg="Failed to execute flatpak command", **result) - return to_native(stdout_data) + return result['stdout'] def main(): diff --git a/plugins/modules/packaging/os/flatpak_remote.py b/plugins/modules/packaging/os/flatpak_remote.py index e7d5605190..edd9da9af6 100644 --- a/plugins/modules/packaging/os/flatpak_remote.py +++ b/plugins/modules/packaging/os/flatpak_remote.py @@ -143,7 +143,6 @@ stdout: sample: "flathub\tFlathub\thttps://dl.flathub.org/repo/\t1\t\n" ''' -import subprocess from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_bytes, to_native @@ -187,16 +186,11 @@ def _flatpak_command(module, noop, command): result['command'] = command return "" - process = subprocess.Popen( - command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout_data, stderr_data = process.communicate() - result['rc'] = process.returncode + result['rc'], result['stdout'], result['stderr'] = module.run_command( + command.split(), check_rc=True + ) result['command'] = command - result['stdout'] = stdout_data - result['stderr'] = stderr_data - if result['rc'] != 0: - module.fail_json(msg="Failed to execute flatpak command", **result) - return to_native(stdout_data) + return result['stdout'] def main(): diff --git a/tests/sanity/ignore-2.10.txt b/tests/sanity/ignore-2.10.txt index 0e0947b3b1..34cc9e2164 100644 --- a/tests/sanity/ignore-2.10.txt +++ b/tests/sanity/ignore-2.10.txt @@ -1254,8 +1254,6 @@ plugins/modules/packaging/os/apt_rpm.py validate-modules:doc-default-does-not-ma plugins/modules/packaging/os/apt_rpm.py validate-modules:parameter-invalid plugins/modules/packaging/os/apt_rpm.py validate-modules:parameter-type-not-in-doc plugins/modules/packaging/os/apt_rpm.py validate-modules:undocumented-parameter -plugins/modules/packaging/os/flatpak.py validate-modules:use-run-command-not-popen -plugins/modules/packaging/os/flatpak_remote.py validate-modules:use-run-command-not-popen plugins/modules/packaging/os/homebrew.py validate-modules:parameter-invalid plugins/modules/packaging/os/homebrew_cask.py validate-modules:parameter-invalid plugins/modules/packaging/os/installp.py validate-modules:parameter-list-no-elements