move make_latest_link to common/utils, add a parameter to keep the origin copy
authorHasan Wan <hasan.wan@intel.com>
Mon, 19 May 2014 09:13:22 +0000 (12:13 +0300)
committerHasan Wan <hasan.wan@intel.com>
Sun, 8 Jun 2014 14:40:23 +0000 (17:40 +0300)
Change-Id: I80aa2ca36b810818fa0bd726789591c2912621c3
Signed-off-by: Hasan Wan <hasan.wan@intel.com>
common/utils.py
job_create_snapshot.py

index d81c185..051e204 100644 (file)
@@ -23,6 +23,7 @@ import glob
 import subprocess
 import re
 import xml.sax.handler
+import shutil
 
 from common import runner
 
@@ -108,30 +109,42 @@ def unicode_to_str(obj):
     else:
         return obj
 
-def sync(source, destination):
+def sync(source, destination, remove=True):
     """ sync srouce to destination, support local filesystem,
         ssh and rsync protocol.
 
         Note: access to destination server must be use key authentication
               or anonymous
     """
+    ret_code = -1
 
     # Through rsync protocol
     if destination.startswith('rsync:'):
         cmd = "rsync -av %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)))
+
     # Through ssh protocol
     elif destination.startswith('ssh:'):
         destination = destination.replace("ssh://", "")
         cmd = "scp -r %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)))
+
     # 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)))
+        if remove:
+            shutil.move(source, destination)
+        else:
+            shutil.copytree(source, destination)
+        # No exception so far, return code 0
+        ret_code = 0
 
     return ret_code
 
@@ -242,3 +255,11 @@ def xml2obj(src):
         xml.sax.parse(src, builder)
     return builder.root._attrs.values()[0]
 
+def make_latest_link(snapshot_path):
+    """ make the latest repo link to the new repo
+        snapshot_path (str): the local path to snapshot
+    """
+    latest = os.path.join(snapshot_path, "../latest")
+    if os.path.lexists(latest):
+        os.unlink(latest)
+    os.symlink(snapshot_path, latest)
index 3b230a7..4391754 100755 (executable)
@@ -14,7 +14,7 @@ from common.buildservice import BuildService
 from common.repomaker import RepoMaker, RepoMakerError
 from common.backenddb import BackendDB
 from common.snapshot import Snapshot, SnapshotError
-
+from common.utils import make_latest_link
 
 class LocalError(Exception):
     """Local error exception."""
@@ -126,15 +126,6 @@ def make_repo(project, backenddb, base_path, live_repo_base, buildconf=None):
             'imagedata': imagedatas
         }
 
-def make_latest_link(snapshot_path):
-    """ make the latest repo link to the new repo
-        snapshot_path (str): the local path to snapshot
-    """
-    latest = os.path.join(snapshot_path, "../latest")
-    if os.path.lexists(latest):
-        os.unlink(latest)
-    os.symlink(snapshot_path, latest)
-
 def main():
     """Script entry point."""