rollback files if file type fails for netcdf
@@ -350,7 +350,7 @@ class NetCDFLogicalFile(AbstractLogicalFile):
"""
# had to import it here to avoid import loop
- from hs_core.views.utils import create_folder
+ from hs_core.views.utils import create_folder, remove_folder
log = logging.getLogger()
@@ -371,6 +371,7 @@ class NetCDFLogicalFile(AbstractLogicalFile):
resource_metadata = []
file_type_metadata = []
files_to_add_to_resource = []
+ upload_folder = ''
if res_file.has_generic_logical_file:
# get the file from irods to temp dir
temp_file = utils.get_file_from_irods(res_file)
@@ -390,10 +391,6 @@ class NetCDFLogicalFile(AbstractLogicalFile):
files_to_add_to_resource.append(dump_file)
file_folder = res_file.file_folder
with transaction.atomic():
- # first delete the netcdf file that we retrieved from irods
- # for setting it to netcdf file type
- delete_resource_file(resource.short_id, res_file.id, user)
-
# create a netcdf logical file object to be associated with
# resource files
logical_file = cls.create()
@@ -413,11 +410,6 @@ class NetCDFLogicalFile(AbstractLogicalFile):
# name as the name for the new folder
new_folder_path = cls.compute_file_type_folder(resource, file_folder,
nc_file_name)
- # Alva: This does nothing at all.
- # fed_file_full_path = ''
- # if resource.resource_federation_path:
- # fed_file_full_path = os.path.join(resource.root_path,
- # new_folder_path)
create_folder(resource.short_id, new_folder_path)
log.info("Folder created:{}".format(new_folder_path))
@@ -439,19 +431,6 @@ class NetCDFLogicalFile(AbstractLogicalFile):
logical_file.add_resource_file(new_res_file)
log.info("NetCDF file type - new files were added to the resource.")
- except Exception as ex:
- msg = "NetCDF file type. Error when setting file type. Error:{}"
- msg = msg.format(ex.message)
- log.exception(msg)
- # TODO: in case of any error put the original file back and
- # delete the folder that was created
- raise ValidationError(msg)
- finally:
- # remove temp dir
- if os.path.isdir(temp_dir):
- shutil.rmtree(temp_dir)
-
- log.info("NetCDF file type was created.")
# use the extracted metadata to populate resource metadata
for element in resource_metadata:
@@ -486,6 +465,23 @@ class NetCDFLogicalFile(AbstractLogicalFile):
log.info("NetCDF file type - metadata was saved to DB")
# set resource to private if logical file is missing required metadata
resource.update_public_and_discoverable()
+ # delete the original resource file
+ delete_resource_file(resource.short_id, res_file.id, user)
+ log.info("Deleted original resource file.")
+ except Exception as ex:
+ msg = "NetCDF file type. Error when setting file type. Error:{}"
+ msg = msg.format(ex.message)
+ log.exception(msg)
+ if upload_folder:
+ # delete any new files uploaded as part of setting file type
+ folder_to_remove = os.path.join('data', 'contents', upload_folder)
+ remove_folder(user, resource.short_id, folder_to_remove)
+ log.info("Deleted newly created file type folder")
+ raise ValidationError(msg)
+ finally:
+ # remove temp dir
+ if os.path.isdir(temp_dir):
+ shutil.rmtree(temp_dir)
else:
err_msg = "Not a valid NetCDF file. File type file validation failed."
log.error(err_msg)
