def __enter__(self):
    if self.allocate:
        if (self.get_disk_freespace('/host') < (max(SWAPAllocator.DISK_FREESPACE_THRESHOLD, self.swap_mem_size) * SWAPAllocator.MiB_TO_BYTES_FACTOR)):
            echo_and_log('Failed to setup SWAP memory due to insufficient disk free space...', LOG_ERR)
            return
        meminfo = self.read_from_meminfo()
        mem_total_in_bytes = (meminfo['MemTotal'] * SWAPAllocator.KiB_TO_BYTES_FACTOR)
        mem_avail_in_bytes = (meminfo['MemAvailable'] * SWAPAllocator.KiB_TO_BYTES_FACTOR)
        if ((mem_total_in_bytes < (self.total_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR)) or (mem_avail_in_bytes < (self.available_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR))):
            echo_and_log('Setup SWAP memory')
            swapfile = SWAPAllocator.SWAP_FILE_PATH
            if os.path.exists(swapfile):
                self.remove_swapmem()
            try:
                self.setup_swapmem()
            except Exception:
                self.remove_swapmem()
                raise
            self.is_allocated = True
def __enter__(self):
    if self.allocate:
        if (self.get_disk_freespace('/host') < (max(SWAPAllocator.DISK_FREESPACE_THRESHOLD, self.swap_mem_size) * SWAPAllocator.MiB_TO_BYTES_FACTOR)):
            echo_and_log('Failed to setup SWAP memory due to insufficient disk free space...', LOG_ERR)
            return
        meminfo = self.read_from_meminfo()
        mem_total_in_bytes = (meminfo['MemTotal'] * SWAPAllocator.KiB_TO_BYTES_FACTOR)
        mem_avail_in_bytes = (meminfo['MemAvailable'] * SWAPAllocator.KiB_TO_BYTES_FACTOR)
        if ((mem_total_in_bytes < (self.total_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR)) or (mem_avail_in_bytes < (self.available_mem_threshold * SWAPAllocator.MiB_TO_BYTES_FACTOR))):
            echo_and_log('Setup SWAP memory')
            swapfile = SWAPAllocator.SWAP_FILE_PATH
            if os.path.exists(swapfile):
                self.remove_swapmem()
            try:
                self.setup_swapmem()
            except Exception:
                self.remove_swapmem()
                raise
            self.is_allocated = True