spacecmd/src/lib/utils.py | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
New commits:
commit 48248b7818e8b09bf25c6407c1d285baa1cca857
Author: Milan Zazrivec <mzazrivec(a)redhat.com>
Date: Fri Dec 20 17:28:21 2013 +0100
1014765 - fix binary file detection
diff --git a/spacecmd/src/lib/utils.py b/spacecmd/src/lib/utils.py
index 923f7f2..5bd341c 100644
--- a/spacecmd/src/lib/utils.py
+++ b/spacecmd/src/lib/utils.py
@@ -28,6 +28,7 @@ from difflib import unified_diff
from tempfile import mkstemp
from textwrap import wrap
import rpm
+import string
from spacecmd.optionparser import SpacecmdOptionParser
try:
@@ -725,19 +726,19 @@ def diff( source_data, target_data, source_channel, target_channel ):
return unified_diff( source_data, target_data, source_channel, target_channel )
def file_needs_b64_enc(self, contents):
+ """Used to check if files (config files primarily) need base64 encoding
+ in order to work properly via the API"""
- # Used to check if files (config files primarily) need base64 encoding
- # in order to work properly via the API
-
- # Files with trailing newlines, which the API strips from files
- # uploaded as text, to avoid this we upload them as base64 encoded
- if contents != contents.rstrip():
- logging.info("trailing newlines detected, uploading as binary")
+ if "\0" in contents:
return True
- # TODO : Add other exceptions here, e.g those containing characters which
- # are valid ascii but not valid XML (e.g the escape character)
+ if not contents: # zero length
+ return False
+
+ text_characters = "".join(map(chr, range(32, 127)) + list("\n\r\t\b"))
+ _null_trans = string.maketrans("", "")
- return False
+ # More than 30% non-text characters -> considered a binary file
+ return float(len(contents.translate(_null_trans, text_characters))) / len(contents) > 0.3
# vim:ts=4:expandtab: