implement the sync api
authorHasan Wan <hasan.wan@intel.com>
Thu, 16 May 2013 09:25:40 +0000 (17:25 +0800)
committerGerrit Code Review <gerrit2@otctools.jf.intel.com>
Sat, 18 May 2013 12:27:45 +0000 (05:27 -0700)
Change-Id: I237184d6ffd48ab0a07279eb372500e580f4bf1d
Signed-off-by: Hasan Wan <hasan.wan@intel.com>
common/utils.py

index 3a720f7..2ee51aa 100644 (file)
@@ -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