diff --git a/changelogs/fragments/deprecate-shell-checksum.yml b/changelogs/fragments/deprecate-shell-checksum.yml new file mode 100644 index 00000000000..040f23d3bab --- /dev/null +++ b/changelogs/fragments/deprecate-shell-checksum.yml @@ -0,0 +1,2 @@ +deprecated_features: + - "The ``ShellModule.checksum`` method is now deprecated and will be removed in ansible-core 2.23. Use ``ActionBase._execute_remote_stat()`` instead." diff --git a/lib/ansible/plugins/shell/powershell.py b/lib/ansible/plugins/shell/powershell.py index 6ee43cb23d2..6b5d368cea4 100644 --- a/lib/ansible/plugins/shell/powershell.py +++ b/lib/ansible/plugins/shell/powershell.py @@ -24,6 +24,10 @@ import ntpath from ansible.executor.powershell.module_manifest import _bootstrap_powershell_script, _get_powershell_script from ansible.module_utils.common.text.converters import to_bytes, to_text from ansible.plugins.shell import ShellBase, _ShellCommand +from ansible.utils.display import Display + + +display = Display() # This is weird, we are matching on byte sequences that match the utf-16-be # matches for '_x(a-fA-F0-9){4}_'. The \x00 and {4} will match the hex sequence @@ -320,6 +324,10 @@ class ShellModule(ShellBase): return self._encode_script(script) def checksum(self, path, *args, **kwargs): + display.deprecated( + "The 'ShellModule.checksum' method is deprecated. Use 'ActionBase._execute_remote_stat()' instead.", + version="2.23" + ) path = self._escape(self._unquote(path)) script = """ If (Test-Path -PathType Leaf '%(path)s') diff --git a/lib/ansible/plugins/shell/sh.py b/lib/ansible/plugins/shell/sh.py index 5b215d4d897..3724bf6c7e0 100644 --- a/lib/ansible/plugins/shell/sh.py +++ b/lib/ansible/plugins/shell/sh.py @@ -14,6 +14,10 @@ extends_documentation_fragment: """ from ansible.plugins.shell import ShellBase +from ansible.utils.display import Display + + +display = Display() class ShellModule(ShellBase): @@ -43,6 +47,10 @@ class ShellModule(ShellBase): _SHELL_GROUP_RIGHT = ')' def checksum(self, path, python_interp): + display.deprecated( + "The 'ShellModule.checksum' method is deprecated. Use 'ActionBase._execute_remote_stat()' instead.", + version="2.23" + ) # In the following test, each condition is a check and logical # comparison (|| or &&) that sets the rc value. Every check is run so # the last check in the series to fail will be the rc that is returned. @@ -67,11 +75,7 @@ class ShellModule(ShellBase): # "one-liner". shell_escaped_path = self.quote(path) test = "rc=flag; [ -r %(p)s ] %(shell_or)s rc=2; [ -f %(p)s ] %(shell_or)s rc=1; [ -d %(p)s ] %(shell_and)s rc=3; %(i)s -V 2>/dev/null %(shell_or)s rc=4; [ x\"$rc\" != \"xflag\" ] %(shell_and)s echo \"${rc} \"%(p)s %(shell_and)s exit 0" % dict(p=shell_escaped_path, i=python_interp, shell_and=self._SHELL_AND, shell_or=self._SHELL_OR) # NOQA - csums = [ - u"({0} -c 'import hashlib; BLOCKSIZE = 65536; hasher = hashlib.sha1();{2}afile = open(\"'{1}'\", \"rb\"){2}buf = afile.read(BLOCKSIZE){2}while len(buf) > 0:{2}\thasher.update(buf){2}\tbuf = afile.read(BLOCKSIZE){2}afile.close(){2}print(hasher.hexdigest())' 2>/dev/null)".format(python_interp, shell_escaped_path, self._SHELL_EMBEDDED_PY_EOL), # NOQA Python > 2.4 (including python3) - u"({0} -c 'import sha; BLOCKSIZE = 65536; hasher = sha.sha();{2}afile = open(\"'{1}'\", \"rb\"){2}buf = afile.read(BLOCKSIZE){2}while len(buf) > 0:{2}\thasher.update(buf){2}\tbuf = afile.read(BLOCKSIZE){2}afile.close(){2}print(hasher.hexdigest())' 2>/dev/null)".format(python_interp, shell_escaped_path, self._SHELL_EMBEDDED_PY_EOL), # NOQA Python == 2.4 - ] + cmd = "({0} -c 'import hashlib; BLOCKSIZE = 65536; hasher = hashlib.sha1();{2}afile = open(\"'{1}'\", \"rb\"){2}buf = afile.read(BLOCKSIZE){2}while len(buf) > 0:{2}\thasher.update(buf){2}\tbuf = afile.read(BLOCKSIZE){2}afile.close(){2}print(hasher.hexdigest())' 2>/dev/null)".format(python_interp, shell_escaped_path, self._SHELL_EMBEDDED_PY_EOL) # NOQA - cmd = (" %s " % self._SHELL_OR).join(csums) cmd = "%s; %s %s (echo \'0 \'%s)" % (test, cmd, self._SHELL_OR, shell_escaped_path) return cmd diff --git a/lib/ansible/utils/hashing.py b/lib/ansible/utils/hashing.py index 81258f7685c..b10a5fc1494 100644 --- a/lib/ansible/utils/hashing.py +++ b/lib/ansible/utils/hashing.py @@ -59,7 +59,6 @@ def secure_hash(filename, hash_func=sha1): return digest.hexdigest() -# The checksum algorithm must match with the algorithm in ShellModule.checksum() method checksum = secure_hash checksum_s = secure_hash_s