Skip to content

Commit

Permalink
Fix missing import and boilerplate
Browse files Browse the repository at this point in the history
Added fix for missing imports and boilerplate in files modules,
also, removed get_exception calls to match 2.6> exception handling.

Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde authored and abadger committed Aug 3, 2017
1 parent 11da85f commit bf54a0c
Show file tree
Hide file tree
Showing 19 changed files with 168 additions and 383 deletions.
19 changes: 5 additions & 14 deletions lib/ansible/modules/files/acl.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Copyright: (c) 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['stableinterface'],
Expand Down
54 changes: 20 additions & 34 deletions lib/ansible/modules/files/archive.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2016, Ben Doherty <[email protected]>
# Copyright: (c) 2016, Ben Doherty <[email protected]>
# Sponsored by Oomph, Inc. http://www.oomphinc.com
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Copyright: (c) 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
Expand Down Expand Up @@ -149,8 +139,9 @@
import filecmp
import zipfile
import tarfile
from traceback import format_exc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils._text import to_native


def main():
Expand Down Expand Up @@ -318,9 +309,8 @@ def main():
else:
arcfile.add(fullpath, arcname, recursive=False)

except Exception:
e = get_exception()
errors.append('%s: %s' % (fullpath, str(e)))
except Exception as e:
errors.append('%s: %s' % (fullpath, to_native(e)))

for filename in filenames:
fullpath = dirpath + filename
Expand All @@ -334,9 +324,8 @@ def main():
arcfile.add(fullpath, arcname, recursive=False)

successes.append(fullpath)
except Exception:
e = get_exception()
errors.append('Adding %s: %s' % (path, str(e)))
except Exception as e:
errors.append('Adding %s: %s' % (path, to_native(e)))
else:
if format == 'zip':
arcfile.write(path, match_root.sub('', path))
Expand All @@ -345,9 +334,9 @@ def main():

successes.append(path)

except Exception:
e = get_exception()
return module.fail_json(msg='Error when writing %s archive at %s: %s' % (format == 'zip' and 'zip' or ('tar.' + format), dest, str(e)))
except Exception as e:
module.fail_json(msg='Error when writing %s archive at %s: %s' % (format == 'zip' and 'zip' or ('tar.' + format), dest, to_native(e)),
exception=format_exc())

if arcfile:
arcfile.close()
Expand All @@ -363,8 +352,7 @@ def main():
shutil.rmtree(path)
elif not check_mode:
os.remove(path)
except OSError:
e = get_exception()
except OSError as e:
errors.append(path)

if len(errors) > 0:
Expand Down Expand Up @@ -421,9 +409,8 @@ def main():

successes.append(path)

except OSError:
e = get_exception()
module.fail_json(path=path, dest=dest, msg='Unable to write to compressed file: %s' % str(e))
except OSError as e:
module.fail_json(path=path, dest=dest, msg='Unable to write to compressed file: %s' % to_native(e), exception=format_exc())

if arcfile:
arcfile.close()
Expand All @@ -442,9 +429,8 @@ def main():
try:
os.remove(path)

except OSError:
e = get_exception()
module.fail_json(path=path, msg='Unable to remove source file: %s' % str(e))
except OSError as e:
module.fail_json(path=path, msg='Unable to remove source file: %s' % to_native(e), exception=format_exc())

params['path'] = dest
file_args = module.load_file_common_arguments(params)
Expand Down
36 changes: 12 additions & 24 deletions lib/ansible/modules/files/assemble.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2012, Stephen Fromm <[email protected]>
# (c) 2016, Toshio Kuratomi <[email protected]>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Copyright: (c) 2012, Stephen Fromm <[email protected]>
# Copyright: (c) 2016, Toshio Kuratomi <[email protected]>
# Copyright: (c) 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['stableinterface'],
Expand Down Expand Up @@ -125,8 +115,8 @@
import tempfile

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.six import b
from ansible.module_utils._text import to_native


# ===========================================
Expand Down Expand Up @@ -178,11 +168,10 @@ def cleanup(path, result=None):
if os.path.exists(path):
try:
os.remove(path)
except (IOError, OSError):
e = get_exception()
except (IOError, OSError) as e:
# don't error on possible race conditions, but keep warning
if result is not None:
result['warnings'] = ['Unable to remove temp file (%s): %s' % (path, str(e))]
result['warnings'] = ['Unable to remove temp file (%s): %s' % (path, to_native(e))]


def main():
Expand Down Expand Up @@ -224,9 +213,8 @@ def main():
if regexp is not None:
try:
compiled_regexp = re.compile(regexp)
except re.error:
e = get_exception()
module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (e, regexp))
except re.error as e:
module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (to_native(e), regexp))

if validate and "%s" not in validate:
module.fail_json(msg="validate must contain %%s: %s" % validate)
Expand Down
23 changes: 7 additions & 16 deletions lib/ansible/modules/files/blockinfile.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2014, 2015 YAEGASHI Takeshi <[email protected]>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Copyright: (c) 2014, 2015 YAEGASHI Takeshi <[email protected]>
# Copyright: (c) 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
Expand Down Expand Up @@ -167,6 +157,7 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_bytes


def write_changes(module, contents, path):

tmpfd, tmpfile = tempfile.mkstemp()
Expand Down
26 changes: 7 additions & 19 deletions lib/ansible/modules/files/copy.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2012, Michael DeHaan <[email protected]>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Copyright: (c) 2012, Michael DeHaan <[email protected]>
# Copyright: (c) 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['stableinterface'],
Expand Down Expand Up @@ -238,7 +228,6 @@

# import module snippets
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils._text import to_bytes, to_native


Expand Down Expand Up @@ -359,8 +348,7 @@ def main():
# the execute bit for the current user set, in
# which case the stat() call will raise an OSError
os.stat(os.path.dirname(b_dest))
except OSError:
e = get_exception()
except OSError as e:
if "permission denied" in to_native(e).lower():
module.fail_json(msg="Destination directory %s is not accessible" % (os.path.dirname(dest)))
module.fail_json(msg="Destination directory %s does not exist" % (os.path.dirname(dest)))
Expand Down
17 changes: 2 additions & 15 deletions lib/ansible/modules/files/fetch.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
# this is a virtual module that is entirely implemented server side

# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Copyright: (c) 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['stableinterface'],
Expand Down
Loading

0 comments on commit bf54a0c

Please sign in to comment.