Пример #1
0
def call(jobs):
    for job in jobs:
        with job.JobContext(logger=logger):
            try:
                sip_path = job.args[1]

                # Move everything out of data directory
                for item in os.listdir(os.path.join(sip_path, 'data')):
                    src = os.path.join(sip_path, 'data', item)
                    dst = os.path.join(sip_path, item)
                    _move_file(job, src, dst)

                os.rmdir(os.path.join(sip_path, 'data'))

                # Move metadata and logs out of objects if they exist
                objects_path = os.path.join(sip_path, 'objects')
                src = os.path.join(objects_path, 'metadata')
                dst = os.path.join(sip_path, 'metadata')
                _move_file(job, src, dst, exit_on_error=False)

                src = os.path.join(objects_path, 'logs')
                dst = os.path.join(sip_path, 'logs')
                _move_file(job, src, dst, exit_on_error=False)

                # Move anything unexpected to submission documentation
                # Leave objects, metadata, etc
                # Original METS ends up in submissionDocumentation
                subm_doc_path = os.path.join(sip_path, 'metadata',
                                             'submissionDocumentation')
                os.makedirs(subm_doc_path)
                mets_file_path = None
                for item in os.listdir(sip_path):
                    # Leave SIP structure
                    if item in archivematicaFunctions.OPTIONAL_FILES + archivematicaFunctions.REQUIRED_DIRECTORIES:
                        continue
                    src = os.path.join(sip_path, item)
                    dst = os.path.join(subm_doc_path, item)
                    if item.startswith('METS.') and item.endswith('.xml'):
                        mets_file_path = dst
                    _move_file(job, src, dst)

                # Reconstruct any empty directories documented in the METS file under the
                # logical structMap labelled "Normative Directory Structure"
                if mets_file_path:
                    archivematicaFunctions.reconstruct_empty_directories(
                        mets_file_path, objects_path, logger=logger)
                else:
                    logger.info(
                        'Unable to reconstruct empty directories: no METS file'
                        ' could be found in {}'.format(sip_path))

                archivematicaFunctions.create_structured_directory(
                    sip_path,
                    manual_normalization=True,
                    printing=True,
                    printfn=job.pyprint)
            except IOError as err:
                job.print_error(repr(err))
                job.set_status(1)
def restructure_transfer_aip(unit_path):
    """
    Restructure a transfer that comes from re-ingesting an Archivematica AIP.
    """
    old_bag = os.path.join(unit_path, 'old_bag', '')
    os.makedirs(old_bag)

    # Move everything to old_bag
    for item in os.listdir(unit_path):
        if item == 'old_bag':
            continue
        src = os.path.join(unit_path, item)
        _move_file(src, old_bag)

    # Create required directories
    # - "/logs" and "/logs/fileMeta"
    # - "/metadata" and "/metadata/submissionDocumentation"
    # - "/objects"
    create_structured_directory(unit_path, printing=True)

    # Move /old_bag/data/METS.<UUID>.xml => /metadata/METS.<UUID>.xml
    p = re.compile(r'^METS\..*\.xml$', re.IGNORECASE)
    src = os.path.join(old_bag, 'data')
    for item in os.listdir(src):
        m = p.match(item)
        if m:
            break  # Stop trying after the first match
    src = os.path.join(src, m.group())
    dst = os.path.join(unit_path, 'metadata')
    mets_file_path = dst
    _move_file(src, dst)

    # Move /old_bag/data/objects/metadata/* => /metadata/
    src = os.path.join(old_bag, 'data', 'objects', 'metadata')
    dst = os.path.join(unit_path, 'metadata')
    if os.path.isdir(src):
        for item in os.listdir(src):
            item_path = os.path.join(src, item)
            _move_file(item_path, dst)
        shutil.rmtree(src)

    # Move /old_bag/data/objects/submissionDocumentation/* => /metadata/submissionDocumentation/
    src = os.path.join(old_bag, 'data', 'objects', 'submissionDocumentation')
    dst = os.path.join(unit_path, 'metadata', 'submissionDocumentation')
    for item in os.listdir(src):
        item_path = os.path.join(src, item)
        _move_file(item_path, dst)
    shutil.rmtree(src)

    # Move /old_bag/data/objects/* => /objects/
    src = os.path.join(old_bag, 'data', 'objects')
    objects_path = dst = os.path.join(unit_path, 'objects')
    for item in os.listdir(src):
        item_path = os.path.join(src, item)
        _move_file(item_path, dst)

    # Move /old_bag/processingMCP.xml => /processingMCP.xml
    src = os.path.join(old_bag, 'processingMCP.xml')
    dst = os.path.join(unit_path, 'processingMCP.xml')
    if os.path.isfile(src):
        _move_file(src, dst)

    # Get rid of old_bag
    shutil.rmtree(old_bag)

    # Reconstruct any empty directories documented in the METS file under the
    # logical structMap labelled "Normative Directory Structure"
    reconstruct_empty_directories(mets_file_path, objects_path, logger=logger)
    # Move anything unexpected to submission documentation
    # Leave objects, metadata, etc
    # Original METS ends up in submissionDocumentation
    subm_doc_path = os.path.join(sip_path, 'metadata',
                                 'submissionDocumentation')
    os.makedirs(subm_doc_path)
    mets_file_path = None
    for item in os.listdir(sip_path):
        # Leave SIP structure
        if item in archivematicaFunctions.OPTIONAL_FILES + archivematicaFunctions.REQUIRED_DIRECTORIES:
            continue
        src = os.path.join(sip_path, item)
        dst = os.path.join(subm_doc_path, item)
        if item.startswith('METS.') and item.endswith('.xml'):
            mets_file_path = dst
        _move_file(src, dst)

    # Reconstruct any empty directories documented in the METS file under the
    # logical structMap labelled "Normative Directory Structure"
    if mets_file_path:
        archivematicaFunctions.reconstruct_empty_directories(mets_file_path,
                                                             objects_path,
                                                             logger=logger)
    else:
        logger.info('Unable to reconstruct empty directories: no METS file'
                    ' could be found in {}'.format(sip_path))

    archivematicaFunctions.create_structured_directory(
        sip_path, manual_normalization=True, printing=True)