summaryrefslogtreecommitdiffstats
path: root/gamechestcli/rsync.py
blob: 37330f940417fa6343fb244cb6a2745e2608e958 (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
#!/usr/bin/python3

import os
import re
import subprocess

import humanfriendly

from .structures import Progress

class Rsync:

    _rsync_progress_re = re.compile(r'^\s*(\S+)\s+(\d+)%\s+(\S+)\s+(\S+)\s+$')

    def __init__(self, src, dst):
        self.proc = subprocess.Popen(
            [
                'rsync',
                '--partial',
                # not human readable, easier to parse (but speed still appears
                # in human form).
                '--no-h',
                '--info=progress2',
                src,
                dst,
            ],
            stdout=subprocess.PIPE,
            #stderr=subprocess.DEVNULL,
            encoding='utf8',
            env={**os.environ,
                 **{'LC_ALL':'C.UTF-8',
                    'LANG':'C.UTF-8',
                    'LANGUAGE':'C.UTF-8',
                    }},
        )
        self.last_progress = Progress()

    def select_fd(self):
        'useful to use selectors with the process stdout file descriptor'
        return self.proc.stdout

    def progress_read(self):
        line = self.proc.stdout.readline()
        if match := self._rsync_progress_re.search(line):
            self.last_progress = Progress(
                int(match.group(1)),
                int(match.group(2)),
                humanfriendly.parse_size(match.group(3), binary=True),
                match.group(4),
            )
        return self.last_progress

    def terminate(self):
        self.proc.terminate()

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

    def wait(self, timeout=None):
        return self.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 Rsync(sys.argv[1], sys.argv[2]) as rsync:
            while rsync.poll() is None:
                progress = rsync.progress_read()
                print(f'{progress.bytes}b {progress.percent}% '
                    f'{progress.speed}b/s {progress.eta}')
            rc = rsync.poll()
            print(f'rsync ended with code: {rc}')
    print('Rsync test main ended')