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

parent ed24d550
...@@ -164,23 +164,30 @@ class Script: ...@@ -164,23 +164,30 @@ class Script:
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, overwrite=False, ignore_dir_list=[]): def copyTree(self, origin, destination, ignore_dir_list=None):
"""Recursively Copy directory. if "overwrite" is set to False, will stop if """Recursively Copy directory.
destination already exists. ignore_dir_list is an array of directories
you want to exclude. ignore_dir_list is a list of relative directories you want to exclude.
Example : copytree("/from", "/to", overwrite=True, ignore_dir_list=["a_private_dir"]) Example :
copytree("/from", "/to", 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'
......
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