orchestra/remote: add (sudo_)write_file method
Add advanced write_file and sudo_write_file to remote context
for more intuitive and handy coding
@@ -458,6 +458,43 @@ class Remote(object):
])
return self.run(args=args, wait=False, stdout=run.PIPE)
+ def write_file(self, path, data, sudo=False, mode=None, owner=None,
+ mkdir=False, append=False):
+ """
+ Write data to remote file
+
+ :param path: file path on remote host
+ :param data: str, binary or fileobj to be written
+ :param sudo: use sudo to write file, defaults False
+ :param mode: set file mode bits if provided
+ :param owner: set file owner if provided
+ :param mkdir: preliminary create the file directory, defaults False
+ :param append: append data to the file, defaults False
+ """
+ dd = 'sudo dd' if sudo else 'dd'
+ args = dd + ' of=' + path
+ if append:
+ args += ' conv=notrunc oflag=append'
+ if mkdir:
+ mkdirp = 'sudo mkdir -p' if sudo else 'mkdir -p'
+ dirpath = os.path.dirname(path)
+ if dirpath:
+ args = mkdirp + ' ' + dirpath + '\n' + args
+ if mode:
+ chmod = 'sudo chmod' if sudo else 'chmod'
+ args += '\n' + chmod + ' ' + mode + ' ' + path
+ if owner:
+ chown = 'sudo chown' if sudo else 'chown'
+ args += '\n' + chown + ' ' + owner + ' ' + path
+ args = 'set -ex' + '\n' + args
+ self.run(args=args, stdin=data)
+
+ def sudo_write_file(self, path, data, **kwargs):
+ """
+ Write data to remote file with sudo, for more info see `write_file()`.
+ """
+ self.write_file(path, data, sudo=True, **kwargs)
+
@property
def os(self):
if not hasattr(self, '_os'):
