From 6c1ad180d74fee08f3259cedd2090fa45dcb1a2e Mon Sep 17 00:00:00 2001 From: Hasan Wan Date: Thu, 16 May 2013 17:25:40 +0800 Subject: [PATCH] implement the sync api Change-Id: I237184d6ffd48ab0a07279eb372500e580f4bf1d Signed-off-by: Hasan Wan --- common/utils.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/common/utils.py b/common/utils.py index 3a720f7..2ee51aa 100644 --- a/common/utils.py +++ b/common/utils.py @@ -20,6 +20,7 @@ import os import glob +import subprocess from common import runner @@ -88,3 +89,30 @@ def unicode_to_str(obj): return obj.encode('utf-8') else: return obj + +def sync(source, destination): + """ sync srouce to destination, support local filesystem, + ssh and rsync protocol. + + Note: access to destination server must be use key authentication + or anonymous + """ + + # Through rsync protocol + if destination.startswith('rsync:'): + cmd = "rsync -av %s/ %s" %(source, destination) + + # Through ssh protocol + elif destination.startswith('ssh:'): + destination = destination.replace("ssh://", "") + cmd = "scp -r %s/* %s" %(source, destination) + + # Try to take the destination as local path + else: + cmd = "mv -v %s/* %s/" %(source, destination) + try: + ret_code = subprocess.call(cmd, shell = True) + except OSError as err: + raise RuntimeException("Execution of %s failed: %s" % (cmd, str(err))) + + return ret_code -- 2.7.4