Commit 6f3cb199 authored by Julien Muchembled's avatar Julien Muchembled

erp5testnode: fix clean up of old instance root folder under Python 3

This fixes commit ea4cb55b.

Our 'rmtree' helper has never been supposed to be called on a
non-existing folder: the ENOENT handling is an unrelated
implementation detail for Python 2.

Also drop erp5.util's rmtree and use the one from slapos.core,
which is a [testnode] dependency.
parent 0eeffeea
...@@ -31,11 +31,11 @@ import subprocess ...@@ -31,11 +31,11 @@ import subprocess
import time import time
import xml_marshaller import xml_marshaller
import argparse import argparse
from six.moves import range
from slapos import client from slapos import client
from slapos.util import rmtree
from . import logger from . import logger
from .Utils import createFolder, rmtree from .Utils import createFolder
from six.moves import range
MAX_PARTITIONS = 10 MAX_PARTITIONS = 10
MAX_SR_RETRIES = 3 MAX_SR_RETRIES = 3
...@@ -278,7 +278,8 @@ class SlapOSControler(object): ...@@ -278,7 +278,8 @@ class SlapOSControler(object):
self._resetSoftware() self._resetSoftware()
else: else:
createFolder(self.software_root) createFolder(self.software_root)
rmtree(self.old_instance_root) # BBB if os.path.exists(self.old_instance_root): # BBB
rmtree(self.old_instance_root)
instance_root = self.instance_root instance_root = self.instance_root
# Delete any existing partition in order to not get its data (ex. # Delete any existing partition in order to not get its data (ex.
# MySQL DB content) from previous runs. To support changes of partition # MySQL DB content) from previous runs. To support changes of partition
......
...@@ -29,8 +29,7 @@ import os ...@@ -29,8 +29,7 @@ import os
import re import re
from . import logger from . import logger
from .ProcessManager import SubprocessError from .ProcessManager import SubprocessError
from .Utils import rmtree from slapos.util import bytes2str, str2bytes, rmtree
from slapos.util import bytes2str, str2bytes
SVN_UP_REV = re.compile(r'^(?:At|Updated to) revision (\d+).$') SVN_UP_REV = re.compile(r'^(?:At|Updated to) revision (\d+).$')
SVN_CHANGED_REV = re.compile(r'^Last Changed Rev.*:\s*(\d+)', re.MULTILINE) SVN_CHANGED_REV = re.compile(r'^Last Changed Rev.*:\s*(\d+)', re.MULTILINE)
......
import os import os
import stat
import shutil
import errno
import six import six
from six.moves import map from six.moves import map
from slapos.util import rmtree
def rmtree(path):
"""Delete a path recursively.
Like shutil.rmtree, but supporting the case that some files or folder
might have been marked read only. """
def chmod_retry(func, failed_path, exc_info):
"""Make sure the directories are executable and writable.
"""
# Depending on the Python version, the following items differ.
if six.PY3:
expected_error_type = PermissionError
expected_func = os.lstat
else:
expected_error_type = OSError
expected_func = os.listdir
e = exc_info[1]
if isinstance(e, expected_error_type):
if e.errno == errno.ENOENT:
# because we are calling again rmtree on listdir errors, this path might
# have been already deleted by the recursive call to rmtree.
return
if e.errno == errno.EACCES:
if func is expected_func:
os.chmod(failed_path, 0o700)
# corner case to handle errors in listing directories.
# https://bugs.python.org/issue8523
return shutil.rmtree(failed_path, onerror=chmod_retry)
# If parent directory is not writable, we still cannot delete the file.
# But make sure not to change the parent of the folder we are deleting.
if failed_path != path:
os.chmod(os.path.dirname(failed_path), 0o700)
return func(failed_path)
raise
shutil.rmtree(path, onerror=chmod_retry)
def createFolder(folder, clean=False): def createFolder(folder, clean=False):
if os.path.exists(folder): if os.path.exists(folder):
......
...@@ -31,6 +31,7 @@ import logging ...@@ -31,6 +31,7 @@ import logging
from six.moves.urllib.parse import urljoin from six.moves.urllib.parse import urljoin
from contextlib import contextmanager from contextlib import contextmanager
from slapos.slap.slap import ConnectionError from slapos.slap.slap import ConnectionError
from slapos.util import rmtree
from . import logger, log_formatter from . import logger, log_formatter
from .ProcessManager import SubprocessError, ProcessManager, CancellationError from .ProcessManager import SubprocessError, ProcessManager, CancellationError
from subprocess import CalledProcessError from subprocess import CalledProcessError
...@@ -39,7 +40,6 @@ from .NodeTestSuite import NodeTestSuite, SlapOSInstance ...@@ -39,7 +40,6 @@ from .NodeTestSuite import NodeTestSuite, SlapOSInstance
from .ScalabilityTestRunner import ScalabilityTestRunner from .ScalabilityTestRunner import ScalabilityTestRunner
from .UnitTestRunner import UnitTestRunner from .UnitTestRunner import UnitTestRunner
from .Utils import deunicodeData from .Utils import deunicodeData
from .Utils import rmtree
from .. import taskdistribution from .. import taskdistribution
MAX_TEMP_TIME = 0.01 # time in days we should keep temp files MAX_TEMP_TIME = 0.01 # time in days we should keep temp files
......
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