Beispiel #1
0
def test_validate_file_metadata_invalid_metadata(requests_mock):
    """Test ``_validate_file_metadata``.

    Function should raise exceptions with descriptive error messages.

    :param requests_mock: Mocker object
    :returns: ``None``
    """
    file_metadata = copy.deepcopy(BASE_FILE)
    file_metadata['file_characteristics'] = {
        "file_created": "2014-01-17T08:19:31Z"
    }
    tests.utils.add_metax_dataset(requests_mock, files=[file_metadata])

    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    client = Metax(configuration.get('metax_url'),
                   configuration.get('metax_user'),
                   configuration.get('metax_password'),
                   verify=configuration.getboolean('metax_ssl_verification'))

    expected_error = (
        "Validation error in metadata of path/to/file: 'file_format' is"
        " a required property\n\nFailed validating 'required' in schema")
    with pytest.raises(InvalidFileMetadataError, match=expected_error):
        # pylint: disable=protected-access
        siptools_research.metadata_validator._validate_file_metadata(
            {'identifier': 'dataset_identifier'}, client, configuration)
def test_verify_file_contained_by_dataset_files():
    """Test is_consistent_for_file method.

    Check that ``DatasetConsistency::is_consistent_for_file()`` succeeds
    when dataset files contains the file

    :returns: ``None``
    """
    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    client = Metax(
        configuration.get('metax_url'),
        configuration.get('metax_user'),
        configuration.get('metax_password'),
        verify=configuration.getboolean('metax_ssl_verification')
    )
    dataset = {
        'identifier': 'dataset_identifier',
        'research_dataset': {'files': [
            {'identifier': 'file_identifier'}
        ], 'directories': []}
    }

    file_metadata = {
        'identifier': 'file_identifier',
        'file_path': "/path/to/file",
        'parent_directory': {'identifier': 'parent_directory_identifier'}
    }
    try:
        dirs = DatasetConsistency(client, dataset)
        dirs.is_consistent_for_file(file_metadata)
    except InvalidDatasetMetadataError as exc:
        pytest.fail(
            '_verify_file_contained_by_dataset raised exception: ' + str(exc)
        )
Beispiel #3
0
def test_validate_datacite(requests_mock):
    """Test _validate_datacite.

    Function should raises exception with readable error message when datacite
    XML contains multiple errors.

    :param requests_mock: Mocker object
    :returns: ``None``
    """
    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    metax_client = Metax(
        configuration.get('metax_url'),
        configuration.get('metax_user'),
        configuration.get('metax_password'),
        verify=configuration.getboolean('metax_ssl_verification'))

    requests_mock.get(
        "https://metaksi/rest/v1/datasets/dataset_identifier?"
        "dataset_format=datacite",
        content=get_very_invalid_datacite())

    # Try to validate datacite
    expected_error = "Datacite metadata is invalid:"
    # pylint: disable=protected-access
    with pytest.raises(InvalidDatasetMetadataError, match=expected_error):
        metadata_validator._validate_datacite('dataset_identifier',
                                              metax_client)
Beispiel #4
0
def test_validate_file_metadata(requests_mock):
    """Test _validate_file_metadata.

    Check that dataset directory caching is working correctly in
    DatasetConsistency when the files have common root directory in
    dataset.directories property.

    :param requests_mock: Mocker object
    :returns: ``None``
    """
    dataset = copy.deepcopy(BASE_DATASET)
    dataset['research_dataset']['directories'] = [{'identifier': 'root_dir'}]
    file_1 = copy.deepcopy(TXT_FILE)
    file_1['identifier'] = 'file_identifier1'
    file_2 = copy.deepcopy(TXT_FILE)
    file_2['identifier'] = 'file_identifier2'
    requests_mock.get(tests.conftest.METAX_URL +
                      '/directories/pid:urn:dir:wf1',
                      json={
                          'identifier': 'first_par_dir',
                          'directory_path': '',
                          'parent_directory': {
                              'identifier': 'second_par_dir'
                          }
                      },
                      status_code=200)
    requests_mock.get(tests.conftest.METAX_URL + '/directories/second_par_dir',
                      json={
                          'identifier': 'second_par_dir',
                          'directory_path': '',
                          'parent_directory': {
                              'identifier': 'root_dir'
                          }
                      },
                      status_code=200)
    requests_mock.get(tests.conftest.METAX_URL + '/directories/root_dir',
                      json={
                          'identifier': 'second_par_dir',
                          'directory_path': '/'
                      },
                      status_code=200)
    files_adapter = requests_mock.get(tests.conftest.METAX_URL +
                                      '/datasets/dataset_identifier/files',
                                      json=[file_1, file_2],
                                      status_code=200)

    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    client = Metax(configuration.get('metax_url'),
                   configuration.get('metax_user'),
                   configuration.get('metax_password'),
                   verify=configuration.getboolean('metax_ssl_verification'))

    # pylint: disable=protected-access
    siptools_research.metadata_validator._validate_file_metadata(
        dataset, client, configuration)

    assert files_adapter.call_count == 1
    def find_file_categories(self):
        """Create logical structure map of dataset files.

        Returns dictionary with filecategories as keys and filepaths as
        values.

        :returns: logical structure map dictionary
        """
        config_object = Configuration(self.config)
        metax_client = Metax(
            config_object.get('metax_url'),
            config_object.get('metax_user'),
            config_object.get('metax_password'),
            verify=config_object.getboolean('metax_ssl_verification'))
        dataset_files = metax_client.get_dataset_files(self.dataset_id)
        dataset_metadata = metax_client.get_dataset(self.dataset_id)
        languages = get_dataset_languages(dataset_metadata)
        dirpath2usecategory = get_dirpath_dict(metax_client, dataset_metadata)
        logical_struct = dict()

        for dataset_file in dataset_files:

            file_id = dataset_file['identifier']

            # Get the use category of file. The path to the file in
            # logical structmap is stored in 'use_category' in metax.
            filecategory = find_file_use_category(file_id, dataset_metadata)

            # If file listed in datasets/<id>/files is not listed in
            # 'files' section of dataset metadata, look for
            # parent_directory of the file from  'directories' section.
            # The "use_category" of file is the "use_category" of the
            # parent directory.
            if filecategory is None:
                name_len = len(dataset_file["file_name"])

                filecategory = find_dir_use_category(
                    dataset_file["file_path"][:-name_len], dirpath2usecategory,
                    languages)

            # If file category was not found even for the parent
            # directory, raise error
            if filecategory is None:
                raise InvalidDatasetMetadataError(
                    "File category for file {} was not found".format(file_id))

            # Append path to logical_struct[filecategory] list. Create
            # list if it does not exist already
            if filecategory not in logical_struct.keys():
                logical_struct[filecategory] = []
            logical_struct[filecategory].append(dataset_file['file_path'])

        return logical_struct
    def run(self):
        """Get dataset 1 from Metax.

        :returns:  ``None``
        """
        config_object = Configuration(self.config)
        metax_client = Metax(
            config_object.get('metax_url'),
            config_object.get('metax_user'),
            config_object.get('metax_password'),
            verify=config_object.getboolean('metax_ssl_verification'))
        metax_client.get_dataset('1')
def test_directory_validation_caching_works(requests_mock):
    """Test directory validation caching.

    Two files are contained by same directory. Metax is called only once for
    each directory in tree and hence the directory validation as well.

    :returns: ``None``
    """
    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    client = Metax(configuration.get('metax_url'),
                   configuration.get('metax_user'),
                   configuration.get('metax_password'),
                   verify=configuration.getboolean('metax_ssl_verification'))
    first_par_dir_adapter = requests_mock.get(
        tests.conftest.METAX_URL + '/directories/first_par',
        json={
            'identifier': 'first_par',
            'directory_path': '/second_par/first_par',
            'parent_directory': {
                'identifier': 'second_par'
            }
        },
        status_code=200)
    second_par_dir_adapter = requests_mock.get(
        tests.conftest.METAX_URL + '/directories/second_par',
        json={
            'identifier': 'second_par',
            'directory_path': '/second_par',
            'parent_directory': {
                'identifier': 'root'
            }
        },
        status_code=200)
    root_dir_adapter = requests_mock.get(tests.conftest.METAX_URL +
                                         '/directories/root',
                                         json={
                                             'identifier': 'root',
                                             'directory_path': '/'
                                         },
                                         status_code=200)
    file2_metadata = copy.deepcopy(FILE_METADATA)
    file2_metadata['file_path'] = ["/path/to/file2"]
    try:
        validator = DirectoryValidation(client)
        validator.is_valid_for_file(FILE_METADATA)
        validator.is_valid_for_file(file2_metadata)
    except InvalidDatasetMetadataError as exc:
        pytest.fail('test_successful_directory_validation fails: ' + str(exc))
    # verify that metax is called only once for directories
    assert first_par_dir_adapter.call_count == 1
    assert second_par_dir_adapter.call_count == 1
    assert root_dir_adapter.call_count == 1
def _create_premis_events(dataset_id, workspace, config):
    """Create premis events from provenance metadata.

    Reads dataset provenance metadata from Metax. For each provenance
    object a METS document that contains a PREMIS event element is
    created.

    :param dataset_id: dataset identifier
    :param workspace: SIP creation directory
    :param config: path to configuration file
    :returns: ``None``
    """
    config_object = Configuration(config)
    metadata = Metax(config_object.get('metax_url'),
                     config_object.get('metax_user'),
                     config_object.get('metax_password'),
                     verify=config_object.getboolean(
                         'metax_ssl_verification')).get_dataset(dataset_id)

    dataset_languages = get_dataset_languages(metadata)

    provenances = metadata["research_dataset"]["provenance"]

    for provenance in provenances:

        event_type = get_localized_value(
            provenance["preservation_event"]["pref_label"],
            languages=dataset_languages)

        try:
            event_datetime = provenance["temporal"]["start_date"]
        except KeyError:
            event_datetime = 'OPEN'

        event_detail = get_localized_value(provenance["description"],
                                           languages=dataset_languages)

        event_outcome = get_localized_value(
            provenance["event_outcome"]["pref_label"],
            languages=dataset_languages)

        event_outcome_detail = get_localized_value(
            provenance["outcome_description"], languages=dataset_languages)

        premis_event.premis_event(workspace=workspace,
                                  event_type=event_type,
                                  event_datetime=event_datetime,
                                  event_detail=event_detail,
                                  event_outcome=event_outcome,
                                  event_outcome_detail=event_outcome_detail)
def test_successful_directory_validation_fails(requests_mock):
    """Test validation of invalid directory tree.

    The root directory is missing the `directory_path` attribute

    :returns: ``None``
    """
    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    client = Metax(configuration.get('metax_url'),
                   configuration.get('metax_user'),
                   configuration.get('metax_password'),
                   verify=configuration.getboolean('metax_ssl_verification'))
    first_par_dir_adapter = requests_mock.get(
        tests.conftest.METAX_URL + '/directories/first_par',
        json={
            'identifier': 'first_par',
            'directory_path': '/second_par/first_par',
            'parent_directory': {
                'identifier': 'second_par'
            }
        },
        status_code=200)
    second_par_dir_adapter = requests_mock.get(
        tests.conftest.METAX_URL + '/directories/second_par',
        json={
            'identifier': 'second_par',
            'directory_path': '/second_par',
            'parent_directory': {
                'identifier': 'root'
            }
        },
        status_code=200)
    root_dir_adapter = requests_mock.get(tests.conftest.METAX_URL +
                                         '/directories/root',
                                         json={'identifier': 'root'},
                                         status_code=200)
    with pytest.raises(InvalidDatasetMetadataError) as exc_info:
        validator = DirectoryValidation(client)
        validator.is_valid_for_file(FILE_METADATA)

    assert str(exc_info.value).startswith(
        "Validation error in metadata of root: "
        "'directory_path' is a required property")

    assert first_par_dir_adapter.call_count == 1
    assert second_par_dir_adapter.call_count == 1
    assert root_dir_adapter.call_count == 1
def test_successful_directory_validation(requests_mock):
    """Directory validation of valid directory tree.

    :returns: ``None``
    """
    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    client = Metax(configuration.get('metax_url'),
                   configuration.get('metax_user'),
                   configuration.get('metax_password'),
                   verify=configuration.getboolean('metax_ssl_verification'))

    first_par_dir_adapter = requests_mock.get(
        tests.conftest.METAX_URL + '/directories/first_par',
        json={
            'identifier': 'first_par',
            'directory_path': '/second_par/first_par',
            'parent_directory': {
                'identifier': 'second_par'
            }
        },
        status_code=200)
    second_par_dir_adapter = requests_mock.get(
        tests.conftest.METAX_URL + '/directories/second_par',
        json={
            'identifier': 'second_par',
            'directory_path': '/second_par',
            'parent_directory': {
                'identifier': 'root'
            }
        },
        status_code=200)
    root_dir_adapter = requests_mock.get(tests.conftest.METAX_URL +
                                         '/directories/root',
                                         json={
                                             'identifier': 'root',
                                             'directory_path': '/'
                                         },
                                         status_code=200)
    try:
        validator = DirectoryValidation(client)
        validator.is_valid_for_file(FILE_METADATA)
    except InvalidDatasetMetadataError as exc:
        pytest.fail('test_successful_directory_validation fails: ' + str(exc))
    assert first_par_dir_adapter.call_count == 1
    assert second_par_dir_adapter.call_count == 1
    assert root_dir_adapter.call_count == 1
    def get_provenance_ids(self):
        """List identifiers of provenance events.

        Gets list of dataset provenance events from Metax, and reads
        provenance IDs of the events from event.xml files found in the
        workspace directory.

        :returns: list of provenance IDs
        """
        config_object = Configuration(self.config)
        metax_client = Metax(
            config_object.get('metax_url'),
            config_object.get('metax_user'),
            config_object.get('metax_password'),
            verify=config_object.getboolean('metax_ssl_verification'))
        metadata = metax_client.get_dataset(self.dataset_id)
        languages = get_dataset_languages(metadata)

        # Get the reference file path from Luigi task input
        # It already contains the workspace path.
        event_ids = get_md_references(
            read_md_references(
                self.workspace,
                os.path.basename(
                    self.input()['create_provenance_information'].path)))

        event_type_ids = {}
        for event_id in event_ids:
            event_file = event_id[1:] + "-PREMIS%3AEVENT-amd.xml"
            event_file_path = os.path.join(self.sip_creation_path, event_file)
            if not os.path.exists(event_file_path):
                continue
            root = ET.parse(encode_path(event_file_path)).getroot()
            event_type = root.xpath("//premis:eventType",
                                    namespaces=NAMESPACES)[0].text
            event_type_ids[event_type] = event_id

        provenance_ids = []
        for provenance in metadata["research_dataset"]["provenance"]:
            event_type = get_localized_value(
                provenance["preservation_event"]["pref_label"],
                languages=languages)
            provenance_ids += [event_type_ids[event_type]]

        return provenance_ids
    def run(self):
        """Read list of required files from Metax and download them.

        Files are written to path based on ``file_path`` in Metax.

        :returns: ``None``
        """
        upload_database = upload_rest_api.database.Database()

        # Find file identifiers from Metax dataset metadata.
        config_object = Configuration(self.config)
        metax_client = Metax(
            config_object.get('metax_url'),
            config_object.get('metax_user'),
            config_object.get('metax_password'),
            verify=config_object.getboolean('metax_ssl_verification'))
        dataset_files = metax_client.get_dataset_files(self.dataset_id)

        # Download files to temporary directory which will be moved to
        # output target path when all files have been downloaded
        with self.output().temporary_path() as temporary_directory:
            os.mkdir(temporary_directory)

            for dataset_file in dataset_files:
                identifier = dataset_file["identifier"]

                # Full path to file
                target_path = os.path.normpath(
                    os.path.join(temporary_directory,
                                 dataset_file["file_path"].strip('/')))
                if not target_path.startswith(temporary_directory):
                    raise InvalidFileMetadataError(
                        'The file path of file %s is invalid: %s' %
                        (identifier, dataset_file["file_path"]))

                # Create the download directory for file if it does not
                # exist already
                if not os.path.isdir(os.path.dirname(target_path)):
                    # TODO: Use exist_ok -parameter when moving to
                    # python3
                    os.makedirs(os.path.dirname(target_path))

                download_file(dataset_file, target_path, self.config,
                              upload_database)
def test_verify_file_contained_by_dataset_missing_from_dataset(requests_mock):
    """Test is_consistent_for_file method.

    Check that ``DatasetConsistency::is_consistent_for_file()`` raises
    exception with descriptive error messages when dataset files nor
    directories do not contain the file.

    :returns: ``None``
    """
    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    client = Metax(
        configuration.get('metax_url'),
        configuration.get('metax_user'),
        configuration.get('metax_password'),
        verify=configuration.getboolean('metax_ssl_verification')
    )
    dataset = {
        'identifier': 'dataset_identifier',
        'research_dataset': {
            'files': [],
            'directories': []
        }
    }

    file_metadata = {
        'identifier': 'file_identifier',
        'file_path': "/path/to/file",
        'parent_directory': {
            'identifier': 'parent_directory_identifier'
        }
    }
    requests_mock.get(
        tests.conftest.METAX_URL + '/directories/parent_directory_identifier',
        json={'identifier': 'parent_directory_identifier'},
        status_code=200
    )
    with pytest.raises(InvalidDatasetMetadataError) as exc_info:
        dirs = DatasetConsistency(client, dataset)
        dirs.is_consistent_for_file(file_metadata)

    assert str(exc_info.value) == ("File not found from dataset files nor "
                                   "directories: /path/to/file")
    def run(self):
        """Report preservation status to Metax.

        Checks the path of ingest report file in digital preservation
        service. If the ingest report is in ~/accepted/.../ directory,
        the dataset has passed validation.If the report is found in
        ~/rejected/.../ directory, or somewhere else, an exception is
        risen. The event handlers will deal with the exceptions.

        :returns: ``None``
        """
        # List of all matching paths ValidateSIP found
        ingest_report_paths = self.input()[0].existing_paths()

        # Only one ingest report should be found
        assert len(ingest_report_paths) == 1

        # 'accepted' or 'rejected'?
        directory = ingest_report_paths[0].split('/')[0]
        if directory == 'accepted':
            # Init metax
            config_object = Configuration(self.config)
            metax_client = Metax(
                config_object.get('metax_url'),
                config_object.get('metax_user'),
                config_object.get('metax_password'),
                verify=config_object.getboolean('metax_ssl_verification'))
            # Set Metax preservation state of this dataset to 6 ("in
            # longterm preservation")
            metax_client.set_preservation_state(
                self.dataset_id,
                state=DS_STATE_IN_DIGITAL_PRESERVATION,
                system_description='Accepted to preservation')
            with self.output().open('w') as output:
                output.write('Dataset id=' + self.dataset_id)
        elif directory == 'rejected':
            # Raise exception that informs event handler that dataset
            # did not pass validation
            raise InvalidSIPError("SIP was rejected")
        else:
            raise ValueError('Report was found in incorrect '
                             'path: %s' % ingest_report_paths[0])
Beispiel #15
0
    def run(self):
        """Copy datacite.xml metadatafile from Metax.

        Creates a METS document that contains dmdSec element with
        datacite metadata.

        :returns: ``None``
        """
        # Get datacite.xml from Metax
        config_object = Configuration(self.config)
        metax_client = Metax(
            config_object.get('metax_url'),
            config_object.get('metax_user'),
            config_object.get('metax_password'),
            verify=config_object.getboolean('metax_ssl_verification'))
        dataset = metax_client.get_dataset(self.dataset_id)
        datacite = metax_client.get_datacite(dataset['identifier'])

        # Write datacite.xml to file
        datacite_path = os.path.join(self.workspace, 'datacite.xml')
        datacite.write(datacite_path)

        tmp = os.path.join(config_object.get('packaging_root'), 'tmp/')
        with TemporaryDirectory(prefix=tmp) as temporary_workspace:
            # Create output files with siptools
            import_description.import_description(
                dmdsec_location=datacite_path,
                workspace=temporary_workspace,
                without_uuid=True)

            # Move created files to SIP creation directory. PREMIS event
            # reference file is moved to output target path after
            # everything else is done.
            with self.output().temporary_path() as target_path:
                shutil.move(
                    os.path.join(temporary_workspace,
                                 'premis-event-md-references.jsonl'),
                    target_path)
                for file_ in os.listdir(temporary_workspace):
                    shutil.move(os.path.join(temporary_workspace, file_),
                                self.sip_creation_path)
Beispiel #16
0
def report_task_failure(task, exception):
    """Report task failure.

    This function is triggered when a WorkflowTask fails. Adds report of
    failed event to workflow database.

    If task failed because dataset is invalid, the preservation status of
    dataset is updated in Metax, and the workflow is disabled.

    :param task: WorkflowTask object
    :param exception: Exception that caused failure
    :returns: ``None``
    """
    database = Database(task.config)
    database.add_event(task.document_id, task.task_name, 'failure',
                       "%s: %s" % (task.failure_message, str(exception)))
    config_object = Configuration(task.config)
    metax_client = metax_access.Metax(
        config_object.get('metax_url'),
        config_object.get('metax_user'),
        config_object.get('metax_password'),
        verify=config_object.getboolean('metax_ssl_verification'))

    if isinstance(exception, siptools_research.exceptions.InvalidDatasetError):
        # Disable workflow
        database.set_disabled(task.document_id)

    if isinstance(exception, siptools_research.exceptions.InvalidSIPError):
        # Set preservation status for dataset in Metax
        metax_client.set_preservation_state(
            task.dataset_id,
            state=(metax_access.
                   DS_STATE_REJECTED_IN_DIGITAL_PRESERVATION_SERVICE),
            system_description=_get_description(task, exception))
    elif isinstance(exception,
                    siptools_research.exceptions.InvalidDatasetMetadataError):
        # Set preservation status for dataset in Metax
        metax_client.set_preservation_state(
            task.dataset_id,
            state=metax_access.DS_STATE_METADATA_VALIDATION_FAILED,
            system_description=_get_description(task, exception))
    def get_identifiers(self):
        """Get file identifiers.

        Return a list of all the file identifiers and the path to the
        downloaded files.

        :returns: Tuple (list of identifiers, cache_path)
        """
        config_object = Configuration(self.config)
        packaging_root = config_object.get("packaging_root")
        cache_path = os.path.join(packaging_root, "file_cache")

        metax_client = Metax(
            config_object.get('metax_url'),
            config_object.get('metax_user'),
            config_object.get('metax_password'),
            verify=config_object.getboolean('metax_ssl_verification'))
        try:
            dataset_files = metax_client.get_dataset_files(self.dataset_id)
            return [_file["identifier"] for _file in dataset_files], cache_path
        except DatasetNotAvailableError:
            return [], cache_path
Beispiel #18
0
def validate_metadata(dataset_id,
                      config="/etc/siptools_research.conf",
                      dummy_doi="false"):
    """Validate dataset.

    Reads dataset metadata, file metadata, and additional techMD XML from
    Metax and validates them against schemas. Raises error if dataset is not
    valid. Raises InvalidDatasetError if dataset is invalid.

    :param dataset_id: dataset identifier
    :param config: configuration file path
    :param: dummy_doi: 'true' if dummy preservation identifier is to be used
    :returns: ``True``, if dataset metadata is valid.
    """
    conf = Configuration(config)
    metax_client = Metax(conf.get('metax_url'),
                         conf.get('metax_user'),
                         conf.get('metax_password'),
                         verify=conf.getboolean('metax_ssl_verification'))
    # Get dataset metadata from Metax
    dataset_metadata = metax_client.get_dataset(dataset_id)

    # Validate dataset metadata
    _validate_dataset_metadata(dataset_metadata, dummy_doi=dummy_doi)

    # Validate dataset localization
    _validate_dataset_localization(dataset_metadata)

    # Validate contract metadata
    _validate_contract_metadata(dataset_metadata['contract']['identifier'],
                                metax_client)

    # Validate file metadata for each file in dataset files
    _validate_file_metadata(dataset_metadata, metax_client, conf)

    # Validate datacite provided by Metax
    _validate_datacite(dataset_id, metax_client, dummy_doi=dummy_doi)

    return True
Beispiel #19
0
def validate_files(dataset_id, config_file="/etc/siptools_research.conf"):
    """Validate all files in a dataset.

    Raises InvalidFileError or MissingFileError if files are invalid.

    :param dataset_id: dataset identifier
    :param config: configuration file path
    :returns: ``True`` if all files are well-formed.
    """
    invalid_files = []
    missing_files = []
    conf = Configuration(config_file)
    metax_client = Metax(
        conf.get('metax_url'),
        conf.get('metax_user'),
        conf.get('metax_password'),
        verify=conf.getboolean('metax_ssl_verification')
    )
    cache_path = os.path.join(conf.get("packaging_root"), "file_cache")

    dataset_files = _download_files(
        metax_client, dataset_id, config_file, missing_files
    )
    if missing_files:
        raise MissingFileError(
            "{} files are missing".format(len(missing_files)), missing_files
        )

    for dataset_file in dataset_files:
        _validate_file(dataset_file, cache_path, invalid_files)
    if invalid_files:
        raise InvalidFileError(
            "{} files are not well-formed".format(len(invalid_files)),
            invalid_files
        )

    return True
    def run(self):
        """Compile all metadata files into METS document.

        :returns: ``None``
        """
        config_object = Configuration(self.config)
        metax_client = Metax(
            config_object.get('metax_url'),
            config_object.get('metax_user'),
            config_object.get('metax_password'),
            verify=config_object.getboolean('metax_ssl_verification'))
        metadata = metax_client.get_dataset(self.dataset_id)

        # Get preservation_identifier from Metax
        preservation_id = metadata["preservation_identifier"]

        # Get contract data from Metax
        contract_id = metadata["contract"]["identifier"]
        contract_metadata = metax_client.get_contract(contract_id)
        contract_identifier = contract_metadata["contract_json"]["identifier"]
        contract_org_name \
            = contract_metadata["contract_json"]["organization"]["name"]

        # Compile METS
        mets = compile_mets.create_mets(workspace=self.sip_creation_path,
                                        mets_profile='tpas',
                                        contractid=contract_identifier,
                                        objid=preservation_id,
                                        organization_name=contract_org_name,
                                        packagingservice='Packaging Service')

        with self.output().open('wb') as outputfile:
            mets.write(outputfile,
                       pretty_print=True,
                       xml_declaration=True,
                       encoding='UTF-8')
Beispiel #21
0
class CreateTechnicalMetadata(WorkflowTask):
    """Create METS documents that contain technical metadata.

    The PREMIS object metadata is created to all dataset files and it is
    written to
    `<sip_creation_path>/<url_encoded_filepath>-PREMIS%3AOBJECT-amd.xml`.
    File properties are written to
    `<sip_creation_path>/<url_encoded_filepath>-scraper.json`.
    PREMIS event metadata and PREMIS agent metadata are written to
    `<sip_creation_path>/<premis_event_id>-PREMIS%3AEVENT-amd.xml` and
    `<sip_creation_path>/<premis_agent_id>-PREMIS%3AEVENT-amd.xml`.
    Import object PREMIS event metadata references are written to
    `<sip_creation_path>/import-object-md-references.jsonl`.

    The file format specific metadata is copied from metax if it is
    available. It is written to
    `<sip_creation_path>/<url_encoded_filepath>-<metadata_type>-amd.xml`,
    where <metadata_type> is NISOIMG, ADDML, AudioMD, or VideoMD.
    File format specific metadata references are written to a json-file
    depending on file format. For example, refences to NISOIMG metadata
    are written to `<sip_creation_path>/create-mix-md-references`.

    List of PREMIS event references is written to
    `<workspace>/create-technical-metadata.jsonl`

    The task requires workspace to be created, dataset metadata to be
    validated and dataset files to be downloaded.
    """

    success_message = 'Technical metadata for objects created'
    failure_message = 'Technical metadata for objects could not be created'

    def __init__(self, *args, **kwargs):
        """Initialize Task."""
        super(CreateTechnicalMetadata, self).__init__(*args, **kwargs)
        self.config_object = Configuration(self.config)
        self.metax_client = Metax(
            self.config_object.get('metax_url'),
            self.config_object.get('metax_user'),
            self.config_object.get('metax_password'),
            verify=self.config_object.getboolean('metax_ssl_verification'))

    def requires(self):
        """List the Tasks that this Task depends on.

        :returns: dictionary of required tasks
        """
        return {
            'workspace':
            CreateWorkspace(workspace=self.workspace,
                            dataset_id=self.dataset_id,
                            config=self.config),
            'validation':
            ValidateMetadata(workspace=self.workspace,
                             dataset_id=self.dataset_id,
                             config=self.config),
            'files':
            GetFiles(workspace=self.workspace,
                     dataset_id=self.dataset_id,
                     config=self.config)
        }

    def output(self):
        """Return output target of this Task.

        :returns: `<workspace>/create-technical-metadata.jsonl`
        :rtype: LocalTarget
        """
        return LocalTarget(
            os.path.join(self.workspace, 'create-technical-metadata.jsonl'))

    def run(self):
        """Create techincal metadta.

        Creates PREMIS technical metadata files and technical attribute
        files.

        :returns: ``None``
        """
        files = self.metax_client.get_dataset_files(self.dataset_id)

        # Create one timestamp for import_object events to avoid
        # creating new events each time import_object is iterated
        event_datetime = datetime.datetime.utcnow().isoformat()

        tmp = os.path.join(self.config_object.get('packaging_root'), 'tmp/')
        with TemporaryDirectory(prefix=tmp) as temporary_workspace:
            for file_ in files:

                filepath = os.path.join('dataset_files',
                                        file_['file_path'].strip('/'))

                # Create METS document that contains PREMIS metadata
                self.create_objects(file_, filepath, event_datetime,
                                    temporary_workspace)

                # Create METS documents that contain technical
                # attributes
                self.create_technical_attributes(file_, filepath,
                                                 temporary_workspace)

            # Move created files to sip creation directory. PREMIS event
            # reference file is moved to output target path after
            # everything else is done.
            with self.output().temporary_path() as target_path:
                shutil.move(
                    os.path.join(temporary_workspace,
                                 'premis-event-md-references.jsonl'),
                    target_path)
                for file_ in os.listdir(temporary_workspace):
                    shutil.move(os.path.join(temporary_workspace, file_),
                                self.sip_creation_path)

    def create_objects(self, metadata, filepath, event_datetime, output):
        """Create PREMIS metadata for file.

        Reads file metadata from Metax. Technical metadata is generated
        by siptools import_object script.

        :param metadata: file metadata dictionary
        :param filepath: file path in SIP
        :param event_datetime: the timestamp for the import_object
                               events
        :param output: output directory for import_object script
        :returns: ``None``
        """
        # Read character set if it defined for this file
        try:
            charset = metadata["file_characteristics"]["encoding"]
        except KeyError:
            charset = None

        # Read format version if it is defined for this file
        try:
            formatversion = metadata["file_characteristics"]["format_version"]
        except KeyError:
            formatversion = ""

        digest_algorithm = metadata["checksum"]["algorithm"]

        # figure out the checksum algorithm
        if digest_algorithm in ["md5", "sha2"]:
            digest_algorithm = algorithm_name(digest_algorithm,
                                              metadata["checksum"]["value"])

        # Read file creation date if it is defined for this file
        try:
            date_created = metadata["file_characteristics"]["file_created"]
        except KeyError:
            date_created = None

        # Create PREMIS file metadata XML
        siptools.scripts.import_object.import_object(
            filepaths=[filepath],
            base_path=self.workspace,
            workspace=output,
            skip_wellformed_check=True,
            file_format=(metadata["file_characteristics"]["file_format"],
                         formatversion),
            checksum=(digest_algorithm, metadata["checksum"]["value"]),
            charset=charset,
            date_created=date_created,
            event_datetime=event_datetime,
            event_target='.')

    def create_technical_attributes(self, metadata, filepath, output):
        """Create technical metadata for a file

        Create METS TechMD files for each metadata type based on previously
        scraped file characteristics

        :param file_identifier: file identifier
        :param filepath: path of file in SIP
        :param output: Path to the temporary workspace
        :returns: ``None``
        """
        creator = siptools.mdcreator.MetsSectionCreator(output)
        metadata_generator = XMLMetadataGenerator(file_path=os.path.join(
            self.input()['files'].path, metadata['file_path'].strip('/')),
                                                  file_metadata=metadata)

        md_elems = metadata_generator.create()

        for md_elem in md_elems:
            # Retrieve the wrapped MD document
            md_namespace = md_elem.nsmap[md_elem.prefix]

            mdtype = TECH_ATTR_TYPES[md_namespace]["mdtype"]
            mdtypeversion = TECH_ATTR_TYPES[md_namespace]["mdtypeversion"]
            othermdtype = TECH_ATTR_TYPES[md_namespace].get(
                "othermdtype", None)
            ref_file = TECH_ATTR_TYPES[md_namespace]["ref_file"]

            # Create METS TechMD file
            techmd_id, _ = creator.write_md(metadata=md_elem,
                                            mdtype=mdtype,
                                            mdtypeversion=mdtypeversion,
                                            othermdtype=othermdtype)

            # Add reference from fileSec to TechMD
            creator.add_reference(techmd_id, filepath)
            creator.write(ref_file=ref_file)
def test_dataset_directories_caching_works(requests_mock):
    """Test is_consistent_for_file method.

    Checks that caching of dataset directories in``DatasetConsistency``
    works and no extra calls are done to Metax. In this test dataset contains
    only one entry in dataset directories which is the root directory of the
    dataset files:

    /root_dir/second_par_dir/first_par_dir/file1
    /root_dir/second_par_dir/first_par_dir/file1

    :returns: ``None``
    """
    FILE_METADATA = {
        'file_path': "/path/to/file1",
        'parent_directory': {
            'identifier': 'first_par_dir'
        },
        "checksum": {
            "algorithm": "md5",
            "value": "foobar"
        },
        "file_characteristics": {
            "file_format": "text/csv"
        },
        "file_storage": {
            "identifier": "foobar",
            "id": 1
        }
    }
    file_1 = copy.deepcopy(FILE_METADATA)
    file_1['identifier'] = 'file_identifier1'
    file_2 = copy.deepcopy(FILE_METADATA)
    file_2['identifier'] = 'file_identifier2'

    # Init metax client
    configuration = Configuration(tests.conftest.UNIT_TEST_CONFIG_FILE)
    client = Metax(
        configuration.get('metax_url'),
        configuration.get('metax_user'),
        configuration.get('metax_password'),
        verify=configuration.getboolean('metax_ssl_verification')
    )
    dataset = {
        'identifier': 'dataset_identifier',
        'research_dataset': {
            'files': [],
            'directories': [{'identifier': 'root_dir'}]
        }
    }

    first_par_dir_adapter = requests_mock.get(
        tests.conftest.METAX_URL + '/directories/first_par_dir',
        json={
            'identifier': 'first_par_dir',
            'parent_directory': {
                'identifier': 'second_par_dir'
            }
        },
        status_code=200
    )
    second_par_dir_adapter = requests_mock.get(
        tests.conftest.METAX_URL + '/directories/second_par_dir',
        json={
            'identifier': 'second_par_dir',
            'parent_directory': {
                'identifier': 'root_dir'
            }
        },
        status_code=200
    )
    try:
        dirs = DatasetConsistency(client, dataset)
        dirs.is_consistent_for_file(file_1)
        dirs.is_consistent_for_file(file_2)
    except InvalidDatasetMetadataError as exc:
        pytest.fail(
            '_verify_file_contained_by_dataset raised exception: ' + str(exc)
        )
    # verify that dataset directory caching works. Metax is called only once
    # for the parent directories for the two files.
    assert first_par_dir_adapter.call_count == 1
    assert second_par_dir_adapter.call_count == 1