diff -ur v4/fsyn.py v5/fsyn.py --- v4/fsyn.py 2020-04-26 17:39:47.000000000 +0900 +++ v5/fsyn.py 2020-04-29 20:50:21.000000000 +0900 @@ -17,7 +17,7 @@ to_str = base.to_str msg_done = lambda done, targ: dbg.out( '{} {}'.format( done, targ ) ) -cmd_call = lambda cmd: cmd_ut.call( cmd, b2s=True ) +cmd_call = lambda cmd, dv='': cmd_ut.call( cmd, dv, b2s=True ) cmd_lst = lambda cmd: sorted( cmd_call( cmd ).strip().split('\n') ) def confirm(msg): @@ -71,6 +71,10 @@ def put_link(id, link, dir_=None): fn = id_fn( id, 'link', dir_ ) + (DIR, BASE) = os.path.split( fn ) + if not path_exists( DIR ): + cmd = 'mkdir -p ' + DIR + cmd_call( cmd ) d = vars( link ) yaml_ut.save( d, fn ) @@ -316,14 +320,48 @@ for id in id_lst: fix_link( id ) + # site ut + + (fsyn_DIR, fsyn_BASE) = os.path.split( fsyn_path() ) + + get_site_ut_cmd = ( lambda cmd, fn, cd_dir=fsyn_DIR: + '( cd {} ; python -m site_ut {} -C {} {} )'.format( fsyn_DIR, cmd, cd_dir, fn ) ) + + def site_exists(fn): + cmd = get_site_ut_cmd( 'exists', fn ) + return cmd_call( cmd, 'err' ) != 'err' + + def site_get_link(id): + fn = path_join( fsyn_BASE, id, 'link' ) + if site_exists( fn ): + cmd = get_site_ut_cmd( 'cat', fn ) + d = yaml_ut.load( cmd_call( cmd ) ) + return empty.new( d ) + return empty.new( from_='', to=[] ) # ! + + def com_exists(path, fn): + if path == 'site_ut': + return site_exists( fn ) + return path_exists( path_join( path, fn ) ) + + def com_get_link(id, dir_=None): + if dir_ == 'site_ut': + return site_get_link( id ) + return get_link(id, dir_) + + + def push(to_path): - (fsyn_DIR, fsyn_BASE) = os.path.split( fsyn_path() ) + from_path = to_path + if to_path == 'site_ut': + from_path = 'site_ut' + to_path = cmd_ut.mk_tmp_dir( '/tmp/fsyn_' ) def copy(id, name, force=False): fn = path_join( fsyn_BASE, id, name ) if not path_exists( path_join( fsyn_DIR, fn ) ): return # no src - if not path_exists( path_join( to_path, fn ) ) or force: + if not com_exists( from_path, fn ) or force: cmd = 'tar cf - -C {} {} | tar xf - -C {}'.format( fsyn_DIR, fn, to_path ) cmd_call( cmd ) @@ -334,12 +372,19 @@ link = get_link( id ) if link_lmt: link.to = list( filter( lambda s: s == link_lmt, link.to ) ) - link_d = get_link( id, to_path ) - link_d.from_ = link.from_ + + link_d = com_get_link( id, from_path ) + + dirty = False + if link_d.from_ != link.from_: + link_d.from_ = link.from_ + dirty = True for s in link.to: if s not in link_d.to: link_d.to.append( s ) - put_link( id, link_d, to_path ) + dirty = True + if dirty: + put_link( id, link_d, to_path ) def push_to(id): push_id( id ) @@ -353,22 +398,29 @@ push_to( get_curr() ) + if from_path == 'site_ut': + cmd = get_site_ut_cmd( 'put', fsyn_BASE, cd_dir=to_path ) + dbg.out( 'upload cmd={} ... '.format( cmd ), '' ) + cmd_call( cmd ) + cmd_ut.rm_rf( to_path ) + dbg.out( 'ok' ) + def pull(from_path): - (fsyn_DIR, fsyn_BASE) = os.path.split( fsyn_path() ) - def copy(id, name, force=False): fn = path_join( fsyn_BASE, id, name ) - if not path_exists( path_join( from_path, fn ) ): + if not com_exists( from_path, fn ): return # no src if not path_exists( path_join( fsyn_DIR, fn ) ) or force: cmd = 'tar cf - -C {} {} | tar xf - -C {}'.format( from_path, fn, fsyn_DIR ) + if from_path == 'site_ut': # !!! + cmd = get_site_ut_cmd( 'get', fn ) cmd_call( cmd ) def pull_id(id): copy( id, 'snap' ) copy( id, 'update.tgz' ) - link = get_link( id, from_path ) + link = com_get_link( id, from_path ) link_d = get_link( id ) link_d.from_ = link.from_ for s in link.to: @@ -378,12 +430,12 @@ def pull_from(id): pull_id( id ) - link = get_link( id, from_path ) + link = com_get_link( id, from_path ) for s in link.to: pull_from( s ) id = get_curr() - if path_exists( path_join( from_path, fsyn_BASE, id ) ): + if com_exists( from_path, path_join( fsyn_BASE, id, 'snap' ) ): # ! link pull_from( id )