diff -ur v2/ftp_ut.py v3/ftp_ut.py --- v2/ftp_ut.py 2020-02-09 11:01:19.000000000 +0900 +++ v3/ftp_ut.py 2020-02-11 00:49:51.000000000 +0900 @@ -2,6 +2,7 @@ import os +import empty import cmd_ut import dbg import arg @@ -9,75 +10,113 @@ to_str = base.to_str -def login_str(host, user, passwd): - lst = [ - 'open {}'.format( host ), - 'user {} {}'.format( user, passwd ), - 'bin', - 'prompt', - ] - return to_str( lst ) - -def get_str(cmd, paths, lcd=True): +def new(host, user, passwd, tmout1=15, tmout2=1, debug=False): + ftp_cmd = 'ftp -n' + proc = cmd_ut.proc_new_comm_tm2( ftp_cmd, tmout1, tmout2 ) + + write = lambda s: proc.write( s + '\n' ) + + def comm(s): + if debug: + dbg.out(s) + s += '\n' + r = proc.comm(s) + if r and debug: + dbg.out(r, '') + return (s, r) + + comm_lst = lambda lst: comm( to_str(lst) ) + + def login(): + lst = [ + 'open {}'.format( host ), + 'user {} {}'.format( user, passwd ), + 'bin', + 'prompt', + 'verbose', + ] + return comm_lst(lst) + + def quit(): + write('quit') + proc.wait() + + do_cmd = lambda cmd, prm='': comm( to_str( (cmd, prm), delim=' ' ) ) + + ls = lambda path='': do_cmd( 'ls', path ) + + def flat_sr(srs): + (s, r) = map( lambda lst: to_str(lst, delim=''), zip( *srs ) ) + return (s, r) + + def add_sr(sr, sr_): + f = lambda i: sr[i] + ( sr_[i] if sr_[i] else '' ) + return ( f(0), f(1) ) + + def cd(path, lcd=True, back=False): + if not path: + return ('', '') + srs = [] + if back: + n = path.count('/') + 1 if path else 0 + path = to_str( ['..'] * n, delim='/' ) + srs.append( do_cmd( 'cd', path ) ) + if lcd: + srs.append( do_cmd( 'lcd', path ) ) + return flat_sr(srs) - def f(path): + def do_cd_cmd(cmd, path, lcd=True): (dir_, name) = os.path.split(path) - n = dir_.count('/') + 1 if dir_ else 0 - back = '/'.join( ['..'] * n ) - - lst = [] - if n > 0: - lst.append( 'cd {}'.format(dir_) ) - if lcd: - lst.append( 'lcd {}'.format(dir_) ) - - lst.append( '{} {}'.format(cmd, name) ) - - if n > 0: - lst.append( 'cd {}'.format(back) ) - if lcd: - lst.append( 'lcd {}'.format(back) ) - - return to_str( lst ) + srs = [] + srs.append( cd( dir_, lcd) ) + srs.append( do_cmd( cmd, name ) ) + srs.append( cd( dir_, lcd, back=True ) ) + return flat_sr(srs) - return to_str( map( f, paths ) ) + def put(path): + return do_cd_cmd( 'put', path ) -def mkdir_str(paths): + def del_(path): + return do_cd_cmd( 'del', path, lcd=False ) - def f(path): - lst = [] + def mkdir(path): + srs = [] nms = path.split('/') for i in range( len(nms) ): - lst.append( 'mkdir {}'.format( '/'.join( nms[:i+1] ) ) ) - return to_str( lst ) - - return to_str( map( f, paths ) ) + path_ = to_str( nms[:i+1], delim='/' ) + srs.append( do_cmd( 'mkdir', path_ ) ) + return flat_sr(srs) + + def rmdir(path): + return do_cmd( 'rmdir', path ) + + def lst_d_f(path): + (s, r) = ls(path) + lst = r.split('\n') + lst = map( lambda s: s.split(), lst ) + + is_mod = lambda s: all( map( lambda c: c in 'cdrwx-', s ) ) + is_stat = lambda ws: len(ws) == 9 and is_mod( ws[0] ) + lst = list( filter( is_stat, lst ) ) + + (lst_d, lst_f) = base.filter_div( lambda ws: ws[0][0] == 'd', lst ) + + to_nms = lambda lst: list( map( lambda ws: ws[-1], lst ) ) + (nms_d, nms_f) = map( to_nms, (lst_d, lst_f) ) + nms_d = list( filter( lambda s: s not in ('.', '..'), nms_d ) ) + return (nms_d, nms_f) + + def do_paths(f, paths): + srs = [] + for path in paths: + srs.append( f(path) ) + return flat_sr(srs) -def rmdir_str(paths): - f = lambda path: 'rmdir {}'.format(path) - return to_str( map( f, paths ) ) - -def ls_str(paths): - f = lambda path: 'ls {}'.format(path) - return to_str( map( f, paths ) ) - -def cmd_str_for_show(s): # passwd ! - def f(line): - ws = line.split(' ') - if ws[0] == 'user': - ws[2] = '----' - return to_str( ws, delim=' ' ) - - return to_str( map( f, s.split('\n') ) ) - -def ls_to_lst(lst): - f = lambda s: s.split()[-1] if s else '' - flt = lambda s: s and s not in ('.', '..') - return filter( flt, map( f, lst ) ) + return empty.new( locals() ) def help(): msgs = [ - '-h -v', + '[-h] [-dbg]', ' host user passwd path ..', ' host user passwd del path ..', ' host user passwd mkdir path ..', @@ -97,43 +136,36 @@ paths = a.get_av() if not paths: help() + debug = a.is_pop('-debug') - lst = [] - s = login_str( host, user, passwd ) - lst.append(s) + ftp = new(host, user, passwd, debug=debug) - s = '' ( p0, p1_ ) = ( paths[0], paths[1:] ) + quiet = p0 in ('ls', 'lst') + + ftp.login() + if not quiet: + dbg.out('login') + + sr = ('', '') if p0 == 'del': - s = get_str( p0, p1_, False ) + sr = ftp.do_paths( ftp.del_, p1_ ) elif p0 == 'mkdir': - s = mkdir_str( p1_ ) + sr = ftp.do_paths( ftp.mkdir, p1_ ) elif p0 == 'rmdir': - s = rmdir_str( p1_ ) + sr = ftp.do_paths( ftp.rmdir, p1_ ) elif p0 == 'ls': - s = ls_str( p1_ ) + sr = ftp.do_paths( ftp.ls, p1_ ) if p1_ else ftp.ls() elif p0 == 'lst': # special - s = ls_str( p1_ ) + lst = [] + for path in p1_: + ( nms_d, nms_f ) = ftp.lst_d_f(path) + lst += nms_d + nms_f + sr = ( '', to_str(lst, last=True) ) else: - s = get_str( 'put', paths ) - if s: - lst.append(s) - - lst.append('quit') - - s = to_str( lst ) - - res_show = [ 'ls', 'lst' ] - - if p0 not in res_show: - dbg.out( cmd_str_for_show(s) ) - - cmd = 'ftp -n' - r = cmd_ut.call_comm( cmd, s.encode() ).decode() - if p0 in res_show: - lst = r.strip().split('\n') - lst = lst[1:] # cut 'Interactive mode off.' - if p0 == 'lst': - lst = sorted( ls_to_lst(lst) ) - dbg.out( to_str(lst) ) + sr = ftp.do_paths( ftp.put, paths ) + + (s, r) = sr + dbg.out( r if quiet else s, '' ) + ftp.quit() # EOF