<commit_before>import os
import shutil
import errno
import tempfile
import mimetypes
import zipfile

from foresite import utils, Aggregation, AggregatedResource, RdfLibSerializer
from rdflib import Namespace, URIRef

import bagit
from hs_core.models import Bags, ResourceFile


class HsBagitException(Exception):
    pass


def delete_files_and_bag(resource):
    """
    delete the resource bag and all resource files.

    Parameters:
    :param resource: the resource to delete the bag and files for.
    :return: none
    """
    istorage = resource.get_irods_storage()

    # delete resource directory first to remove all generated bag-related files for the resource
    if istorage.exists(resource.root_path):
        istorage.delete(resource.root_path)

    if istorage.exists(resource.bag_path):
        istorage.delete(resource.bag_path)

    # TODO: delete this whole mechanism; redundant.
    # delete the bags table
    for bag in resource.bags.all():
        bag.delete()


def create_bag_files(resource):
    """
    create and update files needed by bagit operation that is conducted on iRODS server;
    no bagit operation is performed, only files that will be included in the bag are created<commit_msg>HotFix to delete a resource during an iRODS failure.<commit_after>import os
import shutil
import errno
import tempfile
import mimetypes
import zipfile
import logging

from foresite import utils, Aggregation, AggregatedResource, RdfLibSerializer
from rdflib import Namespace, URIRef

import bagit
from hs_core.models import Bags, ResourceFile


class HsBagitException(Exception):
    pass


def delete_files_and_bag(resource):
    """
    delete the resource bag and all resource files.

    Parameters:
    :param resource: the resource to delete the bag and files for.
    :return: none
    """
    istorage = resource.get_irods_storage()

    # delete resource directory first to remove all generated bag-related files for the resource
    try:
        if istorage.exists(resource.root_path):
            istorage.delete(resource.root_path)
    except Exception as e:
        logger = logging.getLogger(__name__)
        logger.error("cannot remove {}: {}".format(resource.root_path, e))

    try:
        if istorage.exists(resource.bag_path):
            istorage.delete(resource.bag_path)
    except Exception as e:
        logger = logging.getLogger(__name__)
        logger.error("cannot remove {}: {}".format(resource.bag_path, e))

    # TODO: delete this whole mechanism; redundant.
    # delete the bags table
    for bag in resource.bags.all():
        bag.delete()


def create_bag_files(resource):
    """
    create and update files needed by bagit operation that is conducted on iRODS server;
    no bagit operation is performed, only files that will be included in the bag are created
{'fraction_non_alphanumeric': 0.06699751861042183, 'fraction_numerical': 0.0, 'mean_word_length': 3.581168831168831, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2243876', 'n_tokens_mistral': 785, 'n_tokens_neox': 759, 'n_words': 283}