Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- #
- """ this searches history files """
- from __future__ import print_function
- import glob
- import os
- import re
- import sys
- import time
- import optparse
- # help='an integer for the accumulator')
- # help='sum the integers (default: find the max)')
- # print(sys.version_info[0])
- # from importlib import abc
- def main(ignore_case, quiet, query_list):
- """main routine"""
- query_regex_list = []
- tmp = []
- for pattern in query_list:
- if ignore_case:
- query_regex_list.append(re.compile(pattern, re.IGNORECASE))
- else:
- query_regex_list.append(re.compile(pattern))
- for histfile in glob.glob(os.environ['HOME'] + '/.zsh/.zsh_history,*'):
- filestring = re.sub('^.zsh_history,(.*)', r'\1', os.path.split(histfile)[1])
- filestring = re.sub('^(.*).archive$', r'\1', filestring)
- if sys.version_info[0] == 3:
- history = open(histfile, 'r', encoding='ISO-8859-1')
- if sys.version_info[0] == 2:
- history = open(histfile, 'r')
- for line in history:
- # print ("%s %s" % (histfile, line))
- match("%s::%s" % (line, filestring), tmp, query_regex_list)
- history.close()
- tmp = list(set(tmp))
- tmp.sort()
- timeregex = re.compile(r':.(\d{10}):[0-9]+;(.*)\n::(.*)')
- for line in tmp:
- x = timeregex.search(line)
- try:
- if quiet:
- print(x.group(2))
- else:
- print(time.strftime("%Y%m%d %H:%M:%S", time.localtime(float(x.group(1)))), x.group(3), x.group(2))
- except AttributeError:
- sys.stderr.write('error parsing the following line\n' + line + '\n')
- sys.exit(1)
- def match(data, tmp, query):
- """this matches the line in the history with the query"""
- for pattern in query:
- if not pattern.search(data):
- return
- tmp.append(data)
- if __name__ == '__main__':
- usage = "usage: %prog -i query1 query2 query3..."
- parser = optparse.OptionParser(usage)
- parser.allow_interspersed_args = False
- parser.add_option("-i", "--ignore-case",
- action="store_true",
- dest="ignore_case",
- help="ignore case")
- parser.add_option("-q", "--quiet",
- action="store_true",
- dest="quiet",
- help="quiet output")
- (options, args) = parser.parse_args()
- # fixme: do not use len(SEQUENCE) to determine if somethin is empty"
- if len(args) == 0:
- parser.error("incorrect number of arguments")
- main(options.ignore_case, options. Quiet, args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement