def __build(cls, dirName, absoluteRoot): curTh = TaskResource(dirName) dirs = [ name for name in os.listdir(absoluteRoot) if os.path.isdir(os.path.join(absoluteRoot, name)) ] files = [ name for name in os.listdir(absoluteRoot) if os.path.isfile(os.path.join(absoluteRoot, name)) ] filesData = [] for f in files: fileData = cls.readFile(os.path.join(absoluteRoot, f)) hsh = SimpleHash.hash_base64(fileData) filesData.append((f, hsh, fileData)) #print "{}, {}, {}".format( relativeRoot, absoluteRoot, filesData ) curTh.filesData = filesData subDirResources = [] for d in dirs: childSubDirHeader = cls.__build(d, os.path.join(absoluteRoot, d)) subDirResources.append(childSubDirHeader) curTh.subDirResources = subDirResources #print "{} {} {}\n".format( absoluteRoot, len( subDirHeaders ), len( filesData ) ) return curTh
def testHash(self): ex1 = "" hex1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709" b641 = "2jmj7l5rSw0yVb/vlWAYkK/YBwk=\n" hash1 = "\xda9\xa3\xee^kK\r2U\xbf\xef\x95`\x18\x90\xaf\xd8\x07\t" ex2 = "The quick brown fox jumps over the lazy dog" hex2 = "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12" b642 = "L9ThxnotKPzthJ7hu3bnORuT6xI=\n" hash2 = "/\xd4\xe1\xc6z-(\xfc\xed\x84\x9e\xe1\xbbv\xe79\x1b\x93\xeb\x12" self.assertEquals(hash1, SimpleHash.hash(ex1)) self.assertEquals(hash2, SimpleHash.hash(ex2)) self.assertEquals(hex1, SimpleHash.hash_hex(ex1)) self.assertEquals(hex2, SimpleHash.hash_hex(ex2)) self.assertEquals(b641, SimpleHash.hash_base64(ex1)) self.assertEquals(b642, SimpleHash.hash_base64(ex2))
def build_delta_from_header(cls, header, absolute_root): if not isinstance(header, TaskResourceHeader): raise TypeError( "Incorrect header type: {}. Should be TaskResourceHeader". format(type(header))) cur_tr = TaskResource(header.dir_name) dirs = [ name for name in os.listdir(absolute_root) if os.path.isdir(os.path.join(absolute_root, name)) ] files = [ name for name in os.listdir(absolute_root) if os.path.isfile(os.path.join(absolute_root, name)) ] for d in dirs: if d in [sdh.dir_name for sdh in header.sub_dir_headers]: idx = [sdh.dir_name for sdh in header.sub_dir_headers].index(d) cur_tr.sub_dir_resources.append( cls.build_delta_from_header(header.sub_dir_headers[idx], os.path.join(absolute_root, d))) else: cur_tr.sub_dir_resources.append( cls.__build(d, os.path.join(absolute_root, d))) for f in files: if f in [file_[0] for file_ in header.files_data]: idx = [file_[0] for file_ in header.files_data].index(f) if SimpleHash.hash_file_base64(os.path.join( absolute_root, f)) == header.files_data[idx][1]: continue fdata = cls.read_file(os.path.join(absolute_root, f)) if fdata is None: return None cur_tr.files_data.append((f, SimpleHash.hash_base64(fdata), fdata)) return cur_tr
def buildDeltaFromHeader(cls, header, absoluteRoot): assert isinstance(header, TaskResourceHeader) curTr = TaskResource(header.dirName) dirs = [ name for name in os.listdir(absoluteRoot) if os.path.isdir(os.path.join(absoluteRoot, name)) ] files = [ name for name in os.listdir(absoluteRoot) if os.path.isfile(os.path.join(absoluteRoot, name)) ] for d in dirs: if d in [sdh.dirName for sdh in header.subDirHeaders]: idx = [sdh.dirName for sdh in header.subDirHeaders].index(d) curTr.subDirResources.append( cls.buildDeltaFromHeader(header.subDirHeaders[idx], os.path.join(absoluteRoot, d))) else: curTr.subDirResources.append( cls.__build(d, os.path.join(absoluteRoot, d))) for f in files: if f in [file[0] for file in header.filesData]: idx = [file[0] for file in header.filesData].index(f) if SimpleHash.hash_file_base64(os.path.join( absoluteRoot, f)) == header.filesData[idx][1]: continue fdata = cls.readFile(os.path.join(absoluteRoot, f)) if fdata is None: return None curTr.filesData.append((f, SimpleHash.hash_base64(fdata), fdata)) return curTr
def hash(self): return SimpleHash.hash_base64(self.toString())
def hash(self): return SimpleHash.hash_base64(self.to_string().encode('utf-8'))