def run(self): while self.wait_for_shell: time.sleep(0.01) env = builtins.__xonsh_env__ if self.size is None: hsize, units = env.get("XONSH_HISTORY_SIZE") else: hsize, units = to_history_tuple(self.size) files = self.unlocked_files() # flag files for removal if units == "commands": n = 0 ncmds = 0 rmfiles = [] for ts, fcmds, f in files[::-1]: if fcmds == 0: # we need to make sure that 'empty' history files don't hang around fmfiles.append((ts, fcmds, f)) if ncmds + fcmds > hsize: break ncmds += fcmds n += 1 rmfiles += files[:-n] elif units == "files": rmfiles = files[:-hsize] if len(files) > hsize else [] elif units == "s": now = time.time() rmfiles = [] for ts, _, f in files: if (now - ts) < hsize: break rmfiles.append((None, None, f)) elif units == "b": n = 0 nbytes = 0 for _, _, f in files[::-1]: fsize = os.stat(f).st_size if nbytes + fsize > hsize: break nbytes += fsize n += 1 rmfiles = files[:-n] else: raise ValueError("Units of {0!r} not understood".format(unit)) # finally, clean up files for _, _, f in rmfiles: try: os.remove(f) except OSError: pass
def run(self): while self.wait_for_shell: time.sleep(0.01) env = builtins.__xonsh_env__ if self.size is None: hsize, units = env.get('XONSH_HISTORY_SIZE') else: hsize, units = to_history_tuple(self.size) files = self.unlocked_files() # flag files for removal if units == 'commands': n = 0 ncmds = 0 rmfiles = [] for ts, fcmds, f in files[::-1]: if fcmds == 0: # we need to make sure that 'empty' history files don't hang around fmfiles.append((ts, fcmds, f)) if ncmds + fcmds > hsize: break ncmds += fcmds n += 1 rmfiles += files[:-n] elif units == 'files': rmfiles = files[:-hsize] if len(files) > hsize else [] elif units == 's': now = time.time() rmfiles = [] for ts, _, f in files: if (now - ts) < hsize: break rmfiles.append((None, None, f)) elif units == 'b': n = 0 nbytes = 0 for _, _, f in files[::-1]: fsize = os.stat(f).st_size if nbytes + fsize > hsize: break nbytes += fsize n += 1 rmfiles = files[:-n] else: raise ValueError('Units of {0!r} not understood'.format(unit)) # finally, clean up files for _, _, f in rmfiles: try: os.remove(f) except OSError: pass
def run(self): while self.wait_for_shell: time.sleep(0.01) if self.size is not None: hsize, units = xt.to_history_tuple(self.size) else: envs = builtins.__xonsh_env__ hsize, units = envs.get('XONSH_HISTORY_SIZE') if units != 'commands': print('sqlite backed history gc currently only supports ' '"commands" as units', file=sys.stderr) return if hsize < 0: return xh_sqlite_delete_items(hsize, filename=self.filename)
def run(self): while self.wait_for_shell: time.sleep(0.01) env = builtins.__xonsh_env__ # pylint: disable=no-member if self.size is None: hsize, units = env.get('XONSH_HISTORY_SIZE') else: hsize, units = to_history_tuple(self.size) files = self.files(only_unlocked=True) rmfiles_fn = self.gc_units_to_rmfiles.get(units) if rmfiles_fn is None: raise ValueError('Units type {0!r} not understood'.format(units)) for _, _, f in rmfiles_fn(hsize, files): try: os.remove(f) except OSError: pass
def run(self): while self.wait_for_shell: time.sleep(0.01) env = builtins.__xonsh__.env # pylint: disable=no-member if self.size is None: hsize, units = env.get("XONSH_HISTORY_SIZE") else: hsize, units = xt.to_history_tuple(self.size) files = self.files(only_unlocked=True) rmfiles_fn = self.gc_units_to_rmfiles.get(units) if rmfiles_fn is None: raise ValueError("Units type {0!r} not understood".format(units)) for _, _, f in rmfiles_fn(hsize, files): try: os.remove(f) except OSError: pass
def run(self): while self.wait_for_shell: time.sleep(0.01) if self.size is not None: hsize, units = xt.to_history_tuple(self.size) else: envs = XSH.env hsize, units = envs.get("XONSH_HISTORY_SIZE") if units != "commands": print( "sqlite backed history gc currently only supports " '"commands" as units', file=sys.stderr, ) return if hsize < 0: return xh_sqlite_delete_items(hsize, filename=self.filename)
def run(self): while self.wait_for_shell: time.sleep(0.01) env = builtins.__xonsh__.env # pylint: disable=no-member xonsh_debug = env.get("XONSH_DEBUG", 0) if self.size is None: hsize, units = env.get("XONSH_HISTORY_SIZE") else: hsize, units = xt.to_history_tuple(self.size) files = self.files(only_unlocked=True) rmfiles_fn = self.gc_units_to_rmfiles.get(units) if rmfiles_fn is None: raise ValueError("Units type {0!r} not understood".format(units)) size_over, rm_files = rmfiles_fn(hsize, files) hist = getattr(builtins.__xonsh__, "history", None) if hist is not None: # remember last gc pass history size hist.hist_size = size_over + hsize hist.hist_units = units if self.force_gc or size_over < hsize: i = 0 for _, _, f, _ in rm_files: try: os.remove(f) if xonsh_debug: print( f"... Deleted {i:7d} of {len(rm_files):7d} history files.\r", end="", ) pass except OSError: pass i += 1 else: print( f"Warning: History garbage collection would discard more history ({size_over} {units}) than it would keep ({hsize}).\n" "Not removing any history for now. Either increase your limit ($XONSH_HISTORY_SIZE), or run `history gc --force`." )