summaryrefslogtreecommitdiffstats
path: root/gamechest/steps_remove.py
blob: 6b9144a36da22fe10c4f2ba4233b4f12f52dcee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!python3

import contextlib
import subprocess

from stepper import Stepper

def step_remove(queue, conf, status):
    extracted_path = '/'.join((
            conf.gamedir,
            strip_tar_ext(status['installed_package_name']),
    ))
    with contextlib.ExitStack() as stack:
        proc = stack.enter_context(subprocess.Popen(
                ('rm', '-rvf', '--', extracted_path),
                stdout=subprocess.PIPE,
        ))
        selector = stack.enter_context(selectors.DefaultSelector())
        selector.register(proc.stdout, selectors.EVENT_READ)
        itemscount = 0
        while True:
            with contextlib.suppress(queue.Empty):
                if queue.get_nowait() == Stepper.Command.STOP:
                    proc.terminate()
                    break
            if ready := selector.select(0.250):
                data = proc.stdout.read1()
                if len(data) == 0:
                    break
                itemscount += data.count(b'\n')
                yield itemscount*100/status['installed_count']