def _check_tarfile(argname, val, hashkey, newchecksum): """Check that `argname` `val` is a tar file and that provided 'hashkey` matches with the hashkey calculated on the `val`. :arg str argname: name of the argument :arg file val: the file object :arg str hashkey: the sha256 hexdigest of the file, calculated over the tuple (name, size, mtime, uname) of all the tarball members :return: the val if the validation passes.""" # checking that is a valid file or an input string # note: the input string is generated on client side just when the input file is empty _check_file(argname, val) digest = None try: #This newchecksum param and the if/else branch is there for backward compatibility. #The parameter, older exclusion and checksum functions should be removed in the future. if newchecksum == 2: digest = calculateChecksum(val.file, exclude=NEW_USER_SANDBOX_EXCLUSIONS) elif newchecksum == 1: digest = calculateChecksum(val.file, exclude=USER_SANDBOX_EXCLUSIONS) else: tar = tarfile.open(fileobj=val.file, mode='r') lsl = [(x.name, int(x.size), int(x.mtime), x.uname) for x in tar.getmembers()] hasher = hashlib.sha256(str(lsl)) digest = hasher.hexdigest() except tarfile.ReadError: raise InvalidParameter('File is not a .tgz file.') if not digest or hashkey != digest: raise ChecksumFailed("Checksums do not match") return val
def _check_tarfile(argname, val, hashkey, newchecksum): """Check that `argname` `val` is a tar file and that provided 'hashkey` matches with the hashkey calculated on the `val`. :arg str argname: name of the argument :arg file val: the file object :arg str hashkey: the sha256 hexdigest of the file, calculated over the tuple (name, size, mtime, uname) of all the tarball members :return: the val if the validation passes.""" # checking that is a valid file or an input string # note: the input string is generated on client side just when the input file is empty _check_file(argname, val) digest = None try: #This newchecksum param and the if/else branch is there for backward compatibility. #The parameter, older exclusion and checksum functions should be removed in the future. if newchecksum == 2: digest = calculateChecksum(val.file, exclude=NEW_USER_SANDBOX_EXCLUSIONS) elif newchecksum == 1: digest = calculateChecksum(val.file, exclude=USER_SANDBOX_EXCLUSIONS) else: tar = tarfile.open(fileobj=val.file, mode='r') lsl = [(x.name, int(x.size), int(x.mtime), x.uname) for x in tar.getmembers()] hasher = hashlib.sha256(str(lsl)) digest = hasher.hexdigest() except tarfile.ReadError: raise InvalidParameter('File is not a .tgz file.') if not digest or hashkey != digest: raise ChecksumFailed("Checksums do not match") return val
def testChecksum(self): """ Tests checksum method """ checksum1 = calculateChecksum(tarfile_=path.join( getTestBase(), 'WMCore_t/Services_t/UserFileCache_t/ewv_crab_EwvAnalysis_31_111229_140959_publish.tgz' )) checksum2 = calculateChecksum(tarfile_=path.join( getTestBase(), 'WMCore_t/Services_t/UserFileCache_t/ewv_crab_EwvAnalysis_31_resubmit_111229_144319_publish.tgz' )) self.assertTrue(checksum1) self.assertTrue(checksum2) self.assertFalse(checksum1 == checksum2) self.assertRaises(IOError, calculateChecksum, **{'tarfile_': 'does_not_exist'}) return
def testChecksum(self): """ Tests checksum method """ checksum1 = calculateChecksum( tarfile_=path.join( getTestBase(), "WMCore_t/Services_t/UserFileCache_t/ewv_crab_EwvAnalysis_31_111229_140959_publish.tgz" ) ) checksum2 = calculateChecksum( tarfile_=path.join( getTestBase(), "WMCore_t/Services_t/UserFileCache_t/ewv_crab_EwvAnalysis_31_resubmit_111229_144319_publish.tgz", ) ) self.assertTrue(checksum1) self.assertTrue(checksum2) self.assertFalse(checksum1 == checksum2) self.assertRaises(IOError, calculateChecksum, **{"tarfile_": "does_not_exist"}) return