72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import time
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from math import floor
|
|
|
|
SIZE_PREFIXES = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
|
|
|
|
class ActionQueue:
|
|
def __init__(self):
|
|
self.queue = []
|
|
|
|
def download_image(self, image):
|
|
self.queue.append(ActionItem(f'Downloading image {image.name}', image.download))
|
|
self.queue.append(ActionItem(f'Unpacking image {image.name}', image.unpack_downloaded))
|
|
|
|
def delete_image(self, image):
|
|
self.queue.append(ActionItem(f'Deleting image {image.name}', image.delete, False))
|
|
|
|
def download_app(self, app):
|
|
self.queue.append(ActionItem(f'Downloading application {app.name}', app.download))
|
|
self.queue.append(ActionItem(f'Unpacking application {app.name}', app.unpack_downloaded))
|
|
self.queue.append(ActionItem(f'Installing application {app.name}', app.install, False))
|
|
|
|
def process(self):
|
|
index = 0
|
|
queue_length = len(self.queue)
|
|
for item in self.queue:
|
|
index += 1
|
|
item.text = f'[{index}/{queue_length}] {item.text}'
|
|
item.run()
|
|
|
|
class ActionItem:
|
|
def __init__(self, text, action, has_progress=True):
|
|
self.text = text
|
|
self.action = action
|
|
self.has_progress = has_progress
|
|
self.units_total = 1
|
|
self.units_done = 0
|
|
|
|
def run(self):
|
|
if self.has_progress:
|
|
with ThreadPoolExecutor() as executor:
|
|
future = executor.submit(self.action, self)
|
|
while not future.done():
|
|
time.sleep(0.2)
|
|
self.print_progress()
|
|
# Get the result of the future and let it raise exception, if there was any
|
|
data = future.result()
|
|
self.print_progress('\n')
|
|
else:
|
|
self.print_progress()
|
|
self.action()
|
|
self.units_done = 1
|
|
self.print_progress('\n')
|
|
|
|
def print_progress(self, end='\r'):
|
|
print(f'\x1b[K{self.text} ({self.units_done}/{self.units_total}) [{floor(self.units_done/self.units_total*100)} %]', end=end)
|
|
|
|
def readable_size(bytes):
|
|
i = 0
|
|
while bytes > 1024:
|
|
i += 1
|
|
bytes /= 1024
|
|
return f'{bytes:.2f} {SIZE_PREFIXES[i]}B'
|
|
|
|
def print_lock(pid):
|
|
with open(os.path.join('/proc', pid, 'cmdline')) as f:
|
|
cmdline = f.read().replace('\0', ' ').strip()
|
|
print(f'Waiting for lock currently held by process {pid} - {cmdline}')
|