unarchive: PEP8, imports, cosmetics (#24654)
- Make PEP8 compliant - Ensure imports are specific - Few cosmetic changes (sort lists, casing, punctuation)
This commit is contained in:
parent
955dc6e250
commit
8cbed3c534
2 changed files with 109 additions and 119 deletions
|
@ -25,131 +25,123 @@ ANSIBLE_METADATA = {'metadata_version': '1.0',
|
||||||
'status': ['preview'],
|
'status': ['preview'],
|
||||||
'supported_by': 'core'}
|
'supported_by': 'core'}
|
||||||
|
|
||||||
|
DOCUMENTATION = r'''
|
||||||
DOCUMENTATION = '''
|
|
||||||
---
|
---
|
||||||
module: unarchive
|
module: unarchive
|
||||||
version_added: 1.4
|
version_added: '1.4'
|
||||||
short_description: Unpacks an archive after (optionally) copying it from the local machine.
|
short_description: Unpacks an archive after (optionally) copying it from the local machine.
|
||||||
extends_documentation_fragment: [files, decrypt]
|
extends_documentation_fragment: [ decrypt, files ]
|
||||||
description:
|
description:
|
||||||
- The C(unarchive) module unpacks an archive. By default, it will copy the source file from the local system to the target before unpacking.
|
- The C(unarchive) module unpacks an archive.
|
||||||
Set remote_src=yes to unpack an archive which already exists on the target.
|
- By default, it will copy the source file from the local system to the target before unpacking.
|
||||||
|
- Set C(remote_src=yes) to unpack an archive which already exists on the target.
|
||||||
options:
|
options:
|
||||||
src:
|
src:
|
||||||
description:
|
description:
|
||||||
- If remote_src=no (default), local path to archive file to copy to the target server; can be absolute or relative. If remote_src=yes, path on the
|
- If C(remote_src=no) (default), local path to archive file to copy to the target server; can be absolute or relative. If C(remote_src=yes), path on the
|
||||||
target server to existing archive file to unpack.
|
target server to existing archive file to unpack.
|
||||||
- If remote_src=yes and src contains ://, the remote machine will download the file from the url first. (version_added 2.0). This is only for
|
- If C(remote_src=yes) and C(src) contains C(://), the remote machine will download the file from the URL first. (version_added 2.0). This is only for
|
||||||
simple cases, for full download support look at the M(get_url) module.
|
simple cases, for full download support look at the M(get_url) module.
|
||||||
required: true
|
required: true
|
||||||
default: null
|
|
||||||
dest:
|
dest:
|
||||||
description:
|
description:
|
||||||
- Remote absolute path where the archive should be unpacked
|
- Remote absolute path where the archive should be unpacked.
|
||||||
required: true
|
required: true
|
||||||
default: null
|
|
||||||
copy:
|
copy:
|
||||||
description:
|
description:
|
||||||
- "If true, the file is copied from local 'master' to the target machine, otherwise, the plugin will look for src archive at the target machine."
|
- If true, the file is copied from local 'master' to the target machine, otherwise, the plugin will look for src archive at the target machine.
|
||||||
- "This option has been deprecated in favor of C(remote_src)"
|
- This option has been deprecated in favor of C(remote_src).
|
||||||
- "This option is mutually exclusive with C(remote_src)."
|
- This option is mutually exclusive with C(remote_src).
|
||||||
required: false
|
type: 'bool'
|
||||||
choices: [ "yes", "no" ]
|
default: 'yes'
|
||||||
default: "yes"
|
|
||||||
creates:
|
creates:
|
||||||
description:
|
description:
|
||||||
- a filename, when it already exists, this step will B(not) be run.
|
- A filename, when it already exists, this step will B(not) be run.
|
||||||
required: no
|
|
||||||
default: null
|
|
||||||
version_added: "1.6"
|
version_added: "1.6"
|
||||||
list_files:
|
list_files:
|
||||||
description:
|
description:
|
||||||
- If set to True, return the list of files that are contained in the tarball.
|
- If set to True, return the list of files that are contained in the tarball.
|
||||||
required: false
|
type: 'bool'
|
||||||
choices: [ "yes", "no" ]
|
default: 'no'
|
||||||
default: "no"
|
|
||||||
version_added: "2.0"
|
version_added: "2.0"
|
||||||
exclude:
|
exclude:
|
||||||
description:
|
description:
|
||||||
- List the directory and file entries that you would like to exclude from the unarchive action.
|
- List the directory and file entries that you would like to exclude from the unarchive action.
|
||||||
required: false
|
|
||||||
default: []
|
|
||||||
version_added: "2.1"
|
version_added: "2.1"
|
||||||
keep_newer:
|
keep_newer:
|
||||||
description:
|
description:
|
||||||
- Do not replace existing files that are newer than files from the archive.
|
- Do not replace existing files that are newer than files from the archive.
|
||||||
required: false
|
type: 'bool'
|
||||||
default: no
|
default: 'no'
|
||||||
version_added: "2.1"
|
version_added: "2.1"
|
||||||
extra_opts:
|
extra_opts:
|
||||||
description:
|
description:
|
||||||
- Specify additional options by passing in an array.
|
- Specify additional options by passing in an array.
|
||||||
default:
|
default: ""
|
||||||
required: false
|
|
||||||
version_added: "2.1"
|
version_added: "2.1"
|
||||||
remote_src:
|
remote_src:
|
||||||
description:
|
description:
|
||||||
- "Set to C(yes) to indicate the archived file is already on the remote system and not local to the Ansible controller."
|
- Set to C(yes) to indicate the archived file is already on the remote system and not local to the Ansible controller.
|
||||||
- "This option is mutually exclusive with C(copy)."
|
- This option is mutually exclusive with C(copy).
|
||||||
required: false
|
type: 'bool'
|
||||||
default: "no"
|
default: 'no'
|
||||||
choices: ["yes", "no"]
|
|
||||||
version_added: "2.2"
|
version_added: "2.2"
|
||||||
validate_certs:
|
validate_certs:
|
||||||
description:
|
description:
|
||||||
- This only applies if using a https url as the source of the file.
|
- This only applies if using a https URL as the source of the file.
|
||||||
- This should only set to C(no) used on personally controlled sites using self-signed cer
|
- This should only set to C(no) used on personally controlled sites using self-signed certificate.
|
||||||
- Prior to 2.2 the code worked as if this was set to C(yes).
|
- Prior to 2.2 the code worked as if this was set to C(yes).
|
||||||
required: false
|
type: 'bool'
|
||||||
default: "yes"
|
default: 'yes'
|
||||||
choices: ["yes", "no"]
|
|
||||||
version_added: "2.2"
|
version_added: "2.2"
|
||||||
author: "Dag Wieers (@dagwieers)"
|
author: Dag Wieers (@dagwieers)
|
||||||
todo:
|
todo:
|
||||||
- re-implement tar support using native tarfile module
|
- Re-implement tar support using native tarfile module.
|
||||||
- re-implement zip support using native zipfile module
|
- Re-implement zip support using native zipfile module.
|
||||||
notes:
|
notes:
|
||||||
- requires C(gtar)/C(unzip) command on target host
|
- Requires C(gtar)/C(unzip) command on target host.
|
||||||
- can handle I(.zip) files using C(unzip) as well as I(.tar), I(.tar.gz), I(.tar.bz2) and I(.tar.xz) files using C(gtar)
|
- Can handle I(.zip) files using C(unzip) as well as I(.tar), I(.tar.gz), I(.tar.bz2) and I(.tar.xz) files using C(gtar).
|
||||||
- uses gtar's C(--diff arg) to calculate if changed or not. If this C(arg) is not
|
- Uses gtar's C(--diff) arg to calculate if changed or not. If this C(arg) is not
|
||||||
supported, it will always unpack the archive
|
supported, it will always unpack the archive.
|
||||||
- existing files/directories in the destination which are not in the archive
|
- Existing files/directories in the destination which are not in the archive
|
||||||
are not touched. This is the same behavior as a normal archive extraction
|
are not touched. This is the same behavior as a normal archive extraction.
|
||||||
- existing files/directories in the destination which are not in the archive
|
- Existing files/directories in the destination which are not in the archive
|
||||||
are ignored for purposes of deciding if the archive should be unpacked or not
|
are ignored for purposes of deciding if the archive should be unpacked or not.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = r'''
|
||||||
# Example from Ansible Playbooks
|
- name: Extract foo.tgz into /var/lib/foo
|
||||||
- unarchive:
|
unarchive:
|
||||||
src: foo.tgz
|
src: foo.tgz
|
||||||
dest: /var/lib/foo
|
dest: /var/lib/foo
|
||||||
|
|
||||||
# Unarchive a file that is already on the remote machine
|
- name: Unarchive a file that is already on the remote machine
|
||||||
- unarchive:
|
unarchive:
|
||||||
src: /tmp/foo.zip
|
src: /tmp/foo.zip
|
||||||
dest: /usr/local/bin
|
dest: /usr/local/bin
|
||||||
remote_src: True
|
remote_src: yes
|
||||||
|
|
||||||
# Unarchive a file that needs to be downloaded (added in 2.0)
|
- name: Unarchive a file that needs to be downloaded (added in 2.0)
|
||||||
- unarchive:
|
- unarchive:
|
||||||
src: https://example.com/example.zip
|
src: https://example.com/example.zip
|
||||||
dest: /usr/local/bin
|
dest: /usr/local/bin
|
||||||
remote_src: True
|
remote_src: yes
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import re
|
|
||||||
import os
|
|
||||||
import stat
|
|
||||||
import pwd
|
|
||||||
import grp
|
|
||||||
import datetime
|
|
||||||
import time
|
|
||||||
import binascii
|
import binascii
|
||||||
import codecs
|
import codecs
|
||||||
|
import datetime
|
||||||
|
import grp
|
||||||
|
import os
|
||||||
|
import pwd
|
||||||
|
import re
|
||||||
|
import stat
|
||||||
|
import time
|
||||||
from zipfile import ZipFile, BadZipfile
|
from zipfile import ZipFile, BadZipfile
|
||||||
from ansible.module_utils._text import to_bytes, to_text
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.urls import fetch_url
|
||||||
|
from ansible.module_utils._text import to_bytes, to_native, to_text
|
||||||
|
|
||||||
try: # python 3.3+
|
try: # python 3.3+
|
||||||
from shlex import quote
|
from shlex import quote
|
||||||
|
@ -170,18 +162,21 @@ ZIP_FILE_MODE_RE = re.compile(r'([r-][w-][SsTtx-]){3}')
|
||||||
# saving to a tempfile (64k)
|
# saving to a tempfile (64k)
|
||||||
BUFSIZE = 65536
|
BUFSIZE = 65536
|
||||||
|
|
||||||
|
|
||||||
def crc32(path):
|
def crc32(path):
|
||||||
''' Return a CRC32 checksum of a file '''
|
''' Return a CRC32 checksum of a file '''
|
||||||
return binascii.crc32(open(path, 'rb').read()) & 0xffffffff
|
return binascii.crc32(open(path, 'rb').read()) & 0xffffffff
|
||||||
|
|
||||||
|
|
||||||
def shell_escape(string):
|
def shell_escape(string):
|
||||||
''' Quote meta-characters in the args for the unix shell '''
|
''' Quote meta-characters in the args for the unix shell '''
|
||||||
return re.sub(r'([^A-Za-z0-9_])', r'\\\1', string)
|
return re.sub(r'([^A-Za-z0-9_])', r'\\\1', string)
|
||||||
|
|
||||||
|
|
||||||
class UnarchiveError(Exception):
|
class UnarchiveError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# class to handle .zip files
|
|
||||||
class ZipArchive(object):
|
class ZipArchive(object):
|
||||||
|
|
||||||
def __init__(self, src, dest, file_args, module):
|
def __init__(self, src, dest, file_args, module):
|
||||||
|
@ -585,7 +580,6 @@ class ZipArchive(object):
|
||||||
return False, 'Command "%s" could not handle archive.' % self.cmd_path
|
return False, 'Command "%s" could not handle archive.' % self.cmd_path
|
||||||
|
|
||||||
|
|
||||||
# class to handle gzipped tar files
|
|
||||||
class TgzArchive(object):
|
class TgzArchive(object):
|
||||||
|
|
||||||
def __init__(self, src, dest, file_args, module):
|
def __init__(self, src, dest, file_args, module):
|
||||||
|
@ -727,7 +721,7 @@ class TgzArchive(object):
|
||||||
return False, 'Command "%s" found no files in archive.' % self.cmd_path
|
return False, 'Command "%s" found no files in archive.' % self.cmd_path
|
||||||
|
|
||||||
|
|
||||||
# class to handle tar files that aren't compressed
|
# Class to handle tar files that aren't compressed
|
||||||
class TarArchive(TgzArchive):
|
class TarArchive(TgzArchive):
|
||||||
def __init__(self, src, dest, file_args, module):
|
def __init__(self, src, dest, file_args, module):
|
||||||
super(TarArchive, self).__init__(src, dest, file_args, module)
|
super(TarArchive, self).__init__(src, dest, file_args, module)
|
||||||
|
@ -735,14 +729,14 @@ class TarArchive(TgzArchive):
|
||||||
self.zipflag = ''
|
self.zipflag = ''
|
||||||
|
|
||||||
|
|
||||||
# class to handle bzip2 compressed tar files
|
# Class to handle bzip2 compressed tar files
|
||||||
class TarBzipArchive(TgzArchive):
|
class TarBzipArchive(TgzArchive):
|
||||||
def __init__(self, src, dest, file_args, module):
|
def __init__(self, src, dest, file_args, module):
|
||||||
super(TarBzipArchive, self).__init__(src, dest, file_args, module)
|
super(TarBzipArchive, self).__init__(src, dest, file_args, module)
|
||||||
self.zipflag = '-j'
|
self.zipflag = '-j'
|
||||||
|
|
||||||
|
|
||||||
# class to handle xz compressed tar files
|
# Class to handle xz compressed tar files
|
||||||
class TarXzArchive(TgzArchive):
|
class TarXzArchive(TgzArchive):
|
||||||
def __init__(self, src, dest, file_args, module):
|
def __init__(self, src, dest, file_args, module):
|
||||||
super(TarXzArchive, self).__init__(src, dest, file_args, module)
|
super(TarXzArchive, self).__init__(src, dest, file_args, module)
|
||||||
|
@ -767,16 +761,16 @@ def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
# not checking because of daisy chain to file module
|
# not checking because of daisy chain to file module
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
src = dict(required=True, type='path'),
|
src=dict(type='path', required=True),
|
||||||
original_basename = dict(required=False, type='str'), # used to handle 'dest is a directory' via template, a slight hack
|
original_basename=dict(type='str'), # used to handle 'dest is a directory' via template, a slight hack
|
||||||
dest = dict(required=True, type='path'),
|
dest=dict(type='path', required=True),
|
||||||
remote_src = dict(required=False, default=False, type='bool'),
|
remote_src=dict(type='bool', default=False),
|
||||||
creates = dict(required=False, type='path'),
|
creates=dict(type='path'),
|
||||||
list_files = dict(required=False, default=False, type='bool'),
|
list_files=dict(type='bool', default=False),
|
||||||
keep_newer = dict(required=False, default=False, type='bool'),
|
keep_newer=dict(type='bool', default=False),
|
||||||
exclude = dict(required=False, default=[], type='list'),
|
exclude=dict(type='list', default=[]),
|
||||||
extra_opts = dict(required=False, default=[], type='list'),
|
extra_opts=dict(type='list', default=[]),
|
||||||
validate_certs = dict(required=False, default=True, type='bool'),
|
validate_certs=dict(type='bool', default=True),
|
||||||
),
|
),
|
||||||
add_file_common_args=True,
|
add_file_common_args=True,
|
||||||
# check-mode only works for zip files, we cover that later
|
# check-mode only works for zip files, we cover that later
|
||||||
|
@ -787,6 +781,7 @@ def main():
|
||||||
dest = module.params['dest']
|
dest = module.params['dest']
|
||||||
remote_src = module.params['remote_src']
|
remote_src = module.params['remote_src']
|
||||||
file_args = module.load_file_common_arguments(module.params)
|
file_args = module.load_file_common_arguments(module.params)
|
||||||
|
|
||||||
# did tar file arrive?
|
# did tar file arrive?
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
if not remote_src:
|
if not remote_src:
|
||||||
|
@ -879,10 +874,6 @@ def main():
|
||||||
|
|
||||||
module.exit_json(**res_args)
|
module.exit_json(**res_args)
|
||||||
|
|
||||||
# import module snippets
|
|
||||||
from ansible.module_utils.basic import *
|
|
||||||
from ansible.module_utils.urls import *
|
|
||||||
from ansible.module_utils._text import to_native
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -255,7 +255,6 @@ lib/ansible/modules/files/iso_extract.py
|
||||||
lib/ansible/modules/files/replace.py
|
lib/ansible/modules/files/replace.py
|
||||||
lib/ansible/modules/files/synchronize.py
|
lib/ansible/modules/files/synchronize.py
|
||||||
lib/ansible/modules/files/tempfile.py
|
lib/ansible/modules/files/tempfile.py
|
||||||
lib/ansible/modules/files/unarchive.py
|
|
||||||
lib/ansible/modules/files/xattr.py
|
lib/ansible/modules/files/xattr.py
|
||||||
lib/ansible/modules/identity/opendj/opendj_backendprop.py
|
lib/ansible/modules/identity/opendj/opendj_backendprop.py
|
||||||
lib/ansible/modules/messaging/rabbitmq_binding.py
|
lib/ansible/modules/messaging/rabbitmq_binding.py
|
||||||
|
|
Loading…
Reference in a new issue