Move a path being passed around as a byte string to being passed around as a text string. (#17190)

This is enough to get minimal copy module working on python3

We have t omodify dataloader's path_dwim_relative_stack and everything
that calls it to use text paths instead of byte string paths
This commit is contained in:
Toshio Kuratomi 2016-08-22 21:55:30 -07:00 committed by GitHub
parent 20bde8f549
commit 313d4b2c9e
12 changed files with 94 additions and 58 deletions

View file

@ -37,7 +37,7 @@ from ansible.errors import AnsibleError, AnsibleConnectionFailure
from ansible.executor.module_common import modify_module
from ansible.release import __version__
from ansible.parsing.utils.jsonify import jsonify
from ansible.utils.unicode import to_bytes, to_unicode
from ansible.utils.unicode import to_bytes, to_str, to_unicode
try:
from __main__ import display
@ -844,7 +844,7 @@ class ActionBase(with_metaclass(ABCMeta, object)):
result = self._loader.path_dwim_relative_stack(path_stack, dirname, needle)
if result is None:
raise AnsibleError("Unable to find '%s' in expected paths." % needle)
raise AnsibleError("Unable to find '%s' in expected paths." % to_str(needle))
return result

View file

@ -27,7 +27,7 @@ from ansible.errors import AnsibleError
from ansible.plugins.action import ActionBase
from ansible.utils.boolean import boolean
from ansible.utils.hashing import checksum_s
from ansible.utils.unicode import to_str
from ansible.utils.unicode import to_str, to_unicode
class ActionModule(ActionBase):
@ -42,10 +42,10 @@ class ActionModule(ActionBase):
delimit_me = False
add_newline = False
for f in sorted(os.listdir(src_path)):
for f in (to_unicode(p, errors='strict') for p in sorted(os.listdir(src_path))):
if compiled_regexp and not compiled_regexp.search(f):
continue
fragment = "%s/%s" % (src_path, f)
fragment = u"%s/%s" % (src_path, f)
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
continue
@ -119,7 +119,7 @@ class ActionModule(ActionBase):
if not os.path.isdir(src):
result['failed'] = True
result['msg'] = "Source (%s) is not a directory" % src
result['msg'] = u"Source (%s) is not a directory" % src
return result
_re = None

View file

@ -18,10 +18,12 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
import pipes
import random
from ansible import constants as C
from ansible.plugins.action import ActionBase
from ansible.compat.six import iteritems
from ansible.utils.unicode import to_unicode
class ActionModule(ActionBase):

View file

@ -27,7 +27,7 @@ from ansible.errors import AnsibleError
from ansible.plugins.action import ActionBase
from ansible.utils.boolean import boolean
from ansible.utils.hashing import checksum
from ansible.utils.unicode import to_bytes, to_str
from ansible.utils.unicode import to_bytes, to_str, to_unicode
class ActionModule(ActionBase):
@ -96,7 +96,7 @@ class ActionModule(ActionBase):
source = self._find_needle('files', source)
except AnsibleError as e:
result['failed'] = True
result['msg'] = to_str(e)
result['msg'] = to_unicode(e)
return result
# A list of source file tuples (full_path, relative_path) which will try to copy to the destination
@ -111,7 +111,7 @@ class ActionModule(ActionBase):
sz = len(source.rsplit('/', 1)[0]) + 1
# Walk the directory and append the file tuples to source_files.
for base_path, sub_folders, files in os.walk(source):
for base_path, sub_folders, files in os.walk(to_bytes(source)):
for file in files:
full_path = os.path.join(base_path, file)
rel_path = full_path[sz:]

View file

@ -93,19 +93,20 @@ class ActionModule(ActionBase):
dest = os.path.join(dest, base)
# template the source data locally & get ready to transfer
b_source = to_bytes(source)
try:
with open(source, 'r') as f:
with open(b_source, 'r') as f:
template_data = to_unicode(f.read())
try:
template_uid = pwd.getpwuid(os.stat(source).st_uid).pw_name
template_uid = pwd.getpwuid(os.stat(b_source).st_uid).pw_name
except:
template_uid = os.stat(source).st_uid
template_uid = os.stat(b_source).st_uid
temp_vars = task_vars.copy()
temp_vars['template_host'] = os.uname()[1]
temp_vars['template_path'] = source
temp_vars['template_mtime'] = datetime.datetime.fromtimestamp(os.path.getmtime(source))
temp_vars['template_mtime'] = datetime.datetime.fromtimestamp(os.path.getmtime(b_source))
temp_vars['template_uid'] = template_uid
temp_vars['template_fullpath'] = os.path.abspath(source)
temp_vars['template_run_date'] = datetime.datetime.now()
@ -118,7 +119,7 @@ class ActionModule(ActionBase):
)
temp_vars['ansible_managed'] = time.strftime(
managed_str,
time.localtime(os.path.getmtime(source))
time.localtime(os.path.getmtime(b_source))
)
# Create a new searchpath list to assign to the templar environment's file