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

Add decompress module #9175

Open
wants to merge 39 commits into
base: main
Choose a base branch
from

Conversation

shamilovstas
Copy link
Contributor

SUMMARY

Adds decompress module (see feature request at #1042)

Fixes #1042

ISSUE TYPE
  • New Module/Plugin Pull Request
COMPONENT NAME

decompress

ADDITIONAL INFORMATION

@ansibullbot ansibullbot added integration tests/integration module module plugins plugin (any type) tests tests labels Nov 22, 2024
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Nov 22, 2024
@ansibullbot ansibullbot removed the ci_verified Push fixes to PR branch to re-run CI label Nov 22, 2024
@felixfontein felixfontein added the check-before-release PR will be looked at again shortly before release and merged if possible. label Nov 22, 2024
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! I've added some first comments.

plugins/modules/decompress.py Outdated Show resolved Hide resolved
plugins/modules/decompress.py Outdated Show resolved Hide resolved

def configure(self):
if not os.path.exists(self.b_src):
self.module.fail_json(msg="Path does not exist: '%s'" % self.b_src)
Copy link
Collaborator

Choose a reason for hiding this comment

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

In case remove=true and the destination path exists, but the source path does not exist, the module should not fail, but simply exit with changed=false. Otherwise the module isn't idempotent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

plugins/modules/decompress.py Outdated Show resolved Hide resolved
else:
self.dest = dest
self.b_dest = to_native(self.dest, errors='surrogate_or_strict')
self.b_src = to_native(self.src, errors='surrogate_or_strict')
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would expect that b_ indicates bytes, and not native. Why not use byte strings? That's usually the better choice for filesystem paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

plugins/modules/decompress.py Outdated Show resolved Hide resolved
plugins/modules/decompress.py Show resolved Hide resolved
tests/integration/targets/decompress/files/file.txt Outdated Show resolved Hide resolved
shamilovstas and others added 2 commits November 23, 2024 21:19
…existing dest and remove=true; fixes the issue and adds test)
Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

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

Hi @shamilovstas

Thanks for contributing!

Got a few comments, but overall it looks good.

Comment on lines 105 to 121
LZMA_IMP_ERR = None
if six.PY3:
try:
import lzma

HAS_LZMA = True
except ImportError:
LZMA_IMP_ERR = format_exc()
HAS_LZMA = False
else:
try:
from backports import lzma

HAS_LZMA = True
except ImportError:
LZMA_IMP_ERR = format_exc()
HAS_LZMA = False
Copy link
Collaborator

Choose a reason for hiding this comment

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

You might want to consider using module_utils.deps, see https://docs.ansible.com/ansible/latest/collections/community/general/docsite/guide_deps.html for guidance on how to use it.

shutil.copyfileobj(src_file, dest_file)


class Decompress(object):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Comment on lines 174 to 176
self.fmt = self.module.params['format']
if self.fmt not in self.handlers:
self.module.fail_json(msg="Could not decompress '%s' format" % self.fmt)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The format is controlled by the module parameter, so it is always supposed to have a handler. This code is redundant.

@ansibullbot ansibullbot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Nov 25, 2024
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Nov 26, 2024
Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

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

LGTM

if not os.path.exists(b_src):
msg = "Path does not exist: '%s'" % b_src
if self.vars.remove and os.path.exists(b_dest):
self.warn(msg)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why warn in this case? If you really think a warning would be useful in some cases, this should be made configurable, with the default not having a warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea was that it'd be helpful to let the user know that the file they are trying to decompress does not exist. IMO having a src non-existent is not something that should happen regularly. Initially I made it so that in case of non-existing src the module would fail, but changed it after you commenting that it'd break idempodency. Since now the module finished successfully in this case, I thought that at least the user would be warned about this situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not something I'm going to insist on, so I'll just delete the warn

Copy link
Collaborator

Choose a reason for hiding this comment

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

A warning is something that should only be used for very important events. For this case a (new) return value is better suited that can be queried by the user if they're interested in this case.

tests/integration/targets/decompress/tasks/core.yml Outdated Show resolved Hide resolved
tests/integration/targets/decompress/tasks/main.yml Outdated Show resolved Hide resolved
tests/integration/targets/decompress/tasks/main.yml Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-10 Automatically create a backport for the stable-10 branch check-before-release PR will be looked at again shortly before release and merged if possible. has_issue integration tests/integration module module plugins plugin (any type) tests tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add uncompress module
4 participants