def test_get_handler(self): self.assertRaises(KeyError, chksum.get_handler, "x") self.assertEqual(self._inited_count, 1) chksum.chksum_types["x"] = 1 self.assertRaises(KeyError, chksum.get_handler, "y") chksum.chksum_types["y"] = 2 self.assertEqual(1, chksum.get_handler("x")) self.assertEqual(2, chksum.get_handler("y")) self.assertEqual(self._inited_count, 1)
def get_chf(self): try: self.chf = chksum.get_handler(self.chf_type) except KeyError: raise SkipTest( 'no handler for %s, do you need to install PyCrypto or mhash?' % (self.chf_type, ))
def _write_manifest(handle, chf, filename, chksums): """Convenient, internal method for writing manifests""" size = chksums.pop("size") handle.write("%s %s %i" % (chf.upper(), filename, size)) for chf in sorted(chksums): handle.write(" %s %s" % (chf.upper(), get_handler(chf).long2str(chksums[chf]))) handle.write('\n')
def _write(self): md5_handler = get_handler('md5') outfile = None try: outfile = self._get_fd(True) for obj in sorted(self): if obj.is_reg: s = " ".join(("obj", obj.location, md5_handler.long2str(obj.chksums["md5"]), str(int(obj.mtime)))) elif obj.is_sym: s = " ".join(("sym", obj.location, "->", obj.target, str(int(obj.mtime)))) elif obj.is_dir: s = "dir " + obj.location elif obj.is_dev: s = "dev " + obj.location elif obj.is_fifo: s = "fif " + obj.location else: raise Exception(f"unknown type {type(obj)}: {obj}") outfile.write(s + "\n") outfile.close() finally: # if atomic, it forces the update to be wiped. del outfile
def get_chf(self): try: self.chf = chksum.get_handler(self.chf_type) except KeyError: raise SkipTest( 'no handler for %s, do you need to install PyCrypto or mhash?' % (self.chf_type,))
def _get_chf_serializer(chf): if chf == 'eclassdir': return lambda data: os.path.dirname(data.path) if chf == 'mtime': return lambda data: '%.0f' % math.floor(data.mtime) # Skip the leading 0x... getter = operator.attrgetter(chf) return lambda data: get_handler(chf).long2str(getter(data))
def _parse_checksum_line(self, line): pieces = line.split() chksums = OrderedDict() uchksums = OrderedDict() for key, value in izip(pieces[::2], pieces[1::2]): key = key.lower()[:] mykey = key[0] == 'u' and key[1:] or key myvalue = get_handler(mykey).str2long(value.strip()) if key[0] == 'u': uchksums[mykey] = myvalue else: chksums[mykey] = myvalue return chksums, uchksums
def test_get_handler(self): with pytest.raises(chksum.MissingChksumHandler): chksum.get_handler("x") assert self._inited_count == 1 chksum.chksum_types["x"] = 1 with pytest.raises(chksum.MissingChksumHandler): chksum.get_handler("y") chksum.chksum_types["y"] = 2 assert chksum.get_handler("x") == 1 assert chksum.get_handler("y") == 2 assert self._inited_count == 1
def _default_serializer(chf, data): # Skip the leading 0x... getter = operator.attrgetter(chf) return get_handler(chf).long2str(getter(data))
def __init__(self, algorithm, value): self.algorithm = algorithm self.value = value self._handler = get_handler(algorithm)
def get_chf(self): try: self.chf = chksum.get_handler(self.chf_type) except chksum.MissingChksumHandler: self.chf = None