Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import re
00036 from subprocess import Popen, PIPE
00037
00038 class Rsync():
00039
00040 def __init__(self, rsync_args, source, dest, progress_callback=None):
00041 self.rsync_args = rsync_args
00042 self.source = source
00043 self.dest = dest
00044 self.percent_complete = 0
00045 self.progress_callback = progress_callback
00046 self.transfer_rate = 0
00047
00048 self.stdout_block = ''
00049 self.stderr_block = ''
00050
00051 def sync(self):
00052
00053 self.rsync_cmd = ['rsync'] + self.rsync_args + ['--progress', '--outbuf=L', self.source, self.dest]
00054 self.p = Popen(self.rsync_cmd, stdout=PIPE, stderr=PIPE)
00055
00056
00057 for line_with_whitespace in iter(self.p.stdout.readline, b''):
00058 self.line = re.sub( '\s+', ' ', line_with_whitespace).strip()
00059 self.stdout_block += self.line
00060
00061
00062 if self.progress_callback:
00063 self._parse_progress()
00064 self._parse_transfer_rate()
00065 self.progress_callback(self.line, self.percent_complete, self.transfer_rate)
00066
00067 self.stderr_block = '\n'.join(self.p.stderr)
00068
00069 self.p.poll()
00070
00071 if self.p.returncode > -1:
00072
00073 self.progress_callback(None, 100.0, self.transfer_rate)
00074 return True
00075 else:
00076 return False
00077
00078 def _parse_progress(self):
00079
00080 re_matches = re.findall(r'(to-chk|to-check)=(\d+)/(\d+)', self.line)
00081 if re_matches:
00082 progress_tuple = re_matches[0]
00083 self.remaining_files = float(progress_tuple[1])
00084 self.total_files = float(progress_tuple[2])
00085 self.percent_complete = round(100.0 * (1 - (self.remaining_files/self.total_files)), 2)
00086
00087 def _parse_transfer_rate(self):
00088 smoothing_effect = 0.1
00089
00090 rate_tuples = re.findall(r'(\s[0-9]*\.[0-9]+|[0-9]+)(kb|mb|gb|tb)\/s\s', self.line.lower())
00091 rate_conversions = {'kb':3, 'mb':6, 'gb':9, 'tb':12}
00092
00093 if rate_tuples:
00094 if rate_tuples[-1][1] in rate_conversions:
00095 rate_tuple = rate_tuples[-1]
00096 transfer_rate_sample = float(rate_tuple[0]) * pow(10, rate_conversions[rate_tuple[1]])
00097
00098
00099 self.transfer_rate = smoothing_effect * transfer_rate_sample + (1.0-smoothing_effect) * self.transfer_rate
00100
00101 def get_progress(self):
00102 return self.percent_complete