Add support for verbose output for download module

This commit is contained in:
Oleksii Shevchuk 2017-05-22 17:24:12 +03:00
parent f471fc89b3
commit df81c52729
2 changed files with 30 additions and 17 deletions

View File

@ -9,29 +9,43 @@ import time
__class_name__="DownloaderScript"
def download(conn, remotepath, localpath, filter = None, ignore_invalid = False, chunk_size = 16000):
def download(conn, remotepath, localpath, filter=None, ignore_invalid=False, chunk_size=16000, log=None):
if conn.modules.os.path.isdir(remotepath):
download_dir(conn, remotepath, localpath, filter, chunk_size)
download_dir(conn, remotepath, localpath, filter, chunk_size, log)
elif conn.modules.os.path.isfile(remotepath):
if log:
start_time = time.time()
download_file(conn, remotepath, localpath, chunk_size)
if log:
size = os.path.getsize(localpath)
total_time = round(time.time()-start_time, 2)
kb_size = round(size / 10**3, 2)
log('{} -> {}: {}KB ({}KB/s)'.format(
remotepath, localpath,
kb_size,
round((size/total_time)/10**3, 2)))
else:
if not ignore_invalid:
raise ValueError("cannot download %r" % (remotepath,))
def download_dir(conn, remotepath, localpath, filter=None, chunk_size=16000):
def download_dir(conn, remotepath, localpath, filter=None, chunk_size=16000, log=None):
if not os.path.isdir(localpath):
os.makedirs(localpath)
for fn in conn.modules.os.listdir(remotepath):
if not filter or filter(fn):
rfn = conn.modules.os.path.join(remotepath, fn)
lfn = os.path.join(localpath, fn)
download(conn, rfn, lfn, filter=filter, ignore_invalid=True, chunk_size=chunk_size)
download(conn, rfn, lfn, filter=filter, ignore_invalid=True, chunk_size=chunk_size, log=log)
@config(category="manage")
class DownloaderScript(PupyModule):
""" download a file/directory from a remote system """
def init_argparse(self):
self.arg_parser = PupyArgumentParser(prog='download', description=self.__doc__)
self.arg_parser.add_argument('-v', '--verbose', action='store_true', default=False,
help='Be verbose during download')
self.arg_parser.add_argument('remote_file', metavar='<remote_path>')
self.arg_parser.add_argument('local_file', nargs='?', metavar='<local_path>', completer=path_completer)
@ -55,15 +69,10 @@ class DownloaderScript(PupyModule):
os.makedirs(local_dir)
self.info('downloading %s ...'%remote_file)
start_time = time.time()
download(self.client.conn, remote_file, local_file, chunk_size=8*1024*1024)
size = os.path.getsize(local_file)
self.success('file downloaded from remote:%s to local:%s'%(remote_file, local_file))
total_time=round(time.time()-start_time, 2)
self.info(
'%s bytes downloaded in: %ss. average %sKB/s'%(
size, total_time, round((size/total_time)/10**3, 2)
)
download(
self.client.conn,
remote_file, local_file,
chunk_size=8*1024*1024, log=self.info if args.verbose else None
)
self.success('downloaded from remote:%s to local:%s'%(remote_file, local_file))

View File

@ -182,22 +182,26 @@ class PtyShell(object):
cb = rpyc.async(print_callback)
close_cb = rpyc.async(close_callback)
not_eof = True
fd = self.master.fileno()
while not_eof:
r, x = None, None
try:
r, _, x = select.select([self.master], [], [self.master], None)
except OSError, e:
if e.errno in (errno.EAGAIN, errno.EWOULDBLOCK):
continue
except Exception, e:
break
if x or r:
try:
data = self.master.read(8192)
except IOError, e:
data = os.read(fd, 8192)
except OSError, e:
if e.errno in (errno.EAGAIN, errno.EWOULDBLOCK):
continue
except:
except Exception:
data = None
if data: