from glob import glob import re import os import sys import shutil import time def instdir(instrument=''): if instrument != '': instrument += '/' return '%s/sea/%s' % (os.path.expanduser("~"), instrument) COMPRESS = 1 EXPAND = 2 def get_trash(instrument): return os.path.expanduser(f'~/sea_logger_trash_{instrument}') def treat(instrument, yearlimit): trash = get_trash(instrument) logbase = '%slogger/' % instdir(instrument) os.chdir(logbase) print('chdir', logbase) os.makedirs(trash, exist_ok=True) for ynum in range(2007, 2040): year = str(ynum) cyear = f'{year}.tar.lz4' if year < yearlimit: if os.path.exists(year): if os.path.exists(cyear): print(cyear, 'exists already') else: os.system(f'tar cf - {year} | lz4 - {cyear}') print(cyear, 'created') if os.path.exists(f'{trash}/{year}'): shutil.rmtree(f'{trash}/{year}') os.rename(year, f'{trash}/{year}') else: print('no', year) else: if os.path.exists(cyear): if os.path.exists(year): print(year, 'exists already') else: os.system(f'lz4 -d {cyear} | tar xf -') print(year, 'created') os.rename(cyear, f'{trash}/{cyear}') if __name__ == '__main__': action = COMPRESS year = time.strftime("%Y") instruments = [] cleanup = True for opt in sys.argv[1:]: if opt == '-x': action = EXPAND cleanup = False elif opt == '-c': cleanup = False else: if opt.startswith('2'): year = opt else: instruments.append(opt) if len(instruments) == 0: instruments = [''] for instrument in instruments: if not os.path.isdir(instdir(instrument)): raise ValueError('%s is not an instrument on this machine' % instrument) removedirs = ( 'device.force_status_save', 'nv.autoflow.*buf', 'nv.autoflow.*min', 'nv.autoflow.*max', 'table.val_*', '*.*.stddev', 'device.ccu4version', 'cc.f', 'cc.nfb', 'cc.hfb', 'graph_*', ) print("instruments: %s" % ", ".join(instruments)) print("compress years before %s" % year) for instrument in instruments: print('instrument:', instdir(instrument)) if cleanup: for pat in removedirs: print('remove', pat) gap = 0 t0 = time.time() t1 = t0 for dir in glob(instdir(instrument)+'logger/*/'+pat): shutil.rmtree(dir) now = time.time() if now > t0 + 5: if gap > 0: print("+%d" % gap) print(dir) else: gap += 1 time.sleep(min(1.0, now - t1)) # be nice t1 = now treat(instrument, year) print('if you are sure nothing is lost, you may delete loggertrash:') for instrument in instruments: trash = get_trash(instrument) print(f' rm -r {trash}')