def put_r(self, localpath, remotepath, confirm=True, preserve_mtime=False): self._sftp_connect() wtcb = WTCallbacks() cur_local_dir = os.getcwd() os.chdir(localpath) walktree('.', wtcb.file_cb, wtcb.dir_cb, wtcb.unk_cb) 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) if self.isfile(dest) and os.stat(src).st_mtime > self.stat(dest).st_mtime: continue print('put', src, dest, os.stat(src).st_mtime) self.put(src, dest, confirm=confirm, preserve_mtime=preserve_mtime)
def test_walktree_local_bad(): '''test pysftp.walktree on a non-existing directory''' wtcb = pysftp.WTCallbacks() with pytest.raises(OSError): pysftp.walktree('/non-existing', fcallback=wtcb.file_cb, dcallback=wtcb.dir_cb, ucallback=wtcb.unk_cb)
def test_put_d(lsftp): '''test put_d''' localpath = mkdtemp() print(localpath) remote_dir = os.path.split(localpath)[1] build_dir_struct(localpath) localpath = os.path.join(localpath, 'pub') print(localpath) # make a tidy place to put them lsftp.mkdir(remote_dir) # run the op lsftp.put_d(localpath, remote_dir) # inspect results rfs = pysftp.WTCallbacks() with lsftp.cd(remote_dir): lsftp.walktree('.', rfs.file_cb, rfs.dir_cb, rfs.unk_cb) lfs = pysftp.WTCallbacks() save_dir = os.getcwd() os.chdir(localpath) walktree('.', lfs.file_cb, lfs.dir_cb, lfs.unk_cb, recurse=False) os.chdir(save_dir) # cleanup remote for fname in rfs.flist: lsftp.remove(reparent(remote_dir, fname)) for dname in reversed(rfs.dlist): lsftp.rmdir(reparent(remote_dir, dname)) lsftp.rmdir(remote_dir) # cleanup local shutil.rmtree(localpath) # if assertions fail, give some meaningful debug out print("rfs", remote_dir) print(rfs.flist) print(rfs.dlist) print(rfs.ulist) print("lfs", localpath) print(lfs.flist) print(lfs.dlist) print(lfs.ulist) # check results assert rfs.flist == lfs.flist assert rfs.dlist == [] assert rfs.ulist == lfs.ulist assert rfs.ulist == []
def test_walktree_local(): '''test the capability of walktree to walk a local directory structure''' wtcb = pysftp.WTCallbacks() pysftp.walktree('.', fcallback=wtcb.file_cb, dcallback=wtcb.dir_cb, ucallback=wtcb.unk_cb) print(wtcb.dlist) for dname in ['./docs', './tests']: assert dname in wtcb.dlist print(wtcb.ulist) assert wtcb.ulist == [] print(wtcb.flist) for fname in ['./release.sh', './MANIFEST.in', './tests/test_execute.py']: assert fname in wtcb.flist
def put_r(sftp, localpath, remotepath, confirm=True, preserve_mtime=True): """Recursive put of files over sftp""" sftp._sftp_connect() wtcb = WTCallbacks() cur_local_dir = os.getcwd() os.chdir(localpath) walktree('.', wtcb.file_cb, wtcb.dir_cb, wtcb.unk_cb) os.chdir(cur_local_dir) for dname in wtcb.dlist: if dname != '.': pth = reparent(remotepath, dname) if not sftp.isdir(pth): sftp.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 != '.': sftp.mkdir(reparent(remotepath, subdir)) wtcb.dlist = wtcb.dlist + [subdir, ] src = os.path.join(localpath, fname) dest = reparent(remotepath, fname) if os.path.splitext(src)[1] not in FILETYPES: print('skip filetype %s' % src) continue if sftp.isfile(dest): src_mtime = int(os.stat(src).st_mtime) dest_mtime = int(sftp.stat(dest).st_mtime) if src_mtime <= dest_mtime: print('no change %s' % src) continue else: print('changed %s < %s' % (dest_mtime, src_mtime), end=': ') sftp.remove(dest) print('put %s => %s' % (src, dest)) sftp.put(src, dest, confirm=confirm, preserve_mtime=preserve_mtime)