-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: main
Are you sure you want to change the base?
Add decompress module #9175
Conversation
adds integration test for gz decompressing
…the module returns error
…tests related to the module core functionality run as parametrized, tests for idempotency and check mode run only for one format)
…rom the src filename
This comment was marked as outdated.
This comment was marked as outdated.
…Adds copyright header to test files
There was a problem hiding this 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
|
||
def configure(self): | ||
if not os.path.exists(self.b_src): | ||
self.module.fail_json(msg="Path does not exist: '%s'" % self.b_src) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Co-authored-by: Felix Fontein <[email protected]>
…existing dest and remove=true; fixes the issue and adds test)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plugins/modules/decompress.py
Outdated
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 |
There was a problem hiding this comment.
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.
plugins/modules/decompress.py
Outdated
shutil.copyfileobj(src_file, dest_file) | ||
|
||
|
||
class Decompress(object): |
There was a problem hiding this comment.
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 ModuleHelper
. See more in https://docs.ansible.com/ansible/latest/collections/community/general/docsite/guide_modulehelper.html
plugins/modules/decompress.py
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
plugins/modules/decompress.py
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
SUMMARY
Adds
decompress
module (see feature request at #1042)Fixes #1042
ISSUE TYPE
COMPONENT NAME
decompress
ADDITIONAL INFORMATION