updated HistoryGC.files()

This commit is contained in:
Hugo Wang 2016-11-22 08:33:52 +08:00
parent 6f0c635c93
commit d392a6f454

View file

@ -2,7 +2,6 @@
"""Implements the xonsh history object.""" """Implements the xonsh history object."""
import os import os
import sys import sys
import glob
import json import json
import time import time
import uuid import uuid
@ -70,12 +69,16 @@ def _gc_bytes_to_rmfiles(hsize, files):
return rmfiles return rmfiles
def _get_history_files(reverse=False): def _get_history_files(sort=True, reverse=False):
"""Find and return the history files. Optionally sort files by
modify time.
"""
data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR')
data_dir = expanduser_abs_path(data_dir) data_dir = expanduser_abs_path(data_dir)
files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) files = [os.path.join(data_dir, f) for f in os.listdir(data_dir)
if f.startswith('xonsh-') and f.endswith('.json')] if f.startswith('xonsh-') and f.endswith('.json')]
files.sort(key=lambda x: os.path.getmtime(x), reverse=reverse) if sort:
files.sort(key=lambda x: os.path.getmtime(x), reverse=reverse)
return files return files
@ -120,20 +123,22 @@ class HistoryGC(threading.Thread):
"""Find and return the history files. Optionally locked files may be """Find and return the history files. Optionally locked files may be
excluded. excluded.
This is sorted by the last closed time. Returns a list of (timestamp, This is sorted by the last closed time. Returns a list of
file) tuples. (timestamp, number of cmds, file name) tuples.
""" """
# pylint: disable=no-member # pylint: disable=no-member
env = getattr(builtins, '__xonsh_env__', None) env = getattr(builtins, '__xonsh_env__', None)
if env is None: if env is None:
return [] return []
xdd = env.get('XONSH_DATA_DIR')
xdd = expanduser_abs_path(xdd)
fs = [f for f in glob.iglob(os.path.join(xdd, 'xonsh-*.json'))] fs = _get_history_files(sort=False)
files = [] files = []
for f in fs: for f in fs:
try: try:
if os.path.getsize(f) == 0:
# collect empty files (for gc)
files.append((time.time(), 0, f))
continue
lj = LazyJSON(f, reopen=False) lj = LazyJSON(f, reopen=False)
if only_unlocked and lj['locked']: if only_unlocked and lj['locked']:
continue continue