Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show and parse rsync progress in Python?

I am using the development version of rsync with --info-progress option. I am writing a python program which transfer files from server to local computer using rsync:

finalresult = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', 'hostname:/filename', '/home/nfs/django/user'],
                                    stdout=subprocess.PIPE).communicate()[0]

When I run the program in the terminal, it doesn't show me the progress however if I use the rsync utility independently it shows the progress. I want to see the progress in the Terminal and parse or take the progress percentage to use it later to show progress bar to the user in real time. Thank you!

Edit: This is what I have tried so far: The code above stores the command processing in finalresult variable. I tried to print finalresult variable in the django template but it doesn't returns anything.

like image 650
Joyfulgrind Avatar asked Nov 27 '12 06:11

Joyfulgrind


1 Answers

Sorry, but that probably cannot be done neither with subprocess nor with the rest of the standard library. And that is because inside subprocess.communicate() you would find:

def wait(self):
   """Wait for child process to terminate.  Returns returncode
   attribute."""
   if self.returncode is None:
      # Try calling a function os.waitpid(self.pid, 0)
      # Ignore Interrupted System Call (errno.EINTR) errors  
      pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
      self._handle_exitstatus(sts)
   return self.returncode

On Unix the os.waitpid() function call means: Wait for completion of a child process given by process id pid, and return a tuple containing its process id and exit status indication. So, you have to wait until the child process is finished.

The closest alternative would be pexpect, but it still won't do the progress bar analysis. A possible solution would be hacking rsync and patching its progress output so that pexpect could work with it.

like image 133
Zaur Nasibov Avatar answered Sep 22 '22 14:09

Zaur Nasibov