Ejemplo n.º 1
0
def test_hexdigest(filepath, extra_hash, algorithm, expected_hash):
    """Test that hexdigest returns correct sha1 and MD5 hashes."""
    if algorithm is None:
        assert hexdigest(filepath, extra_hash=extra_hash) == expected_hash
    else:
        assert hexdigest(filepath, algorithm=algorithm,
                         extra_hash=extra_hash) == expected_hash
Ejemplo n.º 2
0
def test_hexdigest(filepath, extra_hash, algorithm, expected_hash):
    """
    Test that hexdigest returns correct sha1 and MD5 hashes.

    :filepath: Test file name
    :extra_hash: Hash of depending file(s), or None if no such dependency
    :algorithm: Hash algorithm
    :expected_hash: Expected hash result
    """
    if algorithm is None:
        assert hexdigest(filepath, extra_hash=extra_hash) == expected_hash
    else:
        assert hexdigest(filepath, algorithm=algorithm,
                         extra_hash=extra_hash) == expected_hash
Ejemplo n.º 3
0
def calculate_md5(file_path):
    """
    This function calculates md5sum for a file.
    :file_path: path of file from which the md5sum is calculated.
    :returns: a string with md5-hexdigest.
    """
    return hexdigest(file_path, algorithm='md5')
Ejemplo n.º 4
0
    def checksum(self, algorithm="MD5"):
        """
        Return the checksum of the file with given algorithm.

        :algorithm: MD5 or SHA variant
        :returns: Calculated checksum
        """
        return hexdigest(self.filename, algorithm)
Ejemplo n.º 5
0
def check_checksums(mets_path):
    """Check checksums for all digital objects in METS

    :mets_path: Path to mets
    :returns: Iterable containing all error messages

    """

    checked_files = {}

    if os.path.isdir(mets_path):
        mets_path = os.path.join(mets_path, 'mets.xml')

    sip_path = os.path.dirname(mets_path)

    def _message(metadata_info, message):
        """Format error message"""
        return ensure_text("%s: %s" % (
            message, os.path.relpath(metadata_info["filename"], sip_path)))

    mets_tree = u.readfile(mets_path)
    for metadata_info in iter_metadata_info(mets_tree, mets_path):

        checked_files[metadata_info["filename"]] = None

        if metadata_info['algorithm'] is None:
            yield _message(metadata_info, "Could not find checksum algorithm")
        else:

            try:
                hex_digest = hexdigest(metadata_info['filename'],
                                       metadata_info['algorithm'])
            except IOError as exception:
                if exception.errno == errno.ENOENT:
                    yield _message(metadata_info, "File does not exist")
                continue

            if hex_digest == metadata_info["digest"]:
                print(_message(metadata_info, "Checksum OK"))
            else:
                yield _message(metadata_info, "Invalid Checksum")

    for path in iter_files(sip_path):
        if path.endswith("ignore_validation_errors"):
            continue

        if path not in checked_files:
            yield _message({'filename': path}, "Nonlisted file")
Ejemplo n.º 6
0
    def _generate_xslt_filename(self):
        """
        Generate XSLT filename from schematron file.

        :returns: XSLT filename
        """
        try:
            os.makedirs(self._cachepath)
        except OSError:
            if not os.path.isdir(self._cachepath):
                raise
        extra = ''
        if self._verbose:
            extra = 'verbose'
        if self._extra_hash is not None:
            extra = "%s%s" % (extra, self._extra_hash)
        schema_digest = hexdigest(self._schematron_file, extra_hash=extra)
        schema_basename = os.path.basename(self._schematron_file)

        return os.path.join(self._cachepath, '%s.%s.validator.xsl' % (
            schema_basename, schema_digest))