Extend salt.utils.istextfile() with utf-8 instead of binary detection

This commit is contained in:
Dmitry Kurbatov 2015-07-08 05:11:23 +03:00
parent 2b02315d73
commit 2a349ff102
2 changed files with 7 additions and 1 deletions

View File

@ -1107,6 +1107,11 @@ def istextfile(fp_, blocksize=512):
elif not block: elif not block:
# An empty file is considered a valid text file # An empty file is considered a valid text file
return True return True
try:
block.decode('utf-8')
return True
except UnicodeDecodeError:
pass
nontext = block.translate(None, text_characters) nontext = block.translate(None, text_characters)
return float(len(nontext)) / len(block) <= 0.30 return float(len(nontext)) / len(block) <= 0.30

View File

@ -1555,7 +1555,8 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
'name': '{0}'.format(test_file), 'name': '{0}'.format(test_file),
'__run_num__': 1, '__run_num__': 1,
'comment': 'File {0} updated'.format(test_file), 'comment': 'File {0} updated'.format(test_file),
'diff': 'Replace binary file with text file' 'diff': '--- \n+++ \n@@ -1 +1,3\n+{0}\n {1}\n+{2}\n'.format(
korean_utf8_2, korean_utf8_1, korean_utf8_3),
}, },
('file_|-some-utf8-file-exists_|-{0}' ('file_|-some-utf8-file-exists_|-{0}'
'_|-exists').format(test_file): { '_|-exists').format(test_file): {