def test_put_r(lsftp): '''test put_r''' 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_r(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) 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(os.path.split(localpath)[0]) # 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 == lfs.dlist assert rfs.ulist == lfs.ulist assert rfs.ulist == []
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 get_dir_list(self, scon, srcPath): # Set callback functions wtcb = pysftp.WTCallbacks() # Recursively map files scon.walktree(srcPath, fcallback=wtcb.file_cb, dcallback=wtcb.dir_cb, ucallback=wtcb.unk_cb) lAr = wtcb.flist return lAr
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 get_filepaths_on_server(): """Return list of files present on server""" wtcb = pysftp.WTCallbacks() with pysftp.Connection(host=conf.get('sftp', 'host'), username=conf.get('sftp', 'user'), port=int(conf.get('sftp', 'port')), private_key=conf.get('sftp', 'key_path')) as server: # Walk through all paths on server server.walktree(conf.get('sftp', 'start_path'), fcallback=wtcb.file_cb, dcallback=wtcb.dir_cb, ucallback=wtcb.unk_cb) logging.info("Count of items on server: " + str(len(wtcb.flist))) return wtcb.flist
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(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 = pysftp.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)
def test_walktree_cbclass(sftpserver): '''test the walktree function with callbacks from a class''' with sftpserver.serve_content(VFS): with pysftp.Connection(**conn(sftpserver)) as sftp: wtcb = pysftp.WTCallbacks() sftp.walktree('.', fcallback=wtcb.file_cb, dcallback=wtcb.dir_cb, ucallback=wtcb.unk_cb) assert './pub/foo2/bar1/bar1.txt' in wtcb.flist assert './read.me' in wtcb.flist assert len(wtcb.flist) > 3 dlist = ['./pub', './pub/foo1', './pub/foo2', './pub/foo2/bar1'] assert wtcb.dlist == dlist assert wtcb.ulist == []
import pysftp import configparser cnopts = pysftp.CnOpts() cnopts.hostkeys = None cred = configparser.ConfigParser() cred.read('/home/local1/TEST/credential') ftpIP = cred.get('credential', 'ip') ftpUSER = cred.get('credential', 'username') ftpPWD = cred.get('credential', 'password') rePATH = '/ethtorrent/downloads/complete/' wtcb = pysftp.WTCallbacks() with pysftp.Connection(ftpIP, username=ftpUSER, password=ftpPWD, cnopts=cnopts) as sftp: sftp.walktree(rePATH, fcallback=wtcb.dir_cb, dcallback=wtcb.dir_cb, ucallback=wtcb.unk_cb) print(len(wtcb.flist)) for fpath in wtcb.flist: print(fpath) sftp.close()