Python to rsync

See Python Rsync for a complete program to do this.

This is a method I put together to call rsync from python.

There are some assumption here:

  1. This is a method in a class.
  2. I have logging enabled in the real code.
  3. I have an exception defined named TemplateUpdaterException.

  4. Epydoc is used to generate API documentation.

import os
from subprocess import call, CalledProcessError

    def __rsync(self, src, dst, args="-a --delete", copyDir=False):
        """
        Wrapper for the rsync command. Synchronize the destination to the
        source.

        @param src: The source.
        @param dst: The destination.
        @keyword args: The arguments to pass to rsync.
        @keyword copyDir: C{True} copies the last directory of the C{src} path
                          and C{False} copies only the files in the last
                          directory.
        """
        src = src.strip()

        if copyDir: src = src.rstrip(os.sep)
        else: src = src[-1] != os.sep and src + os.sep or src

        cmd = "rsync %s %s %s" % (args, src, dst)

        try:
            ret = call(cmd, shell=True)
        except (OSError, ValueError, CalledProcessError), e:
            msg = "Could not synchronize template directories %s to %s." % \
                  (dst, src)
            self._log.error(msg)
            raise TemplateUpdaterException(msg)

Python to rsync (last edited 2010-08-15 10:32:10 by CarlNobile)