Commit 6c601a08 authored by Jérome Perrin's avatar Jérome Perrin

prune: fix some UnicodeDecodeError on python3 with binary scripts

Some softwares have some binary executables in builout bin folder,
reading them as text can fail. In this case we can ignore them.
parent affcc2d9
...@@ -217,7 +217,10 @@ def getUsageSignatureFromSoftwareAndSharedPart( ...@@ -217,7 +217,10 @@ def getUsageSignatureFromSoftwareAndSharedPart(
signatures[installed_cfg] = f.read() signatures[installed_cfg] = f.read()
for script in glob.glob(os.path.join(software_root, '*', 'bin', '*')): for script in glob.glob(os.path.join(software_root, '*', 'bin', '*')):
with open(script) as f: with open(script) as f:
try:
signatures[script] = f.read() signatures[script] = f.read()
except UnicodeDecodeError:
logger.debug("Skipping script %s that could not be decoded", script)
if shared_root: if shared_root:
for shared_signature in glob.glob(os.path.join(shared_root, '*', '*', for shared_signature in glob.glob(os.path.join(shared_root, '*', '*',
'.*signature')): '.*signature')):
......
...@@ -31,6 +31,7 @@ import os ...@@ -31,6 +31,7 @@ import os
import shutil import shutil
import unittest import unittest
import slapos.client import slapos.client
import six
try: try:
import mock import mock
...@@ -139,11 +140,19 @@ class TestPrune(unittest.TestCase): ...@@ -139,11 +140,19 @@ class TestPrune(unittest.TestCase):
script = os.path.join(fake_software_path, 'bin', 'buildout') script = os.path.join(fake_software_path, 'bin', 'buildout')
with open(script, 'w') as f: with open(script, 'w') as f:
f.write('#!{}'.format(used_in_script)) f.write('#!{}'.format(used_in_script))
# bin/ can also contain binary executables, this should not cause problems
binary_script = os.path.join(fake_software_path, 'bin', 'binary')
with open(binary_script, 'wb') as f:
f.write(b'\x80')
do_prune(self.logger, self.config, False) do_prune(self.logger, self.config, False)
self.assertTrue(os.path.exists(used_in_script)) self.assertTrue(os.path.exists(used_in_script))
self.assertFalse(os.path.exists(not_used)) self.assertFalse(os.path.exists(not_used))
self.logger.warning.assert_called_with( self.logger.warning.assert_called_with(
'Unusued shared parts at %s%s', not_used, ' ... removed') 'Unusued shared parts at %s%s', not_used, ' ... removed')
if six.PY3:
self.logger.debug.assert_any_call(
'Skipping script %s that could not be decoded', binary_script)
def test_shared_part_used_in_recursive_instance(self): def test_shared_part_used_in_recursive_instance(self):
used_in_software_from_instance = self._createSharedPart('used_in_software_from_instance') used_in_software_from_instance = self._createSharedPart('used_in_software_from_instance')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment