Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the project archive sync support for py2 compatability #8057

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions awx/playbooks/action_plugins/project_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

__metaclass__ = type

import zipfile
import tarfile
import errno
import os
import tarfile
import zipfile

from ansible.plugins.action import ActionBase
from ansible.utils.display import Display

display = Display()

try:
from zipfile import BadZipFile
except ImportError:
from zipfile import BadZipfile as BadZipFile # py2 compat
Copy link
Contributor Author

@ryanpetrello ryanpetrello Sep 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Booooooooooooo python2



class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
Expand All @@ -26,14 +32,15 @@ def run(self, tmp=None, task_vars=None):
archive = zipfile.ZipFile(src)
get_filenames = archive.namelist
get_members = archive.infolist
except zipfile.BadZipFile:
archive = tarfile.open(src)
except BadZipFile:
try:
archive = tarfile.open(src)
except tarfile.ReadError:
Copy link
Contributor Author

@ryanpetrello ryanpetrello Sep 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lack of indentation was actually a legitimate bug we just missed in code review.

result["failed"] = True
result["msg"] = "{0} is not a valid archive".format(src)
return result
get_filenames = archive.getnames
get_members = archive.getmembers
except tarfile.ReadError:
result["failed"] = True
result["msg"] = "{0} is not a valid archive".format(src)
return result

# Most well formed archives contain a single root directory, typically named
# project-name-1.0.0. The project contents should be inside that directory.
Expand Down Expand Up @@ -62,10 +69,19 @@ def run(self, tmp=None, task_vars=None):
try:
is_dir = member.is_dir()
except AttributeError:
is_dir = member.isdir()
try:
is_dir = member.isdir()
except AttributeError:
is_dir = member.filename[-1] == '/' # py2 compat for ZipInfo
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python2's zipfile doesn't have this, but this is what py3 does, so I just copied it here


if is_dir:
os.makedirs(dest, exist_ok=True)
try:
os.makedirs(dest)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poor man's exist_ok=True (which also doesn't exist in py2)

except OSError as exc: # Python >= 2.5
if exc.errno == errno.EEXIST and os.path.isdir(dest):
pass
else:
raise
else:
try:
member_f = archive.open(member)
Expand Down