def rm(self, path, job_name=None, log=True, wait=False, **kwargs): # Delete file from file system # Log the transfer unless otherwise specified # Create prefix object _prefix_path = StoragePrefix(path) try: if _prefix_path.exists(): _prefix_path.delete() except: logging.error(f"Unable to delete path: {path}") raise
def get_file_size(self, path, job_name=None, **kwargs): # Ignore local paths if self.__get_file_protocol(path) == "Local": logging.warning(f"Ignoring path '{path}' as it is local on the disk image. Assuming the path is present!") return True try: # Check if path is prefix, and create StoragePrefix object and get its size if path.endswith("*"): _size = StoragePrefix(path.rstrip("*")).size # Check if it path exists as a file or folder, by creating StorageFile and StorageFolder object else: _file = StorageFile(path) _folder = StorageFolder(path) if _file.exists(): _size = _file.size elif _folder.exists(): _size = _folder.size else: _size = 0 # Convert to GB return float(_size)/2**30 except BaseException as e: logging.error(f"Unable to get file size: {path}") if str(e) != "": logging.error(f"Received the following msg:\n{e}") raise
def path_exists(self, path, job_name=None, **kwargs): # Ignore local paths if self.__get_file_protocol(path) == "Local": logging.warning(f"Ignoring path '{path}' as it is local on the disk image. Assuming the path is present!") return True try: logging.debug(f"Checking existence of {path}...") # Check if path is prefix, and create StoragePrefix object and check if exists if path.endswith("*"): return StoragePrefix(path.rstrip("*")).exists() # Check if it exists as a file or folder, by creating StorageFile and StorageFolder object return StorageFile(path).exists() or StorageFolder(path).exists() except RuntimeError as e: traceback.print_exc() if str(e) != "": logging.error(f"StorageHelper error for {job_name}:\n{e}") return False except: traceback.print_exc() logging.error(f"Unable to check path existence: {path}") raise
def get_file_size(self, path, job_name=None, **kwargs): retry_count = kwargs.get("retry_count", 0) # Ignore local paths if self.__get_file_protocol(path) == "Local": logging.warning(f"Ignoring path '{path}' as it is local on the disk image. Assuming the path is present!") return True if retry_count < 5: try: # Check if path is prefix, and create StoragePrefix object and get its size if path.endswith("*"): _size = StoragePrefix(path.rstrip("*")).size # Check if it path exists as a file or folder, by creating StorageFile and StorageFolder object else: _file = StorageFile(path) _folder = StorageFolder(path) _size = 0 found = False trial_count = 0 while not found: if trial_count > 10: logging.error(f"Cannot get size of '{path}' as it doesn't exist after multiple trials!") break time.sleep(trial_count) if _file.exists(): _size = _file.size found = True elif _folder.exists(): _size = _folder.size found = True else: trial_count += 1 logging.warning(f"Cannot get size of '{path}' as it does not exist! Trial {trial_count}/10") # Convert to GB return float(_size)/2**30 except BaseException as e: logging.error(f"Unable to get file size: {path}") if str(e) != "": logging.error(f"Received the following msg:\n{e}") if "dictionary changed size" in str(e): kwargs['retry_count'] = retry_count + 1 return self.get_file_size(path, job_name, **kwargs) raise else: logging.warning(f"Failed to get size of '{path}'! Attempted to retrieve size {retry_count + 1} times.") return 0
def test_prefix(self): storage_prefix = StoragePrefix( os.path.join(self.TEST_ROOT, "test_folder")) self.assertEqual(len(storage_prefix.objects), 3)