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:
- This is a method in a class.
- I have logging enabled in the real code.
I have an exception defined named TemplateUpdaterException.
- 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)