Ejemplo n.º 1
0
    def run(self, package_path=None, xml_path=None):
        package_path, xml_path = self.parse_params(package_path, xml_path)
        ip = self.get_information_package()
        package_path = package_path if package_path is not None else ip.object_path
        xml_path = xml_path if xml_path is not None else os.path.splitext(
            package_path)[0] + '.xml'

        generate_package_mets(ip, package_path, xml_path)
        return xml_path
Ejemplo n.º 2
0
def GeneratePackageMets(self, package_path=None, xml_path=None):
    package_path, xml_path = self.parse_params(package_path, xml_path)
    ip = self.get_information_package()
    package_path = package_path if package_path is not None else ip.object_path
    xml_path = xml_path if xml_path is not None else os.path.splitext(
        package_path)[0] + '.xml'

    generate_package_mets(ip, package_path, xml_path)
    msg = 'Generated {xml}'.format(xml=xml_path)
    self.create_success_event(msg)
    return xml_path
Ejemplo n.º 3
0
def StorageMigration(self, storage_method, temp_path):
    ip = self.get_information_package()
    container_format = ip.get_container_format()
    storage_method = StorageMethod.objects.get(pk=storage_method)

    try:
        storage_target = storage_method.enabled_target
    except StorageTarget.DoesNotExist:
        raise ValueError('No writeable target available for {}'.format(storage_method))

    dir_path = os.path.join(temp_path, ip.object_identifier_value)
    container_path = os.path.join(temp_path, ip.object_identifier_value + '.{}'.format(container_format))
    aip_xml_path = os.path.join(temp_path, ip.object_identifier_value + '.xml')
    aic_xml_path = os.path.join(temp_path, ip.aic.object_identifier_value + '.xml')

    if storage_target.master_server and not storage_target.remote_server:
        # we are on remote host
        src_container = True
    else:
        # we are not on master, access from existing storage object
        storage_object = ip.get_fastest_readable_storage_object()
        if storage_object.container:
            storage_object.read(container_path, self.get_processtask())
        else:
            storage_object.read(dir_path, self.get_processtask())

        src_container = storage_object.container

    dst_container = storage_method.containers

    # If storage_object is "long term" and storage_method is not (or vice versa),
    # then we have to do some "conversion" before we go any further

    if src_container and not dst_container:
        # extract container
        if container_format == 'tar':
            with tarfile.open(container_path) as tar:
                tar.extractall(temp_path)
        elif container_format == 'zip':
            with zipfile.ZipFile(container_path) as zipf:
                zipf.extractall(temp_path)
        else:
            raise ValueError('Invalid container format: {}'.format(container_format))

    elif not src_container and dst_container:
        # create container, aip xml and aic xml
        if container_format == 'tar':
            with tarfile.open(container_path, 'w') as new_tar:
                new_tar.format = settings.TARFILE_FORMAT
                new_tar.add(dir_path)
        elif container_format == 'zip':
            zip_directory(dirname=dir_path, zipname=container_path, compress=False)
        else:
            raise ValueError('Invalid container format: {}'.format(container_format))

        generate_package_mets(ip, container_path, aip_xml_path)
        generate_aic_mets(ip, aic_xml_path)

    if dst_container or storage_target.remote_server:
        src = [
            container_path,
            aip_xml_path,
            aic_xml_path,
        ]
    else:
        src = [dir_path]

    if storage_target.remote_server:
        # we are on master, copy files to remote

        host, user, passw = storage_target.remote_server.split(',')
        dst = urljoin(host, reverse('informationpackage-add-file-from-master'))
        requests_session = requests.Session()
        requests_session.verify = settings.REQUESTS_VERIFY
        requests_session.auth = (user, passw)

        for s in src:
            copy_file(s, dst, requests_session=requests_session)

    obj_id = ip.preserve(src, storage_target, dst_container, self.get_processtask())

    Notification.objects.create(
        message="Migrated {} to {}".format(ip.object_identifier_value, storage_method.name),
        level=logging.INFO,
        user_id=self.responsible,
        refresh=True,
    )

    return obj_id
Ejemplo n.º 4
0
 def run(self):
     generate_package_mets(self.get_information_package())