| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2024, Stanislav Shamilov <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: decompress |
| 11 |
short_description: Decompresses compressed files |
| 12 |
version_added: 10.1.0 |
| 13 |
description: |
| 14 |
- Decompresses compressed files. |
| 15 |
- The source (compressed) file and destination (decompressed) files are on the remote host. |
| 16 |
- Source file can be deleted after decompression. |
| 17 |
extends_documentation_fragment: |
| 18 |
- ansible.builtin.files |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
src: |
| 27 |
description: |
| 28 |
- Remote absolute path for the file to decompress. |
| 29 |
type: path |
| 30 |
required: true |
| 31 |
dest: |
| 32 |
description: |
| 33 |
- The file name of the destination file where the compressed file is decompressed. |
| 34 |
- If the destination file exists, it is truncated and overwritten. |
| 35 |
- If not specified, the destination filename is derived from O(src) by removing the compression format extension. For |
| 36 |
example, when O(src) is V(/path/to/file.txt.gz) and O(format) is V(gz), O(dest) is V(/path/to/file.txt). If the O(src) |
| 37 |
file does not have an extension for the current O(format), the O(dest) filename is made by appending C(_decompressed) |
| 38 |
to the O(src) filename. For instance, when O(src) is V(/path/to/file.myextension), the (dest) filename is V(/path/to/file.myextension_decompressed). |
| 39 |
type: path |
| 40 |
format: |
| 41 |
description: |
| 42 |
- The type of compression to use to decompress. |
| 43 |
type: str |
| 44 |
choices: [gz, bz2, xz] |
| 45 |
default: gz |
| 46 |
remove: |
| 47 |
description: |
| 48 |
- Remove original compressed file after decompression. |
| 49 |
type: bool |
| 50 |
default: false |
| 51 |
requirements: |
| 52 |
- Requires C(lzma) (standard library of Python 3) if using C(xz) format. |
| 53 |
author: |
| 54 |
- Stanislav Shamilov (@shamilovstas) |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
- name: Decompress file /path/to/file.txt.gz into /path/to/file.txt (gz compression is used by default) |
| 59 |
community.general.decompress: |
| 60 |
src: /path/to/file.txt.gz |
| 61 |
dest: /path/to/file.txt |
| 62 |
|
| 63 |
- name: Decompress file /path/to/file.txt.gz into /path/to/file.txt |
| 64 |
community.general.decompress: |
| 65 |
src: /path/to/file.txt.gz |
| 66 |
|
| 67 |
- name: Decompress file compressed with bzip2 |
| 68 |
community.general.decompress: |
| 69 |
src: /path/to/file.txt.bz2 |
| 70 |
dest: /path/to/file.txt |
| 71 |
format: bz2 |
| 72 |
|
| 73 |
- name: Decompress file and delete the compressed file afterwards |
| 74 |
community.general.decompress: |
| 75 |
src: /path/to/file.txt.gz |
| 76 |
dest: /path/to/file.txt |
| 77 |
remove: true |
| 78 |
""" |
| 79 |
|
| 80 |
RETURN = r""" |
| 81 |
dest: |
| 82 |
description: Path to decompressed file. |
| 83 |
type: str |
| 84 |
returned: success |
| 85 |
sample: /path/to/file.txt |
| 86 |
""" |
| 87 |
|
| 88 |
import bz2 |
| 89 |
import filecmp |
| 90 |
import gzip |
| 91 |
import os |
| 92 |
import shutil |
| 93 |
import tempfile |
| 94 |
|
| 95 |
from ansible.module_utils.common.text.converters import to_bytes |
| 96 |
|
| 97 |
from ansible_collections.community.general.plugins.module_utils import _deps as deps |
| 98 |
from ansible_collections.community.general.plugins.module_utils._mh.module_helper import ModuleHelper |
| 99 |
|
| 100 |
with deps.declare("lzma"): |
| 101 |
import lzma |
| 102 |
|
| 103 |
|
| 104 |
def lzma_decompress(src): |
| 105 |
return lzma.open(src, "rb") |
| 106 |
|
| 107 |
|
| 108 |
def bz2_decompress(src): |
| 109 |
return bz2.open(src, "rb") |
| 110 |
|
| 111 |
|
| 112 |
def gzip_decompress(src): |
| 113 |
return gzip.open(src, "rb") |
| 114 |
|
| 115 |
|
| 116 |
def decompress(b_src, b_dest, handler): |
| 117 |
with handler(b_src) as src_file: |
| 118 |
with open(b_dest, "wb") as dest_file: |
| 119 |
shutil.copyfileobj(src_file, dest_file) |
| 120 |
|
| 121 |
|
| 122 |
class Decompress(ModuleHelper): |
| 123 |
output_params = "dest" |
| 124 |
|
| 125 |
module = dict( |
| 126 |
argument_spec=dict( |
| 127 |
src=dict(type="path", required=True), |
| 128 |
dest=dict(type="path"), |
| 129 |
format=dict(type="str", default="gz", choices=["gz", "bz2", "xz"]), |
| 130 |
remove=dict(type="bool", default=False), |
| 131 |
), |
| 132 |
add_file_common_args=True, |
| 133 |
supports_check_mode=True, |
| 134 |
) |
| 135 |
|
| 136 |
def __init_module__(self): |
| 137 |
self.handlers = {"gz": gzip_decompress, "bz2": bz2_decompress, "xz": lzma_decompress} |
| 138 |
if self.vars.dest is None: |
| 139 |
self.vars.dest = self.get_destination_filename() |
| 140 |
deps.validate(self.module) |
| 141 |
self.configure() |
| 142 |
|
| 143 |
def configure(self): |
| 144 |
b_dest = to_bytes(self.vars.dest, errors="surrogate_or_strict") |
| 145 |
b_src = to_bytes(self.vars.src, errors="surrogate_or_strict") |
| 146 |
if not os.path.exists(b_src): |
| 147 |
if self.vars.remove and os.path.exists(b_dest): |
| 148 |
self.module.exit_json(changed=False) |
| 149 |
else: |
| 150 |
self.do_raise(msg=f"Path does not exist: '{b_src}'") |
| 151 |
if os.path.isdir(b_src): |
| 152 |
self.do_raise(msg=f"Cannot decompress directory '{b_src}'") |
| 153 |
if os.path.isdir(b_dest): |
| 154 |
self.do_raise(msg=f"Destination is a directory, cannot decompress: '{b_dest}'") |
| 155 |
|
| 156 |
def __run__(self): |
| 157 |
b_dest = to_bytes(self.vars.dest, errors="surrogate_or_strict") |
| 158 |
b_src = to_bytes(self.vars.src, errors="surrogate_or_strict") |
| 159 |
|
| 160 |
file_args = self.module.load_file_common_arguments(self.module.params, path=self.vars.dest) |
| 161 |
handler = self.handlers[self.vars.format] |
| 162 |
try: |
| 163 |
tempfd, temppath = tempfile.mkstemp(dir=self.module.tmpdir) |
| 164 |
self.module.add_cleanup_file(temppath) |
| 165 |
b_temppath = to_bytes(temppath, errors="surrogate_or_strict") |
| 166 |
decompress(b_src, b_temppath, handler) |
| 167 |
except OSError as e: |
| 168 |
self.do_raise(msg=f"Unable to create temporary file '{e}'") |
| 169 |
|
| 170 |
if os.path.exists(b_dest): |
| 171 |
self.changed = not filecmp.cmp(b_temppath, b_dest, shallow=False) |
| 172 |
else: |
| 173 |
self.changed = True |
| 174 |
|
| 175 |
if self.changed and not self.module.check_mode: |
| 176 |
try: |
| 177 |
self.module.atomic_move(b_temppath, b_dest) |
| 178 |
except OSError: |
| 179 |
self.do_raise(msg=f"Unable to move temporary file '{b_temppath}' to '{self.vars.dest}'") |
| 180 |
|
| 181 |
if self.vars.remove and not self.check_mode: |
| 182 |
os.remove(b_src) |
| 183 |
self.changed = self.module.set_fs_attributes_if_different(file_args, self.changed) |
| 184 |
|
| 185 |
def get_destination_filename(self): |
| 186 |
src = self.vars.src |
| 187 |
fmt_extension = f".{self.vars.format}" |
| 188 |
if src.endswith(fmt_extension) and len(src) > len(fmt_extension): |
| 189 |
filename = src[: -len(fmt_extension)] |
| 190 |
else: |
| 191 |
filename = f"{src}_decompressed" |
| 192 |
return filename |
| 193 |
|
| 194 |
|
| 195 |
def main(): |
| 196 |
Decompress.execute() |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__": |
| 200 |
main() |