def build_header_delta_from_chosen(cls, header, absolute_root, chosen_files=[]): if not isinstance(header, TaskResourceHeader): raise TypeError( "Incorrect header type: {}. Should be TaskResourceHeader". format(type(header))) cur_th = TaskResourceHeader(header.dir_name) abs_dirs = split_path(absolute_root) for file_ in chosen_files: dir_, file_name = os.path.split(file_) dirs = split_path(dir_)[len(abs_dirs):] last_header = cur_th last_ref_header = header last_header, last_ref_header, ref_header_found = cls.__resolve_dirs( dirs, last_header, last_ref_header) hsh = SimpleHash.hash_file_base64(file_) if ref_header_found: if last_ref_header.__has_file(file_name): if hsh == last_ref_header.__get_file_hash(file_name): continue last_header.files_data.append((file_name, hsh)) return cur_th
def test(self): file_path = os.path.join(self.path, 'file.txt') with open(file_path, 'wb') as out: out.write(b'The quick brown fox jumps over the lazy dog\n') b64 = b"vkF3aLXDxcHZvLLnwRkZbddrVXA=\n" self.assertEqual(b64, SimpleHash.hash_file_base64(file_path))
def buildHeaderDeltaFromHeader(cls, header, absoluteRoot): assert isinstance(header, TaskResourceHeader) curTr = TaskResourceHeader(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.subDirHeaders.append( cls.buildHeaderDeltaFromHeader( header.subDirHeaders[idx], os.path.join(absoluteRoot, d))) else: curTr.subDirHeaders.append( cls.__build(d, os.path.join(absoluteRoot, d))) for f in files: fileHash = 0 if f in [file[0] for file in header.filesData]: idx = [file[0] for file in header.filesData].index(f) fileHash = SimpleHash.hash_file_base64( os.path.join(absoluteRoot, f)) if fileHash == header.filesData[idx][1]: continue if not fileHash: fileHash = SimpleHash.hash_file_base64( os.path.join(absoluteRoot, f)) curTr.filesData.append((f, fileHash)) return curTr
def extract(self, toPath): for dir in self.subDirResources: if not os.path.exists(os.path.join(toPath, dir.dirName)): os.makedirs(os.path.join(toPath, dir.dirName)) dir.extract(os.path.join(toPath, dir.dirName)) for f in self.filesData: if not os.path.exists(os.path.join( toPath, f[0])) or SimpleHash.hash_file_base64( os.path.join(toPath, f[0])) != f[1]: self.writeFile(os.path.join(toPath, f[0]), f[2])
def extract(self, to_path): for dir_ in self.sub_dir_resources: if not os.path.exists(os.path.join(to_path, dir_.dir_name)): os.makedirs(os.path.join(to_path, dir_.dir_name)) dir_.extract(os.path.join(to_path, dir_.dir_name)) for f in self.files_data: if not os.path.exists(os.path.join( to_path, f[0])) or SimpleHash.hash_file_base64( os.path.join(to_path, f[0])) != f[1]: self.write_file(os.path.join(to_path, f[0]), f[2])
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 __build(cls, dir_name, absolute_root, chosen_files=None): cur_th = TaskResourceHeader(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)) ] files_data = [] for f in files: if chosen_files and os.path.join(absolute_root, f) not in chosen_files: continue hsh = SimpleHash.hash_file_base64(os.path.join(absolute_root, f)) files_data.append((f, hsh)) # print "{}, {}, {}".format(relative_root, absolute_root, files_data) cur_th.files_data = files_data sub_dir_headers = [] for d in dirs: child_sub_dir_header = cls.__build(d, os.path.join(absolute_root, d), chosen_files) sub_dir_headers.append(child_sub_dir_header) cur_th.sub_dir_headers = sub_dir_headers # print "{} {} {}\n".format(absolute_root, len(sub_dir_headers), len(files_data)) return cur_th
def test_fileHash(self): file_ = path.join(path.dirname(__file__), 'file.txt') b64 = "vkF3aLXDxcHZvLLnwRkZbddrVXA=\n" self.assertEquals(b64, SimpleHash.hash_file_base64(file_))