From ce544f370c24f651424ca222fb03e5ffb9c3b3ca Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sun, 12 Oct 2025 00:42:01 +1300 Subject: [PATCH] archive: lzma is standard in Python 3.7+ (#10908) * archive: lzma is standard in Python 3.7+ * add changelog frag --- changelogs/fragments/10908-archive-lzma.yml | 2 ++ plugins/modules/archive.py | 28 ++++----------------- 2 files changed, 7 insertions(+), 23 deletions(-) create mode 100644 changelogs/fragments/10908-archive-lzma.yml diff --git a/changelogs/fragments/10908-archive-lzma.yml b/changelogs/fragments/10908-archive-lzma.yml new file mode 100644 index 0000000000..bcce681bed --- /dev/null +++ b/changelogs/fragments/10908-archive-lzma.yml @@ -0,0 +1,2 @@ +minor_changes: + - archive - remove conditional code for older Python versions (https://github.com/ansible-collections/community.general/pull/10908). diff --git a/plugins/modules/archive.py b/plugins/modules/archive.py index 17f28a28bc..fe850391c8 100644 --- a/plugins/modules/archive.py +++ b/plugins/modules/archive.py @@ -179,6 +179,7 @@ import bz2 import glob import gzip import io +import lzma import os import re import shutil @@ -186,23 +187,12 @@ import tarfile import zipfile from fnmatch import fnmatch from traceback import format_exc +from zipfile import BadZipFile from zlib import crc32 -from ansible.module_utils.basic import AnsibleModule, missing_required_lib +from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.common.text.converters import to_bytes, to_native -try: # python 3.2+ - from zipfile import BadZipFile # type: ignore[attr-defined] -except ImportError: # older python - from zipfile import BadZipfile as BadZipFile - -LZMA_IMP_ERR = None -try: - import lzma - HAS_LZMA = True -except ImportError: - LZMA_IMP_ERR = format_exc() - HAS_LZMA = False STATE_ABSENT = 'absent' STATE_ARCHIVED = 'archive' @@ -564,11 +554,8 @@ class TarArchive(Archive): self.file.add(path, archive_name, recursive=False, filter=filter) def _get_checksums(self, path): - if HAS_LZMA: - LZMAError = lzma.LZMAError - else: - # Just picking another exception that's also listed below - LZMAError = tarfile.ReadError + LZMAError = lzma.LZMAError + try: if self.format == 'xz': with lzma.open(_to_native_ascii(path), 'r') as f: @@ -619,11 +606,6 @@ def main(): supports_check_mode=True, ) - if not HAS_LZMA and module.params['format'] == 'xz': - module.fail_json( - msg=missing_required_lib("lzma or backports.lzma", reason="when using xz format"), exception=LZMA_IMP_ERR - ) - check_mode = module.check_mode archive = get_archive(module)