diff -ur v7/ftp_ut.py v8/ftp_ut.py --- v7/ftp_ut.py 2020-02-15 11:46:49.000000000 +0900 +++ v8/ftp_ut.py 2020-02-15 17:18:27.000000000 +0900 @@ -19,13 +19,33 @@ def write(s): if debug: - dbg.out(s) + dbg.out( '|>'+s ) s += '\n' proc.write(s) return s def read(): - return proc.read() + r = proc.read() + if r and '\r' in r: + r = r.replace( '\r', '' ) + if r and debug: + r_ = r.replace('\n', '\\n\n') + dbg.out( to_str( r_.strip().split('\n'), pre='<|' ) ) + return r + + def read_to(k): + r = read() + if not r: + return r + while not r.endswith( k ): + r_ = read() + if not r_: + break + r += r_ + return r + + def read_to_prompt(): + return read_to( 'ftp> ' ) def login(): lst = [ @@ -36,22 +56,19 @@ 'prompt off', ] write( to_str(lst) ) - return read() + return read_to_prompt() def quit(): write('quit') - r = read() - if debug: - dbg.out( r, '' ) - dbg.out + read() proc.kill() proc.wait() - do_cmd = lambda cmd, prm='': write( to_str( (cmd, prm), delim=' ' ) ) + do_cmd = lambda cmd, prm='': write( cmd + ( ' ' + prm if prm else '' ) ) def ls(path=''): do_cmd( 'ls', path ) - r = read() + r = read_to_prompt() return r if r else '' def cd(path, lcd=True, back=False): @@ -74,23 +91,30 @@ return r def put(path): - return do_cd_cmd( 'put', path ) + r = '' + (dir_, name) = os.path.split( path ) + if not is_exist( dir_ ): + r += mkdir( dir_ ) + r += do_cd_cmd( 'put', path ) + return r def del_(path): return do_cd_cmd( 'del', path, lcd=False ) def mkdir(path): r = '' - nms = path.split('/') - for i in range( len(nms) ): - path_ = to_str( nms[:i+1], delim='/' ) - r += do_cmd( 'mkdir', path_ ) + (dir_, name) = os.path.split( path ) + if dir_: + r += mkdir( dir_ ) + if not is_exist( path ): + r += do_cmd( 'mkdir', path ) + read_to_prompt() return r def rmdir(path): return do_cmd( 'rmdir', path ) - def lstf(path=''): + def get_lst(path=''): r = ls(path) if path else ls() lst = r.strip().split('\n') lst = map( lambda s: s.split(), lst ) @@ -102,12 +126,16 @@ rlst = [] for ws in lst: nm = ws[-1] - if nm in ('.', '..'): - continue if ws[0][0] == 'd': nm += '/' rlst.append( nm ) - return to_str( rlst, last=True ) + return rlst + + def lstf(path=''): + return to_str( get_lst( path ), last=True ) + + def is_exist(path): + return get_lst(path) def do_paths(f, paths): r = '' @@ -173,7 +201,7 @@ site = a.pop_str('-s') v = get_site( site ) - (host, usr, passwd, cd) = ('', '', '', '') + (host, uesr, passwd, cd) = ('', '', '', '') if v: if type(v) == str: dbg.err('err in .yaml, ' + v) @@ -201,8 +229,6 @@ r = ftp.login() if not quiet: - if r and debug: - dbg.out( r, '' ) dbg.out('login') if cd: