Move uses of to_bytes, to_text, to_native to use the module_utils version (#17423)

We couldn't copy to_unicode, to_bytes, to_str into module_utils because
of licensing.  So once created it we had two sets of functions that did
the same things but had different implementations.  To remedy that, this
change removes the ansible.utils.unicode versions of those functions.
This commit is contained in:
Toshio Kuratomi 2016-09-06 22:54:17 -07:00 committed by GitHub
parent 7a9395b5e0
commit 4ed88512e4
89 changed files with 759 additions and 894 deletions

View file

@ -20,11 +20,12 @@ __metaclass__ = type
import os
from errno import EEXIST
from ansible.errors import AnsibleError
from ansible.utils.unicode import to_bytes, to_str, to_unicode
from ansible.compat.six import PY2
from ansible.module_utils._text import to_bytes, to_native, to_text
__all__ = ['unfrackpath', 'makedirs_safe']
def unfrackpath(path):
'''
Returns a path that is free of symlinks, environment
@ -40,10 +41,9 @@ def unfrackpath(path):
example::
'$HOME/../../var/mail' becomes '/var/spool/mail'
'''
canonical_path = os.path.normpath(os.path.realpath(os.path.expanduser(os.path.expandvars(to_bytes(path, errors='strict')))))
if PY2:
return to_unicode(canonical_path, errors='strict')
return to_unicode(canonical_path, errors='surrogateescape')
canonical_path = os.path.normpath(os.path.realpath(os.path.expanduser(os.path.expandvars(to_bytes(path, errors='surrogate_or_strict')))))
return to_text(canonical_path, errors='surrogate_or_strict')
def makedirs_safe(path, mode=None):
'''Safe way to create dirs in muliprocess/thread environments.
@ -64,4 +64,4 @@ def makedirs_safe(path, mode=None):
os.makedirs(b_rpath)
except OSError as e:
if e.errno != EEXIST:
raise AnsibleError("Unable to create local directories(%s): %s" % (to_str(rpath), to_str(e)))
raise AnsibleError("Unable to create local directories(%s): %s" % (to_native(rpath), to_native(e)))