From: Jan Fajerski Date: Thu, 16 Mar 2017 13:21:31 +0000 (+0100) Subject: misc: add flexibility and move_file X-Git-Tag: 1.1.0~434^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=8a5b6137a46f74f0cb0440635fa098dd6311572a;p=teuthology.git misc: add flexibility and move_file 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 --- diff --git a/teuthology/misc.py b/teuthology/misc.py index b9b5baa031..b792d0e0a2 100644 --- a/teuthology/misc.py +++ b/teuthology/misc.py @@ -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: