def test_issue_63(lsftp): '''put_r-fails-when-overwriting-directory''' localpath = mkdtemp() remote_dir = os.path.split(localpath)[1] build_dir_struct(localpath) localpath = os.path.join(localpath, 'pub') # make a tidy place to put them lsftp.mkdir(remote_dir) # run the op lsftp.put_r(localpath, remote_dir) try: lsftp.put_r(localpath, remote_dir) failed = False except IOError: failed = True # inspect results rfs = pysftp.WTCallbacks() with lsftp.cd(remote_dir): lsftp.walktree('.', rfs.file_cb, rfs.dir_cb, rfs.unk_cb) # 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(os.path.split(localpath)[0]) # assert that it worked without throwing an error assert not failed
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 rrdl(server,path): #with server.cd(path): directory = server.listdir(path) for item in directory: if 'sync_app' not in item: if server.isfile(sftp.reparent(path,item)): remoteFiles.append(sftp.reparent(path,item)) elif server.isdir(sftp.reparent(path,item)): remoteDirectory.append(sftp.reparent(path,item)) rrdl(server,sftp.reparent(path,item))
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 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)
def test_reparent_dotted(): '''test the reparent if oldpath beings with '.' ''' newparent = '/tmp/fuzzy' oldpath = '.' assert reparent(newparent, oldpath) == os.path.join(newparent, oldpath)
def test_reparent_root(): '''test the reparent to a root path''' newparent = '/tmp/fuzzy' oldpath = '/pub' assert reparent(newparent, oldpath) == os.path.join(newparent, '.' + oldpath)
def test_reparent_with_ending_slash(): '''test the reparent if newparent ends with a slash ''' newparent = '/tmp/fuzzy/' oldpath = '.' assert reparent(newparent, oldpath) == os.path.join(newparent, oldpath)
def test_reparent_root(): '''test the reparent if oldpath beings with '.' ''' newparent = '/tmp/fuzzy' oldpath = '/pub' assert reparent(newparent, oldpath) == os.path.join(newparent, '.' + oldpath)
def test_reparent_with_ending_slash(): '''test the reparent if oldpath beings with '.' ''' newparent = '/tmp/fuzzy/' oldpath = '.' assert reparent(newparent, oldpath) == os.path.join(newparent, oldpath)