Running and monitoring rsync with Python

#!/usr/bin/python3

import subprocess,sys,time,re
import pprint
import string

def execute(cmd):
        process = subprocess.Popen(cmd, shell=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                universal_newlines=True)
        for stdout_line in iter(process.stdout.readline, ""):
                yield stdout_line
        process.stdout.close()
        return_code = process.wait()

        if return_code:
                raise subprocess.CalledProcessError(return_code, cmd)

for line2 in execute("rsync --info=progress2 -rl /usr/ ./mc"):
        line = ''.join(c for c in line2 if c.isprintable())
        result = re.search(r"([0-9\,]+)\s+([0-9]+)%\s+([0-9a-zA-Z\.\/]+)", line)
        if result:
                transfered = result.group(1)
                percentage = result.group(2)
                speed = result.group(3)
                print(f"Rsync process Transfered: {transfered} Percentage: {percentage} Speed: {speed}")
~                                                                                                         
Dette indlæg blev udgivet i Python. Bogmærk permalinket.