Luke review : add try/catch and be safer, documentation, follow conventions

parent ed24d550
...@@ -163,26 +163,33 @@ class Script: ...@@ -163,26 +163,33 @@ class Script:
setuptools.archive_util.unpack_archive(path, extract_dir) setuptools.archive_util.unpack_archive(path, extract_dir)
self.cleanup_dir_list.append(extract_dir) self.cleanup_dir_list.append(extract_dir)
return extract_dir return extract_dir
def copyTree(self, origin, destination, ignore_dir_list=None):
"""Recursively Copy directory.
def copytree(self, origin, destination, overwrite=False, ignore_dir_list=[]): ignore_dir_list is a list of relative directories you want to exclude.
"""Recursively Copy directory. if "overwrite" is set to False, will stop if Example :
destination already exists. ignore_dir_list is an array of directories copytree("/from", "/to", ignore_dir_list=["a_private_dir"])
you want to exclude.
Example : copytree("/from", "/to", overwrite=True, ignore_dir_list=["a_private_dir"])
""" """
if os.path.exists(destination) and not overwrite: # Check existence before beginning. We don't want to cleanup something
self.logger.info('Destination already exists, aborting.') # that does not belong to us.
return False if os.path.exists(destination):
raise shutil.Error('Destination already exists: %s' % destination)
self.logger.info("Copying %s to %s" % (origin, destination)) self.logger.info("Copying %s to %s" % (origin, destination))
if ignore_dir_list is None:
ignore_dir_list = []
try: try:
shutil.copytree(origin, destination, shutil.copytree(origin, destination,
ignore=lambda src,names:ignore_dir_list) ignore=lambda src,names:ignore_dir_list)
except shutil.Error: except (shutil.Error, OSError) as error:
self.logger.error("Error occurred : %s") # Cleanup in case of failure
shutil.rmtree(destination) try:
return False shutil.rmtree(destination, ignore_errors=True)
except (shutil.Error, OSError), strerror:
self.logger.error("Error occurred when cleaning after error: %s", strerror)
raise error
return True return True
script = 'raise NotImplementedError' script = 'raise NotImplementedError'
def __init__(self, buildout, name, options): def __init__(self, buildout, name, options):
self.cleanup_dir_list = [] self.cleanup_dir_list = []
......
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