diff -ur v1/fsyn.py v2/fsyn.py --- v1/fsyn.py 2020-04-06 00:24:24.000000000 +0900 +++ v2/fsyn.py 2020-04-19 15:43:08.000000000 +0900 @@ -27,17 +27,23 @@ def fsyn_new(dir_path): - fsyn_path = dir_path + '.fsyn' - id_fn = lambda id, name: path_join( fsyn_path, id, name ) + def fsyn_path(dir_=None): + if not dir_: + return dir_path + '.fsyn' + + ( DIR, BASE ) = os.path.split( dir_path ) + return path_join( dir_, BASE ) + '.fsyn' + + id_fn = lambda id, name, dir_=None: path_join( fsyn_path( dir_ ), id, name ) def set_curr(id): - fn = path_join( fsyn_path, 'curr' ) + fn = path_join( fsyn_path(), 'curr' ) cmd = 'echo {} > {}'.format( id, fn ) cmd_call( cmd ) def get_curr(): - fn = path_join( fsyn_path, 'curr' ) + fn = path_join( fsyn_path(), 'curr' ) cmd = 'cat ' + fn return cmd_call( cmd ).strip() @@ -56,24 +62,24 @@ f = lambda name: path_exists( id_fn( id, name ) ) return all( map( f, ( 'link', 'snap' ) ) ) - def get_link(id): - fn = id_fn( id, 'link' ) + def get_link(id, dir_=None): + fn = id_fn( id, 'link', dir_ ) if path_exists( fn ): d = yaml_ut.load_fn( fn ) return empty.new( d ) return empty.new( from_='', to=[] ) # ! - def put_link(id, link): - fn = id_fn( id, 'link' ) + def put_link(id, link, dir_=None): + fn = id_fn( id, 'link', dir_ ) d = vars( link ) yaml_ut.save( d, fn ) def create(): - if path_exists( fsyn_path ): - dbg.err_exit( 'exists {}'.format( fsyn_path ) ) + if path_exists( fsyn_path() ): + dbg.err_exit( 'exists {}'.format( fsyn_path() ) ) id = 'org' - cmd = 'mkdir -p ' + path_join( fsyn_path, id ) + cmd = 'mkdir -p ' + path_join( fsyn_path(), id ) cmd_call( cmd ) snap = get_snap( 'now' ) @@ -88,7 +94,7 @@ set_curr( id ) - msg_done( 'create', fsyn_path ) + msg_done( 'create', fsyn_path() ) def get_curr_cmp(): id_old = get_curr() @@ -120,7 +126,7 @@ id_old = curr_cmp.id_old cmp = curr_cmp.cmp - cmd = 'mkdir ' + path_join( fsyn_path, id ) + cmd = 'mkdir ' + path_join( fsyn_path(), id ) cmd_call( cmd ) fn = id_fn( id, 'snap' ) @@ -161,7 +167,7 @@ dbg.out( msg ) def ids_in_dir(): - cmd = 'ls ' + fsyn_path + cmd = 'ls ' + fsyn_path() lst = cmd_lst( cmd ) f = lambda s: s == 'org' or tm_ut.is_date_time_str( s, tm_ut.sample ) return list( filter( f, lst ) ) @@ -202,7 +208,7 @@ def ckout(id): (targ_id, id) = ( id, get_curr() ) - if not path_exists( path_join( fsyn_path, targ_id ) ): + if not path_exists( path_join( fsyn_path(), targ_id ) ): msg = 'not found ' + targ_id dbg.err_exit( msg ) @@ -310,6 +316,43 @@ for id in id_lst: fix_link( id ) + def push(to_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( fsyn_DIR, fn ) ): + return # no src + if not path_exists( path_join( to_path, fn ) ) or force: + cmd = 'tar cf - -C {} {} | tar xf - -C {}'.format( fsyn_DIR, fn, to_path ) + cmd_call( cmd ) + + def push_id(id, link_lmt=''): + copy( id, 'snap' ) + copy( id, 'update.tgz' ) + + 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_ + for s in link.to: + if s not in link_d.to: + link_d.to.append( s ) + put_link( id, link_d, to_path ) + + def push_to(id): + push_id( id ) + link = get_link( id ) + for s in link.to: + push_to( s ) + + lst = is_ancestor_lst( 'org', get_curr() ) # [ 'org', ... ] + for i in range( len( lst ) - 1 ): + push_id( lst[ i ], lst[ i + 1 ] ) + + push_to( get_curr() ) + return empty.new( locals() ) @@ -326,6 +369,7 @@ cmd_new( 'checkout', [ 'id' ] ), cmd_new( 'check', comment='show dirty' ), cmd_new( 'fixlink', comment='fix link info' ), + cmd_new( 'push', [ 'to_path' ] ), ] dic = dict( map( lambda cmd: ( cmd.name, cmd ), cmds ) )