def exec_journals(self): direxists = self.__directory_exists( os.path.join(self.remotepath, self.root)) print( f'{"@"+self.hostname+" " if self.hostname else ""}Reading the Journals. Updating Remote Server Files...\n' ) ''' If the Directory does not Exist (First-Time Syncing) Create the whole Directory Tree ''' if not direxists: self.__cwd_scp(self.localpath, self.remotepath) else: ''' Read the Journal and Update the Necessary Files ''' for activity in self.__journal(): # activity : ['timestamp' ,'event' , 'src', 'dest'] src_path = activity[2] if activity[1] == 'moved': dest_path = activity[3] self.sftp_client.posix_rename( os.path.join( self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/')), os.path.join( self.remotepath, self.root, ''.join(dest_path.split(self.root, 1)[1:]).strip('/'))) elif activity[1] == 'created': dest_path = os.path.join( self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/')) if os.path.isdir(src_path): self.sftp_client.mkdir(dest_path) else: self.sftp_client.put(src_path, dest_path, callback=None, confirm=True) elif activity[1] == 'deleted': dest_path = os.path.join( self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/')) if os.path.isdir(src_path): self.sftp_client.rmdir(dest_path) else: self.sftp_client.remove(dest_path) elif activity[1] == 'modified': dest_path = os.path.join( self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/')) if os.path.isdir(src_path): pass else: if not cmp(src_path, dest_path, self.sftp_client, shallow=self.shallow_filecmp): self.sftp_client.put(src_path, dest_path, callback=None, confirm=True)
def __exec_journals(self, local_journal, workspace_name): root = self.root[workspace_name] print (f'{root}{"@"+self.ssh_client_dict["host"]} Reading the Journals. Updating Remote Server Files...\n') ''' If the Directory does not Exist (First-Time Syncing) Create the whole Directory Tree ''' ''' Read the Journal and Update the Necessary Files ''' for activity in self.__journal(local_journal, workspace_name): # activity : ['timestamp' ,'event' , 'src', 'dest', 'local'] try: src_path = activity[2] if activity[1] == 'moved': dest_path = activity[3] if self.verbose: print(f'{workspace_name}@{self.ssh_client_dict["host"]} {self.__colorize("Moved", "b")} {" Directory" if os.path.isdir(src_path) else " File"}:\n[LOCAL PATH] {src_path}\n[SERVER PATH] {dest_path}') self.sftp_client.posix_rename(os.path.join(self.remotepath, root, ''.join(src_path.split(root, 1)[1:]).strip('/')), os.path.join(self.remotepath, root, ''.join(dest_path.split(root, 1)[1:]).strip('/'))) elif activity[1] == 'created': dest_path = os.path.join(self.remotepath, root, ''.join(src_path.split(root, 1)[1:]).strip('/')) if self.verbose: print(f'{workspace_name}@{self.ssh_client_dict["host"]} {self.__colorize("Created", "g")} {" Directory" if os.path.isdir(src_path) else " File"}:\n[LOCAL PATH] {src_path}\n[SERVER PATH] {dest_path}') if os.path.isdir(src_path): self.sftp_client.mkdir(dest_path) else: self.sftp_client.put(src_path, dest_path, callback=None, confirm=True) elif activity[1] == 'deleted': dest_path = os.path.join(self.remotepath, root, ''.join(src_path.split(root, 1)[1:]).strip('/')) if self.verbose: print(f'{workspace_name}@{self.ssh_client_dict["host"]} {self.__colorize("Deleted", "r")} {" Directory" if os.path.isdir(src_path) else " File"}:\n[LOCAL PATH] {src_path}\n[SERVER PATH] {dest_path}') if os.path.isdir(src_path): self.sftp_client.rmdir(dest_path) else: self.sftp_client.remove(dest_path) elif activity[1] == 'modified': dest_path = os.path.join(self.remotepath, root, ''.join(src_path.split(root, 1)[1:]).strip('/')) if self.verbose: print(f'{workspace_name}@{self.ssh_client_dict["host"]} {self.__colorize("Modified", "y")} {" Directory" if os.path.isdir(src_path) else " File"}:\n[LOCAL PATH] {src_path}\n[SERVER PATH] {dest_path}') if os.path.isdir(src_path): pass else: if not cmp(src_path, dest_path, self.sftp_client, shallow=self.shallow_filecmp): self.sftp_client.put(src_path, dest_path, callback=None, confirm=True) except IOError as e: if e.errno == errno.ENOENT: print (f'Ommiting {" Directory" if os.path.isdir(src_path) else " File"}: {src_path}')
def on_modified(self, event): super(ServerWorkSync, self).on_modified(event) timestamp = int(time.time()) what = 'directory' if event.is_directory else 'file' if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{self.__colorize("Modified", "y")} {what}: {event.src_path}' ) try: if self.autosync: dest_path = os.path.join( self.remotepath, self.root, ''.join(event.src_path.split(self.root, 1)[1:]).strip('/')) if event.is_directory: # NOTE: idk if this event is useful for directories, so i'll leave it for future use. pass else: if not cmp(event.src_path, dest_path, self.sftp_client, shallow=self.shallow_filecmp): self.sftp_client.put(event.src_path, dest_path, callback=None, confirm=True) else: if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{what}: {event.src_path} is the Same (No need for Upload)!' ) else: rec = [timestamp, 'modified', event.src_path, ''] self.__journal(mode='w', data=rec) except FileNotFoundError: if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{what}: {event.src_path} does not Exist!' )
def __handshake(self): cnt = 0 direxists = self.__directory_exists( os.path.join(self.remotepath, self.root)) print( f'{"@"+self.hostname+" " if self.hostname else ""}Initiating Handshake. Transferring All Data to SSH Server...\n' ) if not direxists: self.__cwd_scp(self.localpath, self.remotepath) else: ''' Update the old Files; Copy (new) Files From Server to Client ''' for root, _, files in self.__remote_os_walk( os.path.join(self.remotepath, self.root)): dir_of_interest = ''.join(root.split(self.root, 1)[1:]).strip('/') server_files = [os.path.join(root, file) for file in files] if not os.path.exists( os.path.join(self.localpath, dir_of_interest)): os.makedirs(os.path.join(self.localpath, dir_of_interest)) if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{self.__colorize("Created", "g")} directory: {file}' ) for idx, file in enumerate(server_files): print(f'Synchronized {cnt} Files.', end='\r', flush=True) try: src_path = os.path.join(self.localpath, dir_of_interest, files[idx]) if not cmp(src_path, file, self.sftp_client, shallow=self.shallow_filecmp) and cmpolder( src_path, file, self.sftp_client): if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{self.__colorize("Updated", "y")} file: {file}' ) self.sftp_client.put( os.path.join(self.localpath, dir_of_interest, files[idx]), file) except IOError as e: if e.errno == errno.ENOENT: if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{self.__colorize("Downloading", "g")} file: {file}' ) self.sftp_client.get(file, src_path) cnt += 1 if not direxists: self.__cwd_scp(self.localpath, self.remotepath) else: ''' Update the old Files; Copy (new) Files From Server to Client ''' for root, _, files in self.__remote_os_walk( os.path.join(self.remotepath, self.root)): dir_of_interest = ''.join(root.split(self.root, 1)[1:]).strip('/') server_files = [os.path.join(root, file) for file in files] if not os.path.exists( os.path.join(self.localpath, dir_of_interest)): os.makedirs(os.path.join(self.localpath, dir_of_interest)) if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{self.__colorize("Created", "g")} directory: {file}' ) for idx, file in enumerate(server_files): print(f'Synchronized {cnt} Files.', end='\r', flush=True) try: src_path = os.path.join(self.localpath, dir_of_interest, files[idx]) if not cmp(src_path, file, self.sftp_client, shallow=self.shallow_filecmp) and cmpolder( src_path, file, self.sftp_client): if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{self.__colorize("Updated", "y")} file: {file}' ) self.sftp_client.put( os.path.join(self.localpath, dir_of_interest, files[idx]), file) except IOError as e: if e.errno == errno.ENOENT: if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{self.__colorize("Downloading", "g")} file: {file}' ) self.sftp_client.get(file, src_path) cnt += 1 ''' Copy the new Files (and Directories) from Client to the Server ''' for root, _, files in os.walk(os.path.abspath(self.localpath)): rel_dir_of_file = ''.join(root.split(self.root, 1)[1:]).strip('/') dir_of_interest = os.path.join(self.remotepath, self.root, rel_dir_of_file) try: self.sftp_client.stat(dir_of_interest) except IOError as e: if e.errno == errno.ENOENT: self.mkdir_p(dir_of_interest, is_dir=True) for file in files: print(f'Synchronized {cnt} Files.', end='\r', flush=True) try: self.sftp_client.stat( os.path.join(self.remotepath, self.root, rel_dir_of_file, file)) except IOError as e: if e.errno == errno.ENOENT: remote_file_path = os.path.join( self.remotepath, self.root, rel_dir_of_file, file) if self.verbose: print( f'{"@"+self.hostname+" " if self.hostname else ""}{self.__colorize("Created", "g")} file: {remote_file_path}' ) self.sftp_client.put(os.path.join(root, file), remote_file_path, callback=None, confirm=True) cnt += 1
def __exec_journals(self, local_journal): print( f'{self.root}{"@"+self.ssh_client_dict["host"]} Reading the Journals. Updating Remote Server Files...\n' ) ''' If the Directory does not Exist (First-Time Syncing) Create the whole Directory Tree ''' ''' Read the Journal and Update the Necessary Files ''' for activity in self.__journal(local_journal): # activity : ['timestamp' ,'event' , 'src', 'dest'] src_path = activity[2] if activity[1] == 'moved': dest_path = activity[3] print( f"{activity[1]} --- {os.path.join(self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/'))} --- {os.path.join(self.remotepath, self.root, ''.join(dest_path.split(self.root, 1)[1:]).strip('/'))}" ) self.sftp_client.posix_rename( os.path.join( self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/')), os.path.join( self.remotepath, self.root, ''.join(dest_path.split(self.root, 1)[1:]).strip('/'))) elif activity[1] == 'created': dest_path = os.path.join( self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/')) print(f"{activity[1]} --- {activity[2]} --- {dest_path}") if os.path.isdir(src_path): self.sftp_client.mkdir(dest_path) else: self.sftp_client.put(src_path, dest_path, callback=None, confirm=True) elif activity[1] == 'deleted': dest_path = os.path.join( self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/')) print(f"{activity[1]} --- {activity[2]} --- {dest_path}") if os.path.isdir(src_path): self.sftp_client.rmdir(dest_path) else: self.sftp_client.remove(dest_path) elif activity[1] == 'modified': dest_path = os.path.join( self.remotepath, self.root, ''.join(src_path.split(self.root, 1)[1:]).strip('/')) print(f"{activity[1]} --- {activity[2]} --- {dest_path}") if os.path.isdir(src_path): pass else: if not cmp(src_path, dest_path, self.sftp_client, shallow=self.shallow_filecmp): self.sftp_client.put(src_path, dest_path, callback=None, confirm=True)