def check_if_dir(self, path): """ Checks if the remote path is a directory """ stdin, stdout, stderr = self.client.exec_command('isdir {}'.format(path)) response = escape(stdout.readline().rstrip('\n')) if response == 'True': return True return False
def remote_completion_list(self, relative_path): """ Get the matching completion for a relative path on the remote machine """ # Normalize path if relative_path.startswith('~') or relative_path.startswith('/'): path = os.path.expanduser(relative_path) os.path.join(self.remote_directory_root, relative_path) else: path = relative_path path = os.path.normpath(path) if not self.check_if_dir(path): path = os.path.dirname(path) dirname = os.path.dirname(relative_path) # If the User uses '/' on filenames, we need to get the real directory # This is needed to handle wrong input while not self.check_if_dir( dirname) and dirname != './' and dirname != '/': dirname = os.path.dirname(dirname) basename = os.path.basename(relative_path) else: dirname = relative_path basename = '' stdin, stdout, stderr = self.client.exec_command('ls {}'.format( escape(path))) completion = [] for line in stdout.readlines(): if line.startswith(basename): completion.append(os.path.join(dirname, line.rstrip())) self.completion_list = completion
def remote_completion_list(self, relative_path): """ Get the matching completion for a relative path on the remote machine """ # Normalize path if relative_path.startswith('~') or relative_path.startswith('/'): path = os.path.expanduser(relative_path) os.path.join(self.remote_directory_root, relative_path) else: path = relative_path path = os.path.normpath(path) if not self.check_if_dir(path): path = os.path.dirname(path) dirname = os.path.dirname(relative_path) # If the User uses '/' on filenames, we need to get the real directory # This is needed to handle wrong input while not self.check_if_dir(dirname) and dirname != './' and dirname != '/': dirname = os.path.dirname(dirname) basename = os.path.basename(relative_path) else: dirname = relative_path basename = '' stdin, stdout, stderr = self.client.exec_command('ls {}'.format(escape(path))) completion = [] for line in stdout.readlines(): if line.startswith(basename): completion.append(os.path.join(dirname, line.rstrip())) self.completion_list = completion
def __init__(self, client, rsync, terminal): self.rsync = rsync self.client = client self.terminal = terminal stdin, stdout, stderr = client.exec_command('cd .') self.current_path = escape(stdout.readline().rstrip('\n'))
def check_if_dir(self, path): """ Checks if the remote path is a directory """ stdin, stdout, stderr = self.client.exec_command( 'isdir {}'.format(path)) response = escape(stdout.readline().rstrip('\n')) if response == 'True': return True return False
def next(self): """ Returns the next completion suggestion """ if len(self.completion_list) > 0: program, args = parser(self.completion_buffer) if 'target' in args and args['target'] != '.': args['target'] = escape(self.completionPath + self.completion_list[self.completion_index]) else: args['path'][-1] = escape(self.completion_list[self.completion_index]) buffer = assemble(program, args) else: buffer = self.completion_buffer # Increment completion index self.completion_index += 1 if self.completion_index >= len(self.completion_list): self.completion_index = 0 return buffer
def __init__(self, client, terminal): self.client = client self.terminal = terminal self.completion_list = [] self.completion_index = 0 self.completion_buffer = None self.completionPath = '' stdin, stdout, stderr = client.exec_command('cd .') self.remote_directory_root = escape(stdout.readline().rstrip('\n'))
def next(self): """ Returns the next completion suggestion """ if len(self.completion_list) > 0: program, args = parser(self.completion_buffer) if 'target' in args and args['target'] != '.': args['target'] = escape( self.completionPath + self.completion_list[self.completion_index]) else: args['path'][-1] = escape( self.completion_list[self.completion_index]) buffer = assemble(program, args) else: buffer = self.completion_buffer # Increment completion index self.completion_index += 1 if self.completion_index >= len(self.completion_list): self.completion_index = 0 return buffer
def interpret(self, command): command = command.lstrip() # Parsing input, getting actual command name and arguments try: program, args = parser(command) except ArgumentParserError as error: self.terminal.add_lines(format_parse_error(error, command)) return True if program == 'ls': compiled_paths = map( lambda path: os.path.join(self.current_path, path), args['path']) args['path'] = list(compiled_paths) # Send command to server stdin, stdout, stderr = self.client.exec_command(ls_assemble(args)) # Print response for line in stdout.readlines(): self.terminal.add_line(line.rstrip('\n')) elif program == 'cd': # Compute path targetPath = os.path.join(self.current_path, args['path'][0]) args['path'] = [targetPath] # Send command to server stdin, stdout, stderr = self.client.exec_command(cd_assemble(args)) errors = [] for line in stderr.readlines(): errors.append(line.rstrip('\n')) if not len(errors) > 1: # Save current position self.current_path = escape(stdout.readline().rstrip('\n')) else: # Print response self.terminal.add_lines(errors) elif program == 'push': rsync_args = ['rsync'] rsync_args.append('--recursive') rsync_args.append('--partial') rsync_args.append('--perms') rsync_args.append('--times') rsync_args.append('--links') rsync_args.append('--info=progress2') rsync_args += args['files'] target_path = '{}:{}'.format( self.rsync[0], os.path.join(self.current_path, args['target'])) rsync_args.append(target_path) rsync_process = subprocess.Popen(rsync_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Add a line in the terminal self.lines. We need this for update_last_lines to work self.terminal.add_line('') # Continously pass output to terminal, abort rsync process if ctrl-c occurs try: no_error = True while rsync_process.poll() is None and no_error: line = rsync_process.stdout.readline() line = line.rstrip() line = line.decode('utf-8') if line != '': self.terminal.update_last_lines([line]) no_error = rsync_error_check(line) stdout, stderr = rsync_process.communicate() except KeyboardInterrupt: self.terminal.add_line( 'Terminating rsync process, please wait') rsync_process.terminate() rsync_process.communicate() self.terminal.update_last_lines(['Rsync process terminated']) elif program == 'get': rsync_args = ['rsync'] # rsync_args.append("-e 'ssh -p {}'".format(self.rsync[1])) rsync_args.append('--recursive') rsync_args.append('--partial') rsync_args.append('--perms') rsync_args.append('--times') rsync_args.append('--links') rsync_args.append('--info=progress2') compiled_paths = map( lambda path: '{}:{}'.format( self.rsync[0], os.path.join(self.current_path, path)), args['path']) rsync_args += list(compiled_paths) rsync_args.append(os.path.expanduser(args['target'])) rsync_process = subprocess.Popen(rsync_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Add a line in the terminal self.lines. We need this for update_last_lines to work self.terminal.add_line('') # Continously pass output to terminal, abort rsync process if ctrl-c occurs try: no_error = True while rsync_process.poll() is None and no_error: line = rsync_process.stdout.readline() line = line.rstrip() line = line.decode('utf-8') if line != '': self.terminal.update_last_lines([line]) no_error = rsync_error_check(line) stdout, stderr = rsync_process.communicate() except KeyboardInterrupt: self.terminal.add_line( 'Terminating rsync process, please wait') rsync_process.terminate() rsync_process.communicate() self.terminal.update_last_lines(['Rsync process terminated']) elif program == 'exit': return False else: self.terminal.add_line('Invalid Command. Valid Commands are:') self.terminal.add_line( 'ls [path], cd [path], get [-r] [path], exit') return True
def interpret(self, command): command = command.lstrip() # Parsing input, getting actual command name and arguments try: program, args = parser(command) except ArgumentParserError as error: self.terminal.add_lines(format_parse_error(error, command)) return True if program == 'ls': compiled_paths = map(lambda path: os.path.join(self.current_path, path), args['path']) args['path'] = list(compiled_paths) # Send command to server stdin, stdout, stderr = self.client.exec_command(ls_assemble(args)) # Print response for line in stdout.readlines(): self.terminal.add_line(line.rstrip('\n')) elif program == 'cd': # Compute path targetPath = os.path.join(self.current_path, args['path'][0]) args['path'] = [targetPath] # Send command to server stdin, stdout, stderr = self.client.exec_command(cd_assemble(args)) errors = [] for line in stderr.readlines(): errors.append(line.rstrip('\n')) if not len(errors) > 1: # Save current position self.current_path = escape(stdout.readline().rstrip('\n')) else: # Print response self.terminal.add_lines(errors) elif program == 'push': rsync_args = ['rsync'] rsync_args.append('--recursive') rsync_args.append('--partial') rsync_args.append('--perms') rsync_args.append('--times') rsync_args.append('--links') rsync_args.append('--info=progress2') rsync_args += args['files'] target_path = '{}:{}'.format(self.rsync[0], os.path.join(self.current_path, args['target'])) rsync_args.append(target_path) rsync_process = subprocess.Popen(rsync_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Add a line in the terminal self.lines. We need this for update_last_lines to work self.terminal.add_line('') # Continously pass output to terminal, abort rsync process if ctrl-c occurs try: no_error = True while rsync_process.poll() is None and no_error: line = rsync_process.stdout.readline() line = line.rstrip() line = line.decode('utf-8') if line != '': self.terminal.update_last_lines([line]) no_error = rsync_error_check(line) stdout, stderr = rsync_process.communicate() except KeyboardInterrupt: self.terminal.add_line('Terminating rsync process, please wait') rsync_process.terminate() rsync_process.communicate() self.terminal.update_last_lines(['Rsync process terminated']) elif program == 'get': rsync_args = ['rsync'] # rsync_args.append("-e 'ssh -p {}'".format(self.rsync[1])) rsync_args.append('--recursive') rsync_args.append('--partial') rsync_args.append('--perms') rsync_args.append('--times') rsync_args.append('--links') rsync_args.append('--info=progress2') compiled_paths = map(lambda path: '{}:{}'.format(self.rsync[0], os.path.join(self.current_path, path)), args['path']) rsync_args += list(compiled_paths) rsync_args.append(os.path.expanduser(args['target'])) rsync_process = subprocess.Popen(rsync_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Add a line in the terminal self.lines. We need this for update_last_lines to work self.terminal.add_line('') # Continously pass output to terminal, abort rsync process if ctrl-c occurs try: no_error = True while rsync_process.poll() is None and no_error: line = rsync_process.stdout.readline() line = line.rstrip() line = line.decode('utf-8') if line != '': self.terminal.update_last_lines([line]) no_error = rsync_error_check(line) stdout, stderr = rsync_process.communicate() except KeyboardInterrupt: self.terminal.add_line('Terminating rsync process, please wait') rsync_process.terminate() rsync_process.communicate() self.terminal.update_last_lines(['Rsync process terminated']) elif program == 'exit': return False else: self.terminal.add_line('Invalid Command. Valid Commands are:') self.terminal.add_line('ls [path], cd [path], get [-r] [path], exit') return True