def get_r(self, remotedir, localdir, preserve_mtime=False): """recursively copy remotedir structure to localdir :param str remotedir: the remote directory to copy from :param str localdir: the local directory to copy to :param bool preserve_mtime: *Default: False* - preserve modification time on files :returns: None :raises: """ logging_utils.log_func_details(remotedir=remotedir, localdir=localdir, preserve_mtime=preserve_mtime) wtcb = WTCallbacks() self.walktree(remotedir, wtcb.file_cb, wtcb.dir_cb, wtcb.unk_cb) # handle directories we recursed through for dname in wtcb.dlist: for subdir in path_advance(dname): try: os.mkdir(reparent(localdir, subdir)) # force result to a list for setter, wtcb.dlist = wtcb.dlist + [ subdir, ] except OSError: # dir exists pass for fname in wtcb.flist: # they may have told us to start down farther, so we may not have # recursed through some, ensure local dir structure matches head, _ = os.path.split(fname) if head not in wtcb.dlist: for subdir in path_advance(head): if subdir not in wtcb.dlist and subdir != '.': os.mkdir(reparent(localdir, subdir)) wtcb.dlist = wtcb.dlist + [ subdir, ] self.get(fname, reparent(localdir, fname), preserve_mtime=preserve_mtime)
def put_r(self, localpath, remote_path, confirm=True, preserve_mtime=False): """Recursively copies a local directory's contents to a remote_path :param str localpath: the local path to copy (source) :param str remote_path: the remote path to copy to (target) :param bool confirm: whether to do a stat() on the file afterwards to confirm the file size :param bool preserve_mtime: *Default: False* - make the modification time(st_mtime) on the remote file match the time on the local. (st_atime can differ because stat'ing the localfile can/does update it's st_atime) :returns: None :raises IOError: if remote_path doesn't exist :raises OSError: if localpath doesn't exist """ logging_utils.log_func_details(localpath=localpath, remote_path=remote_path, confirm=confirm, preserve_mtime=preserve_mtime) wtcb = WTCallbacks() cur_local_dir = os.getcwd() os.chdir(localpath) walktree('.', wtcb.file_cb, wtcb.dir_cb, wtcb.unk_cb) # restore local directory os.chdir(cur_local_dir) for dname in wtcb.dlist: if dname != '.': pth = reparent(remote_path, dname) if not self.is_dir(pth): self.mkdir(pth) for fname in wtcb.flist: head, _ = os.path.split(fname) if head not in wtcb.dlist: for subdir in path_advance(head): if subdir not in wtcb.dlist and subdir != '.': self.mkdir(reparent(remote_path, subdir)) wtcb.dlist = wtcb.dlist + [ subdir, ] src = os.path.join(localpath, fname) dest = reparent(remote_path, fname) # print('put', src, dest) self.put(src, dest, confirm=confirm, preserve_mtime=preserve_mtime)
def get_r(self, remotedir, localdir, preserve_mtime=False): """recursively copy remotedir structure to localdir :param str remotedir: the remote directory to copy from :param str localdir: the local directory to copy to :param bool preserve_mtime: *Default: False* - preserve modification time on files :returns: None :raises: """ self._sftp_connect() wtcb = WTCallbacks() self.walktree(remotedir, wtcb.file_cb, wtcb.dir_cb, wtcb.unk_cb) # handle directories we recursed through for dname in wtcb.dlist: for subdir in path_advance(dname): try: os.mkdir(reparent(localdir, subdir)) # force result to a list for setter, wtcb.dlist = wtcb.dlist + [subdir, ] except OSError: # dir exists pass for fname in wtcb.flist: # they may have told us to start down farther, so we may not have # recursed through some, ensure local dir structure matches head, _ = os.path.split(fname) if head not in wtcb.dlist: for subdir in path_advance(head): if subdir not in wtcb.dlist and subdir != '.': os.mkdir(reparent(localdir, subdir)) wtcb.dlist = wtcb.dlist + [subdir, ] self.get(fname, reparent(localdir, fname), preserve_mtime=preserve_mtime)
def put_r(self, localpath, remotepath, confirm=True, preserve_mtime=False): """Recursively copies a local directory's contents to a remotepath :param str localpath: the local path to copy (source) :param str remotepath: the remote path to copy to (target) :param bool confirm: whether to do a stat() on the file afterwards to confirm the file size :param bool preserve_mtime: *Default: False* - make the modification time(st_mtime) on the remote file match the time on the local. (st_atime can differ because stat'ing the localfile can/does update it's st_atime) :returns: None :raises IOError: if remotepath doesn't exist :raises OSError: if localpath doesn't exist """ self._sftp_connect() wtcb = WTCallbacks() cur_local_dir = os.getcwd() os.chdir(localpath) walktree('.', wtcb.file_cb, wtcb.dir_cb, wtcb.unk_cb) # restore local directory os.chdir(cur_local_dir) for dname in wtcb.dlist: if dname != '.': pth = reparent(remotepath, dname) if not self.isdir(pth): self.mkdir(pth) for fname in wtcb.flist: head, _ = os.path.split(fname) if head not in wtcb.dlist: for subdir in path_advance(head): if subdir not in wtcb.dlist and subdir != '.': self.mkdir(reparent(remotepath, subdir)) wtcb.dlist = wtcb.dlist + [subdir, ] src = os.path.join(localpath, fname) dest = reparent(remotepath, fname) # print('put', src, dest) self.put(src, dest, confirm=confirm, preserve_mtime=preserve_mtime)