def main(): """Main function for specchio Example: specchio test/ user@host:test/ :return: None """ init_logger() if os.popen("whereis ssh").read() == "": return logger.error("Specchio need `ssh`, " "but there is no `ssh` in the system") if os.popen("whereis rsync").read() == "": return logger.error("Specchio need `rsync`, " "but there is no `rsync` in the system") if len(sys.argv) == 3: src_path = sys.argv[1].strip() dst_ssh, dst_path = sys.argv[2].strip().split(":") logger.info("Initialize Specchio") event_handler = SpecchioEventHandler( src_path=src_path, dst_ssh=dst_ssh, dst_path=dst_path ) observer = Observer() observer.schedule(event_handler, src_path, recursive=True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() logger.info("Specchio stopped, have a nice day :)") else: print """Usage: specchio src/ user@host:dst/"""
def main(): """Main function for specchio Example: specchio test/ user@host:test/ :return: None """ if len(sys.argv) == 3: src_path = sys.argv[1].strip() dst_ssh, dst_path = sys.argv[2].strip().split(":") init_logger() logger.info("Initialize Specchio") event_handler = SpecchioEventHandler( src_path=src_path, dst_ssh=dst_ssh, dst_path=dst_path ) observer = Observer() observer.schedule(event_handler, src_path, recursive=True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() else: print """Usage: specchio src/ user@host:dst/"""
def init_gitignore(self, src_path): logger.info("Loading ignore pattern from all `.gitignore`") gitignore_list = walk_get_gitignore(src_path) self.gitignore_dict = get_all_re(gitignore_list) # Match file or folder from the nearest `.gitignore` _gitignore_list = sorted(self.gitignore_dict.keys())[::-1] for index in range(len(_gitignore_list)): # Change '/test/.gitignore' to '/test/' _gitignore_list[index] = _gitignore_list[index][:-10] self.gitignore_list = _gitignore_list logger.info("All ignore pattern has been loaded")
def on_moved(self, event): isdir = isinstance(event, DirMovedEvent) abs_src_src_path = os.path.abspath(event.src_path) abs_src_dst_path = os.path.abspath(event.dest_path) src_ignore_tag, dst_ignore_tag = (self.is_ignore( abs_src_src_path, isdir), self.is_ignore(abs_src_dst_path, isdir)) relative_src_path = self.get_relative_src_path(event.src_path) relative_dst_path = self.get_relative_src_path(event.dest_path) dst_src_path = os.path.join(self.dst_path, relative_src_path) dst_dst_path = os.path.join(self.dst_path, relative_dst_path) if src_ignore_tag and dst_ignore_tag: return elif dst_ignore_tag: remote_rm(dst_ssh=self.dst_ssh, dst_path=dst_src_path) logger.info("Remove {} remotely".format(dst_src_path)) elif src_ignore_tag: dst_folder_path = dst_dst_path[:-len(dst_dst_path.split("/")[-1])] remote_create_folder(dst_ssh=self.dst_ssh, dst_path=dst_folder_path) rsync(dst_ssh=self.dst_ssh, src_path=abs_src_dst_path, dst_path=dst_dst_path) logger.info("Rsync {} remotely".format(dst_dst_path)) else: remote_mv(dst_ssh=self.dst_ssh, src_path=dst_src_path, dst_path=dst_dst_path) logger.info("Move {} to {} remotely".format( dst_src_path, dst_dst_path)) logger.info("Because of move method, try to update all ignore pattern") self.init_gitignore(self.src_path)
def on_moved(self, event): isdir = isinstance(event, DirMovedEvent) abs_src_src_path = os.path.abspath(event.src_path) abs_src_dst_path = os.path.abspath(event.dest_path) src_ignore_tag, dst_ignore_tag = ( self.is_ignore(abs_src_src_path, isdir), self.is_ignore(abs_src_dst_path, isdir) ) relative_src_path = self.get_relative_src_path(event.src_path) relative_dst_path = self.get_relative_src_path(event.dest_path) dst_src_path = os.path.join(self.dst_path, relative_src_path) dst_dst_path = os.path.join(self.dst_path, relative_dst_path) if src_ignore_tag and dst_ignore_tag: return elif dst_ignore_tag: remote_rm(dst_ssh=self.dst_ssh, dst_path=dst_src_path) logger.info("Remove {} remotely".format(dst_src_path)) elif src_ignore_tag: dst_folder_path = dst_dst_path[:-len(dst_dst_path.split("/")[-1])] remote_create_folder(dst_ssh=self.dst_ssh, dst_path=dst_folder_path) rsync(dst_ssh=self.dst_ssh, src_path=abs_src_dst_path, dst_path=dst_dst_path) logger.info("Rsync {} remotely".format(dst_dst_path)) else: remote_mv(dst_ssh=self.dst_ssh, src_path=dst_src_path, dst_path=dst_dst_path) logger.info("Move {} to {} remotely".format( dst_src_path, dst_dst_path )) logger.info("Because of move method, try to update all ignore pattern") self.init_gitignore(self.src_path)
def on_deleted(self, event): abs_src_path = os.path.abspath(event.src_path) isdir = isinstance(event, DirDeletedEvent) if self.is_ignore(abs_src_path, isdir): return relative_path = self.get_relative_src_path(event.src_path) dst_path = os.path.join(self.dst_path, relative_path) # If the file is `.gitignore`, remove this `gitignore` in dict and list if dst_path.split("/")[-1] == ".gitignore": logger.info( "Remove some ignore pattern, because changed " "file({}) named `.gitignore` locally".format(abs_src_path)) self.del_gitignore(abs_src_path) logger.info("Remove {} remotely".format(dst_path)) remote_rm(dst_ssh=self.dst_ssh, dst_path=dst_path)
def on_deleted(self, event): abs_src_path = os.path.abspath(event.src_path) if self.is_ignore(abs_src_path): return relative_path = self.get_relative_src_path(event.src_path) dst_path = os.path.join(self.dst_path, relative_path) # If the file is `.gitignore`, remove this `gitignore` in dict and list if dst_path.split("/")[-1] == ".gitignore": logger.info("Remove some ignore pattern, because changed " "file({}) named `.gitignore` locally".format( abs_src_path )) self.del_gitignore(abs_src_path) logger.info("Remove {} remotely".format(dst_path)) remote_rm(dst_ssh=self.dst_ssh, dst_path=dst_path)
def main(): """Main function for specchio Example: specchio test/ user@host:test/ :return: None """ init_logger() _popen_str = os.popen("whereis ssh").read().strip() if _popen_str == "" or _popen_str == "ssh:": return logger.error("Specchio need `ssh`, " "but there is no `ssh` in the system") _popen_str = os.popen("whereis rsync").read().strip() if _popen_str == "" or _popen_str == "rsync:": return logger.error("Specchio need `rsync`, " "but there is no `rsync` in the system") if len(sys.argv) >= 3: src_path = sys.argv[-2].strip() dst_ssh, dst_path = sys.argv[-1].strip().split(":") option_valid = all((option in GENERAL_OPTIONS) for option in sys.argv[1:-2]) if option_valid: logger.info("Initialize Specchio") is_init_remote = "--init-remote" in sys.argv[1:-2] event_handler = SpecchioEventHandler( src_path=src_path, dst_ssh=dst_ssh, dst_path=dst_path, is_init_remote=is_init_remote ) observer = Observer() observer.schedule(event_handler, src_path, recursive=True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() logger.info("Specchio stopped, have a nice day :)") else: print MANUAL else: print MANUAL
def main(): """Main function for specchio Example: specchio test/ user@host:test/ :return: None """ init_logger() _popen_str = os.popen("whereis ssh").read().strip() if _popen_str == "" or _popen_str == "ssh:": return logger.error("Specchio need `ssh`, " "but there is no `ssh` in the system") _popen_str = os.popen("whereis rsync").read().strip() if _popen_str == "" or _popen_str == "rsync:": return logger.error("Specchio need `rsync`, " "but there is no `rsync` in the system") if len(sys.argv) >= 3: src_path = sys.argv[-2].strip() dst_ssh, dst_path = sys.argv[-1].strip().split(":") option_valid = all((option in GENERAL_OPTIONS) for option in sys.argv[1:-2]) if option_valid: logger.info("Initialize Specchio") is_init_remote = "--init-remote" in sys.argv[1:-2] event_handler = SpecchioEventHandler( src_path=src_path, dst_ssh=dst_ssh, dst_path=dst_path, is_init_remote=is_init_remote ) observer = Observer() observer.schedule(event_handler, src_path, recursive=True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() logger.info("Specchio stopped, have a nice day :)") else: print(MANUAL) else: print(MANUAL)
def __init__(self, src_path, dst_ssh, dst_path, is_init_remote=False): """Constructor of `SpecchioEventHandler` :param src_path: str -- source path :param dst_ssh: str -- user name and host name of destination path just like: user@host :param dst_path: str -- destination path :param is_init_remote: bool -- initialize the file remotely or not :return: None """ self.init_gitignore(src_path) self.src_path = src_path self.dst_ssh = dst_ssh self.dst_path = dst_path self.git_path = os.path.join(os.path.abspath(self.src_path), ".git/") super(SpecchioEventHandler, self).__init__() if is_init_remote: logger.info("Starting to initialize the file remotely first") self.init_remote() logger.info("Initialization of the remote file has been done")
def on_modified(self, event): abs_src_path = os.path.abspath(event.src_path) if self.is_ignore(abs_src_path): return if isinstance(event, FileModifiedEvent): relative_path = self.get_relative_src_path(event.src_path) dst_path = os.path.join(self.dst_path, relative_path) # Create folder of file dst_folder_path = dst_path[:-len(dst_path.split("/")[-1])] # If the file is `.gitignore`, update gitignore dict and list if dst_path.split("/")[-1] == ".gitignore": logger.info("Update ignore pattern, because changed " "file({}) named `.gitignore` locally".format( abs_src_path )) self.update_gitignore(abs_src_path) remote_create_folder(dst_ssh=self.dst_ssh, dst_path=dst_folder_path) logger.info("Rsync {} remotely".format(dst_path)) rsync(dst_ssh=self.dst_ssh, src_path=abs_src_path, dst_path=dst_path)
def on_created(self, event): abs_src_path = os.path.abspath(event.src_path) isdir = isinstance(event, DirCreatedEvent) if self.is_ignore(abs_src_path, isdir): return relative_path = self.get_relative_src_path(event.src_path) dst_path = os.path.join(self.dst_path, relative_path) if isinstance(event, DirCreatedEvent): logger.info("Create {} remotely".format(dst_path)) remote_create_folder(dst_ssh=self.dst_ssh, dst_path=dst_path) else: dst_path = os.path.join(self.dst_path, relative_path) # Create folder of file dst_folder_path = dst_path[:-len(dst_path.split("/")[-1])] remote_create_folder(dst_ssh=self.dst_ssh, dst_path=dst_folder_path) logger.info("Rsync {} remotely".format(dst_path)) rsync(dst_ssh=self.dst_ssh, src_path=abs_src_path, dst_path=dst_path) if dst_path.split("/")[-1] == ".gitignore": logger.info( "Update ignore pattern, because changed " "file({}) named `.gitignore` locally".format(abs_src_path)) self.update_gitignore(abs_src_path)
def on_created(self, event): abs_src_path = os.path.abspath(event.src_path) isdir = isinstance(event, DirCreatedEvent) if self.is_ignore(abs_src_path, isdir): return relative_path = self.get_relative_src_path(event.src_path) dst_path = os.path.join(self.dst_path, relative_path) if isinstance(event, DirCreatedEvent): logger.info("Create {} remotely".format(dst_path)) remote_create_folder(dst_ssh=self.dst_ssh, dst_path=dst_path) else: dst_path = os.path.join(self.dst_path, relative_path) # Create folder of file dst_folder_path = dst_path[:-len(dst_path.split("/")[-1])] remote_create_folder(dst_ssh=self.dst_ssh, dst_path=dst_folder_path) logger.info("Rsync {} remotely".format(dst_path)) rsync(dst_ssh=self.dst_ssh, src_path=abs_src_path, dst_path=dst_path) if dst_path.split("/")[-1] == ".gitignore": logger.info("Update ignore pattern, because changed " "file({}) named `.gitignore` locally".format( abs_src_path )) self.update_gitignore(abs_src_path)
def init_gitignore(self, src_path): logger.info("Loading ignore pattern from all `.gitignore`") gitignore_list = walk_get_gitignore(src_path) self.gitignore_dict = get_all_re(gitignore_list) # Match file or folder from the nearest `.gitignore` _gitignore_list = sorted(self.gitignore_dict.keys())[::-1] for index in range(len(_gitignore_list)): # Change '/test/.gitignore' to '/test/' _gitignore_list[index] = _gitignore_list[index][:-10] self.gitignore_list = _gitignore_list logger.info(self.gitignore_list) logger.info("All ignore pattern has been loaded")