File size: 2,105 Bytes
a02e3b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | """Keep mutable experiment I/O off the unreliable persistent filesystem."""
import fcntl,json,os,pathlib,shutil,time
ROOT=pathlib.Path(__file__).resolve().parents[1]
LOCAL=ROOT/'.release_local';FROZEN=ROOT/'.robottt_persistent';ACTIVE=pathlib.Path('/tmp/robottt-active')
def activate():
with pathlib.Path('/tmp/robottt-active-bootstrap.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX)
if LOCAL.is_symlink() and LOCAL.resolve()==ACTIVE and (ACTIVE/'.workspace-ready.json').exists():return not json.loads((ACTIVE/'.workspace-ready.json').read_text()).get('remote_restored',False)
source=FROZEN if FROZEN.exists() else LOCAL
if not source.is_dir():raise RuntimeError('Persistent bootstrap snapshot missing; restore assets from HF')
if shutil.disk_usage('/tmp').free<195*2**30:raise RuntimeError('Insufficient ephemeral headroom for the bounded working copy')
temporary=ACTIVE.with_name(ACTIVE.name+'.copy-'+str(os.getpid()))
links={}
def copy(src,dst):
stat=os.stat(src);key=(stat.st_dev,stat.st_ino)
if stat.st_nlink>1 and key in links:os.link(links[key],dst)
else:
shutil.copy2(src,dst)
if stat.st_nlink>1:links[key]=dst
return dst
shutil.copytree(source,temporary,symlinks=True,copy_function=copy,ignore=shutil.ignore_patterns('__pycache__','hf-backup-staging','hf-assets-staging'))
(temporary/'.workspace-ready.json').write_text(json.dumps(dict(source=str(source),created_unix_time=time.time())))
if ACTIVE.exists():raise RuntimeError('Unexpected incomplete active directory; refusing overwrite')
temporary.replace(ACTIVE)
if not FROZEN.exists():LOCAL.rename(FROZEN)
if LOCAL.is_symlink():LOCAL.unlink()
elif LOCAL.exists():raise RuntimeError('Unexpected live workspace directory')
LOCAL.symlink_to(ACTIVE,target_is_directory=True)
print('Mutable experiment I/O:',ACTIVE,'; immutable bootstrap:',FROZEN,flush=True)
return True
if __name__=='__main__':activate()
|