patch: PEP8, imports, cosmetics (#24649)

- Make PEP8 compliant
- Ensure imports are specific
- Few cosmetic changes (sort lists, casing, punctuation)
pull/4420/head
Dag Wieers 2017-06-02 09:42:40 +02:00 committed by John R Barker
parent 85219dfdf3
commit 91ee93ce13
2 changed files with 42 additions and 47 deletions

View File

@ -23,78 +23,73 @@ ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['stableinterface'], 'status': ['stableinterface'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = r'''
DOCUMENTATION = '''
--- ---
module: patch module: patch
author: author:
- "Jakub Jirutka (@jirutka)" - Jakub Jirutka (@jirutka)
- "Luis Alberto Perez Lazaro (@luisperlaz)" - Luis Alberto Perez Lazaro (@luisperlaz)
version_added: 1.9 version_added: '1.9'
description: description:
- Apply patch files using the GNU patch tool. - Apply patch files using the GNU patch tool.
short_description: Apply patch files using the GNU patch tool. short_description: Apply patch files using the GNU patch tool
options: options:
basedir: basedir:
description: description:
- Path of a base directory in which the patch file will be applied. - Path of a base directory in which the patch file will be applied.
May be omitted when C(dest) option is specified, otherwise required. May be omitted when C(dest) option is specified, otherwise required.
required: false
dest: dest:
description: description:
- Path of the file on the remote machine to be patched. - Path of the file on the remote machine to be patched.
- The names of the files to be patched are usually taken from the patch - The names of the files to be patched are usually taken from the patch
file, but if there's just one file to be patched it can specified with file, but if there's just one file to be patched it can specified with
this option. this option.
required: false aliases: [ originalfile ]
aliases: [ "originalfile" ]
src: src:
description: description:
- Path of the patch file as accepted by the GNU patch tool. If - Path of the patch file as accepted by the GNU patch tool. If
C(remote_src) is 'no', the patch source file is looked up from the C(remote_src) is 'no', the patch source file is looked up from the
module's "files" directory. module's I(files) directory.
required: true required: true
aliases: [ "patchfile" ] aliases: [ patchfile ]
remote_src: remote_src:
description: description:
- If C(no), it will search for src at originating/master machine, if C(yes) it will - If C(no), it will search for src at originating/master machine, if C(yes) it will
go to the remote/target machine for the src. Default is C(no). go to the remote/target machine for the C(src).
choices: [ "yes", "no" ] choices: [ 'no', 'yes' ]
required: false default: 'no'
default: "no"
strip: strip:
description: description:
- Number that indicates the smallest prefix containing leading slashes - Number that indicates the smallest prefix containing leading slashes
that will be stripped from each file name found in the patch file. that will be stripped from each file name found in the patch file.
For more information see the strip parameter of the GNU patch tool. For more information see the strip parameter of the GNU patch tool.
required: false default: 0
default: "0"
backup: backup:
version_added: "2.0" version_added: "2.0"
description: description:
- passes --backup --version-control=numbered to patch, - Passes C(--backup --version-control=numbered) to patch,
producing numbered backup copies producing numbered backup copies.
choices: [ 'yes', 'no' ] choices: [ 'no', 'yes' ]
default: 'no' default: 'no'
binary: binary:
version_added: "2.0" version_added: "2.0"
description: description:
- Setting to C(yes) will disable patch's heuristic for transforming CRLF - Setting to C(yes) will disable patch's heuristic for transforming CRLF
line endings into LF. Line endings of src and dest must match. If set to line endings into LF. Line endings of src and dest must match. If set to
C(no), patch will replace CRLF in src files on POSIX. C(no), C(patch) will replace CRLF in C(src) files on POSIX.
required: false choices: [ 'no', 'yes' ]
default: "no" default: 'no'
notes: notes:
- This module requires GNU I(patch) utility to be installed on the remote host. - This module requires GNU I(patch) utility to be installed on the remote host.
''' '''
EXAMPLES = ''' EXAMPLES = r'''
- name: apply patch to one file - name: Apply patch to one file
patch: patch:
src: /tmp/index.html.patch src: /tmp/index.html.patch
dest: /var/www/index.html dest: /var/www/index.html
- name: apply patch to multiple files under basedir - name: Apply patch to multiple files under basedir
patch: patch:
src: /tmp/customize.patch src: /tmp/customize.patch
basedir: /var/www basedir: /var/www
@ -102,7 +97,8 @@ EXAMPLES = '''
''' '''
import os import os
from os import path, R_OK, W_OK
from ansible.module_utils.basic import AnsibleModule, get_exception
class PatchError(Exception): class PatchError(Exception):
@ -143,41 +139,43 @@ def apply_patch(patch_func, patch_file, basedir, dest_file=None, binary=False, s
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec={ argument_spec=dict(
'src': {'required': True, 'aliases': ['patchfile']}, src=dict(type='path', required=True, aliases=['patchfile']),
'dest': {'aliases': ['originalfile']}, dest=dict(type='path', aliases=['originalfile']),
'basedir': {}, basedir=dict(type='path'),
'strip': {'default': 0, 'type': 'int'}, strip=dict(type='int', default=0),
'remote_src': {'default': False, 'type': 'bool'}, remote_src=dict(type='bool', default=False),
# NB: for 'backup' parameter, semantics is slightly different from standard # NB: for 'backup' parameter, semantics is slightly different from standard
# since patch will create numbered copies, not strftime("%Y-%m-%d@%H:%M:%S~") # since patch will create numbered copies, not strftime("%Y-%m-%d@%H:%M:%S~")
'backup': {'default': False, 'type': 'bool'}, backup=dict(type='bool', default=False),
'binary': {'default': False, 'type': 'bool'}, binary=dict(type='bool', default=False),
}, ),
required_one_of=[['dest', 'basedir']], required_one_of=[['dest', 'basedir']],
supports_check_mode=True supports_check_mode=True,
) )
# Create type object as namespace for module params # Create type object as namespace for module params
p = type('Params', (), module.params) p = type('Params', (), module.params)
p.src = os.path.expanduser(p.src) p.src = os.path.expanduser(p.src)
if not os.access(p.src, R_OK): if not os.access(p.src, os.R_OK):
module.fail_json(msg="src %s doesn't exist or not readable" % (p.src)) module.fail_json(msg="src %s doesn't exist or not readable" % (p.src))
if p.dest and not os.access(p.dest, W_OK): if p.dest and not os.access(p.dest, os.W_OK):
module.fail_json(msg="dest %s doesn't exist or not writable" % (p.dest)) module.fail_json(msg="dest %s doesn't exist or not writable" % (p.dest))
if p.basedir and not path.exists(p.basedir): if p.basedir and not os.path.exists(p.basedir):
module.fail_json(msg="basedir %s doesn't exist" % (p.basedir)) module.fail_json(msg="basedir %s doesn't exist" % (p.basedir))
if not p.basedir: if not p.basedir:
p.basedir = path.dirname(p.dest) p.basedir = os.path.dirname(p.dest)
patch_bin = module.get_bin_path('patch') patch_bin = module.get_bin_path('patch')
if patch_bin is None: if patch_bin is None:
module.fail_json(msg="patch command not found") module.fail_json(msg="patch command not found")
patch_func = lambda opts: module.run_command("%s %s" % (patch_bin, ' '.join(opts)))
def patch_func(opts):
return module.run_command('%s %s' % (patch_bin, ' '.join(opts)))
# patch need an absolute file name # patch need an absolute file name
p.src = os.path.abspath(p.src) p.src = os.path.abspath(p.src)
@ -185,8 +183,8 @@ def main():
changed = False changed = False
if not is_already_applied(patch_func, p.src, p.basedir, dest_file=p.dest, binary=p.binary, strip=p.strip): if not is_already_applied(patch_func, p.src, p.basedir, dest_file=p.dest, binary=p.binary, strip=p.strip):
try: try:
apply_patch( patch_func, p.src, p.basedir, dest_file=p.dest, binary=p.binary, strip=p.strip, apply_patch(patch_func, p.src, p.basedir, dest_file=p.dest, binary=p.binary, strip=p.strip,
dry_run=module.check_mode, backup=p.backup ) dry_run=module.check_mode, backup=p.backup)
changed = True changed = True
except PatchError: except PatchError:
e = get_exception() e = get_exception()
@ -194,8 +192,6 @@ def main():
module.exit_json(changed=changed) module.exit_json(changed=changed)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -309,7 +309,6 @@ lib/ansible/modules/files/copy.py
lib/ansible/modules/files/find.py lib/ansible/modules/files/find.py
lib/ansible/modules/files/ini_file.py lib/ansible/modules/files/ini_file.py
lib/ansible/modules/files/iso_extract.py lib/ansible/modules/files/iso_extract.py
lib/ansible/modules/files/patch.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