Example #1
0
    def _validate(self):
        """Check that this object's fields follow our invariants.

        """
        from hera_librarian import utils

        if '/' in self.name:
            raise ValueError('illegal file name "%s": names may not contain "/"' % self.name)

        utils.normalize_and_validate_md5(self.md5)

        if not (self.size >= 0):  # catches NaNs, just in case ...
            raise ValueError('illegal size %d of file "%s": negative' % (self.size, self.name))
Example #2
0
def test_normalize_and_validate_md5():
    """Test md5sum normalization"""
    md5sum = "d41d8cd98f00b204e9800998ecf8427e"
    # function does not do anything for text already lowercase
    assert utils.normalize_and_validate_md5(md5sum) == md5sum

    md5sum_padded = md5sum + "   "
    assert utils.normalize_and_validate_md5(md5sum_padded) == md5sum

    md5sum_upper = md5sum.upper() + "   "
    assert utils.normalize_and_validate_md5(md5sum_upper) == md5sum

    # make sure error is raised when length is incorrect
    with pytest.raises(ValueError):
        utils.normalize_and_validate_md5(md5sum[:-1])

    return
Example #3
0
    def __init__(self, name, type, obsid, source, size, md5, create_time=None):
        if create_time is None:
            # We round our times to whole seconds so that they can be
            # accurately represented as integer Unix times, just in case
            # floating-point rounding could sneak in as an issue.
            create_time = datetime.datetime.utcnow().replace(microsecond=0)

        from hera_librarian import utils
        md5 = utils.normalize_and_validate_md5(md5)

        self.name = name
        self.type = type
        self.create_time = create_time
        self.obsid = obsid
        self.source = source
        self.size = size
        self.md5 = md5
        self._validate()