]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
misc: add flexibility and move_file 1049/head
authorJan Fajerski <jfajerski@suse.com>
Thu, 16 Mar 2017 13:21:31 +0000 (14:21 +0100)
committerJan Fajerski <jfajerski@suse.com>
Tue, 21 Mar 2017 07:00:39 +0000 (08:00 +0100)
Add preserve_perms argument to move_file to make the preservation of
permissions optional. This makes move_file usable if the target
file does not yet exist.

Signed-off-by: Jan Fajerski <jfajerski@suse.com>
teuthology/misc.py

index b9b5baa0313a3e7d7c82cc439a73d496135826b8..b792d0e0a2e7a6250c06a6e1733acb541fda6778 100644 (file)
@@ -550,27 +550,31 @@ def copy_file(from_remote, from_path, to_remote, to_path=None):
     ])
 
 
-def move_file(remote, from_path, to_path, sudo=False):
+def move_file(remote, from_path, to_path, sudo=False, preserve_perms=True):
     """
     Move a file from one path to another on a remote site
 
-    The file needs to be stat'ed first, to make sure we
-    maintain the same permissions
-    """
-    args = []
-    if sudo:
-        args.append('sudo')
-    args.extend([
-        'stat',
-        '-c',
-        '\"%a\"',
-        to_path
-    ])
-    proc = remote.run(
-        args=args,
-        stdout=StringIO(),
-    )
-    perms = proc.stdout.getvalue().rstrip().strip('\"')
+    If preserve_perms is true, the contents of the destination file (to_path,
+    which must already exist in this case) are replaced with the contents of the
+    source file (from_path) and the permissions of to_path are preserved. If
+    preserve_perms is false, to_path does not need to exist, and is simply
+    clobbered if it does.
+    """
+    if preserve_perms:
+        args = []
+        if sudo:
+            args.append('sudo')
+        args.extend([
+            'stat',
+            '-c',
+            '\"%a\"',
+            to_path
+        ])
+        proc = remote.run(
+            args=args,
+            stdout=StringIO(),
+        )
+        perms = proc.stdout.getvalue().rstrip().strip('\"')
 
     args = []
     if sudo:
@@ -586,24 +590,26 @@ def move_file(remote, from_path, to_path, sudo=False):
         stdout=StringIO(),
     )
 
-    # reset the file back to the original permissions
-    args = []
-    if sudo:
-        args.append('sudo')
-    args.extend([
-        'chmod',
-        perms,
-        to_path,
-    ])
-    proc = remote.run(
-        args=args,
-        stdout=StringIO(),
-    )
+    if preserve_perms:
+        # reset the file back to the original permissions
+        args = []
+        if sudo:
+            args.append('sudo')
+        args.extend([
+            'chmod',
+            perms,
+            to_path,
+        ])
+        proc = remote.run(
+            args=args,
+            stdout=StringIO(),
+        )
 
 
 def delete_file(remote, path, sudo=False, force=False):
     """
-    rm a file on a remote site.
+    rm a file on a remote site. Use force=True if the call should succeed even
+    if the file is absent or rm path would otherwise fail.
     """
     args = []
     if sudo: