def list_files(self): success = True # Perform cleanup so that we are ready to list files self._auto_reader_lock.acquire() self._auto_read_enabled = False # Causes to read any leftover output self.read_junk() # Kills any running program and reads until prompt self.send_kill() self.read_to_next_prompt() # Send list command and read output self.ws.write("import os;os.listdir()\r\n") ret = "" try: ret = self.read_to_next_prompt() except TimeoutError: success = False self._auto_read_enabled = True self._auto_reader_lock.release() if success and ret: return re.findall("'([^']+)'", ret) else: raise OperationError()
def _remove_file_job(self, file_name, transfer, set_text): #TODO use translate (?) s = "Removing %s, please wait..." % file_name if set_text is not None: set_text(s) self._auto_reader_lock.acquire() self._auto_read_enabled = False success = False command = "os.remove" if Settings().use_transfer_scripts: #This removes both files and folders recursively command = "__upl.remove" self._send_command(command, file_name) try: self.read_to_next_prompt() success = True except TimeoutError: success = False transfer.mark_finished() self._auto_read_enabled = True self._auto_reader_lock.release() if not success: raise OperationError()
def list_file_sizes(self): filenames = self.list_files() success = True # Pause autoreader so we can receive response self._auto_reader_lock.acquire() self._auto_read_enabled = False # Stop any running script self.send_kill() # Read any leftovers self.read_junk() # Mark the start of file listing communication self.send_line("print('#fs#')") # Now we either wait for any running program to finish # or read output that it might be producing until it finally # closes and our command gets executed. ret = "" res = [] while "#fs#" not in ret: try: ret = self.read_to_next_prompt() except TimeoutError: success = False # Now we can be sure that we are ready for running commands # Send command for getting size of each file numFiles = len(filenames) progress = QProgressDialog("Retrieving file sizes...", "a", 0, numFiles) progress.setWindowModality(Qt.WindowModal) if success: for (fn, i) in zip(filenames, range(numFiles)): progress.setValue(i) print(fn) if progress.wasCanceled(): break self.send_line("uos.stat('%s')[6]" % fn) # Wait for reply try: ret = int(self.read_to_next_prompt().split('\r\n')[1]) except TimeoutError: success = False res.append(ret) self._auto_read_enabled = True self._auto_reader_lock.release() progress.setValue(numFiles) if success and res: return res else: raise OperationError()
def remove_file(self, file_name): success = True # Prevent echo self._auto_reader_lock.acquire() self._auto_read_enabled = False self.send_line("import os; os.remove(\"{}\")".format(file_name)) try: self.read_to_next_prompt() except TimeoutError: success = False self._auto_read_enabled = True self._auto_reader_lock.release() if not success: raise OperationError()
def list_files(self): success = True self._auto_reader_lock.acquire() self._auto_read_enabled = False self.read_junk() self.ws.write("import os;os.listdir()\r\n") ret = "" try: ret = self.read_to_next_prompt() except TimeoutError: success = False self._auto_read_enabled = True self._auto_reader_lock.release() if success and ret: return re.findall("'([^']+)'", ret) else: raise OperationError()
def list_statvfs(self): # Pause autoreader so we can receive response self._auto_reader_lock.acquire() self._auto_read_enabled = False output = self._send_command('os.statvfs', '/', ret=True) self._auto_read_enabled = True self._auto_reader_lock.release() if output == "": raise OperationError() else: output = output.split("\n")[1][1:-1].split(", ") output = [int(val) for val in output] self.statvfs = output return output
def list_files(self, mcu_folder="/"): # Pause autoreader so we can receive response self._auto_reader_lock.acquire() self._auto_read_enabled = False output = self._send_command("__upl.listdir", mcu_folder, ret=True) self._auto_read_enabled = True self._auto_reader_lock.release() if output == "": raise OperationError() else: if "[" in output: ret = re.findall("'([^']+)'", output) else: ret = [] return ret
def get_file_size(self, file_name): success = True file_size = 0 self._auto_reader_lock.acquire() self._auto_read_enabled = False self.send_line("import os; os.stat(\"{}\")".format(file_name)) try: res = self.read_to_next_prompt() # Skip first line which is command echo res = res[res.find("\n"):] # Strip parentheses and split to items items = res.strip("()\r\n ").split(", ") # Sixth item is file size file_size = int(items[6]) except TimeoutError: success = False self._auto_read_enabled = True self._auto_reader_lock.release() if not success: raise OperationError() return file_size
def list_files(self): success = True # Pause autoreader so we can receive response self._auto_reader_lock.acquire() self._auto_read_enabled = False # Stop any running script self.send_kill() # Read any leftovers self.read_junk() # Mark the start of file listing communication self.send_line("print('#fs#')") # Now we either wait for any running program to finish # or read output that it might be producing until it finally # closes and our command gets executed. ret = "" while "#fs#" not in ret: try: ret = self.read_to_next_prompt() except TimeoutError: success = False # Now we can be sure that we are ready for listing files # Send command for listing files if success: self.send_line("import os; os.listdir()") # Wait for reply try: ret = self.read_to_next_prompt() except TimeoutError: success = False self._auto_read_enabled = True self._auto_reader_lock.release() if success and ret: return re.findall("'([^']+)'", ret) else: raise OperationError()