summaryrefslogtreecommitdiffstats
path: root/gamechestcli/extract.py
blob: 2ac7fdee77d873f55e542e64f6c87ddb7eeb5e6c (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python3

import os
import re
import subprocess

import humanfriendly

from structures import Progress

class Extract:

    _progress_re = re.compile(r'^\s*(\S+)\s+\[([^]]+)/s\]\s+ETA\s+(\S+)\s*$')

    def __init__(self, src, dst):
        common_parameters = dict(
            encoding='utf8',
            env={**os.environ,
                 **{'LC_ALL':'C.UTF-8',
                    'LANG':'C.UTF-8',
                    'LANGUAGE':'C.UTF-8',
                    }},
        )
        self.src_size = os.stat(src).st_size
        self.pv_proc = subprocess.Popen(
            [
                'pv',
                '--force',
                '--format', '%b %a %e',
                src,
            ],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            **common_parameters,
        )
        lzip_command = '/usr/bin/lzip'
        plzip_command = '/usr/bin/plzip'
        lzip_command_to_use = lzip_command
        if os.path.exists(plzip_command):
            lzip_command_to_use = plzip_command
        self.zip_proc = subprocess.Popen(
            [
                lzip_command_to_use,
                '--decompress',
            ],
            stdin=self.pv_proc.stdout,
            stdout=subprocess.PIPE,
            **common_parameters,
        )
        self.tar_proc = subprocess.Popen(
            [
                'tar',
                '-C', dst,
                '-xf', '-'
            ],
            stdin=self.zip_proc.stdout,
            **common_parameters,
        )
        self.last_progress = Progress()

    def select_fd(self):
        'useful to use selectors with the process most meaningful fd'
        return self.pv_proc.stderr

    def progress_read(self):
        line = self.pv_proc.stderr.readline()
        if match := self._progress_re.search(line):
            written_bytes = humanfriendly.parse_size(match.group(1))
            self.last_progress = Progress(
                written_bytes,
                int(100 * written_bytes / self.src_size),
                humanfriendly.parse_size(match.group(2)),
                match.group(3),
            )
        return self.last_progress

    def terminate(self):
        for proc in (self.pv_proc, self.zip_proc, self.tar_proc):
            proc.terminate()

    def poll(self):
        'returns None if not terminated, otherwise return returncode'
        return self.tar_proc.poll()

    def wait(self, timeout=None):
        self.pv_proc.wait(timeout)
        self.zip_proc.wait(timeout)
        return self.tar_proc.wait(timeout)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, value, traceback):
        self.terminate()
        self.wait()


if __name__ == '__main__':
    import sys
    import contextlib
    with contextlib.suppress(KeyboardInterrupt):
        with Extract(sys.argv[1], sys.argv[2]) as extract:
            while extract.poll() is None:
                progress = extract.progress_read()
                print(f'{progress.bytes}b {progress.percent}% '
                    f'{progress.speed}b/s {progress.eta}')
            rc = extract.poll()
            print(f'ended with code: {rc}')
    print('test main ended')