diff -ur v8/ftp_ut.py v9/ftp_ut.py --- v8/ftp_ut.py 2020-02-15 17:18:27.000000000 +0900 +++ v9/ftp_ut.py 2020-02-16 12:25:20.000000000 +0900 @@ -31,6 +31,9 @@ if r and debug: r_ = r.replace('\n', '\\n\n') dbg.out( to_str( r_.strip().split('\n'), pre='<|' ) ) + if r is None: + dbg.out( 'timeout' ) + r = '' return r def read_to(k): @@ -42,11 +45,18 @@ if not r_: break r += r_ - return r + return r def read_to_prompt(): return read_to( 'ftp> ' ) + def add_prm( cmd, prm='' ): + return cmd + ' ' + prm if prm else cmd + + def do_cmd(cmd): + write( cmd ) + return read_to_prompt() + def login(): lst = [ 'open {}'.format( host ), @@ -55,8 +65,7 @@ 'verb off', 'prompt off', ] - write( to_str(lst) ) - return read_to_prompt() + return do_cmd( to_str(lst) ) def quit(): write('quit') @@ -64,12 +73,9 @@ proc.kill() proc.wait() - do_cmd = lambda cmd, prm='': write( cmd + ( ' ' + prm if prm else '' ) ) - def ls(path=''): - do_cmd( 'ls', path ) - r = read_to_prompt() - return r if r else '' + cmd = add_prm( 'ls', path ) + return do_cmd( cmd ) def cd(path, lcd=True, back=False): r = '' @@ -77,16 +83,24 @@ if back: n = path.count('/') + 1 if path else 0 path = to_str( ['..'] * n, delim='/' ) - r += do_cmd( 'cd', path ) + cmd = add_prm( 'cd', path ) + do_cmd( cmd ) + r += cmd + '\n' if lcd: - r += do_cmd( 'lcd', path ) + cmd = add_prm( 'lcd', path ) + do_cmd( cmd ) + r += cmd + '\n' return r def do_cd_cmd(cmd, path, lcd=True): (dir_, name) = os.path.split( path ) - r = '' + r = '' r += cd( dir_, lcd ) - r += do_cmd( cmd, name ) + + cmd = add_prm( cmd, name ) + do_cmd( cmd ) + r += cmd + '\n' + r += cd( dir_, lcd, back=True ) return r @@ -107,14 +121,23 @@ if dir_: r += mkdir( dir_ ) if not is_exist( path ): - r += do_cmd( 'mkdir', path ) - read_to_prompt() + cmd = add_prm( 'mkdir', path ) + do_cmd( cmd ) + r += cmd + '\n' return r def rmdir(path): - return do_cmd( 'rmdir', path ) + cmd = add_prm( 'rmdir', path ) + do_cmd( cmd ) + return cmd + '\n' + + def cut_dot(lst): + return list( filter( lambda s: s not in ( './', '../' ), lst ) ) - def get_lst(path=''): + def cut_tail(lst, c): + return list( map( lambda s: s[:-1] if s and s[-1] == c else s, lst ) ) + + def get_lst(path='', dot=True): r = ls(path) if path else ls() lst = r.strip().split('\n') lst = map( lambda s: s.split(), lst ) @@ -129,13 +152,41 @@ if ws[0][0] == 'd': nm += '/' rlst.append( nm ) - return rlst + return rlst if dot else cut_dot( rlst ) def lstf(path=''): return to_str( get_lst( path ), last=True ) + def rm_rf(path): + s = get_kind( path ) + if not s: + return '' + + if s == 'f': + return del_( path ) + + lst = s.split( '\n' ) + if not lst or lst[0] != 'd': + return '' + + r = '' + lst = cut_tail( lst[1:], '/' ) + for name in lst: + r += rm_rf( path + '/' + name ) + r += rmdir( path ) + return r + def is_exist(path): - return get_lst(path) + return get_lst( path ) + + def get_kind(path): + lst = get_lst( path ) + if not lst: + return '' # no entry + if './' not in lst and '../' not in lst: + return 'f' # file + lst = cut_dot( lst ) + return to_str( [ 'd' ] + lst ) # directory, and contents def do_paths(f, paths): r = '' @@ -190,6 +241,7 @@ ' host user passwd rmdir path ..', ' host user passwd ls path ..', ' host user passwd lst path ..', + ' host user passwd rm_rf path ..', '', ' site_name in ftp_ut.yaml, can ommit host user passwd' ] @@ -227,7 +279,7 @@ ( p0, p1_ ) = ( paths[0], paths[1:] ) quiet = p0 in ( 'ls', 'lst' ) - r = ftp.login() + ftp.login() if not quiet: dbg.out('login') @@ -247,6 +299,8 @@ r = ftp.do_paths( ftp.ls, p1_ ) if p1_ else ftp.ls() elif p0 == 'lst': r = ftp.do_paths( ftp.lstf, p1_ ) if p1_ else ftp.lstf() + elif p0 == 'rm_rf': + r = ftp.do_paths( ftp.rm_rf, p1_ ) else: r = ftp.do_paths( ftp.put, p1_ if p0 == 'put' else paths ) dbg.out( r, '' )