-
Notifications
You must be signed in to change notification settings - Fork 1
/
hgrep.py
executable file
·102 lines (87 loc) · 3.37 KB
/
hgrep.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# grep the history
import sys
import argparse
import subprocess
import glob
import os.path
import datetime
class colours:
BLUE = '\033[94m'
GREEN = '\033[92m'
ENDC = '\033[0m'
# ----- command line parsing -----
parser = argparse.ArgumentParser(
description="Greps the history with optional date restrictions. \
The date restrictions support absolute numbers for year, month and \
day, and also relative to today, with - meaning this year/month/day \
and -n meaning n years/months/days ago.")
parser.add_argument("string", type=str,
help="The search string.")
parser.add_argument("year", type=str, nargs="?", default="",
help="Restrict search to YEAR.")
parser.add_argument("month", type=str, nargs="?", default="",
help="Restrict search to MONTH.")
parser.add_argument("day", type=str, nargs="?", default="",
help="Restrict search to DAY.")
parser.add_argument("--color", type=str, choices=["never","always","auto"],
default="auto", help="Use colour output.")
parser.add_argument("--histdir", type=str, default="~/.history",
help="History directory location (default ~/.history)")
args = parser.parse_args()
# ----- end command line parsing -----
if len(args.year) == 1 and args.year[0] == "-":
year = str(datetime.date.today().year)
elif len(args.year) > 1 and args.year[0] == "-":
year = str(datetime.date.today().year - int(args.year[1:]))
else:
year = args.year
if len(args.month) == 1 and args.month[0] == "-":
month = str(datetime.date.today().month)
elif len(args.month) > 1 and args.month[0] == "-":
month = str(datetime.date.today().month - int(args.month[1:]))
elif len(args.month) == 1:
month = "0" + args.month
else:
month = args.month
if len(args.day) == 1 and args.day[0] == "-":
day = str(datetime.date.today().day)
elif len(args.day) > 1 and args.day[0] == "-":
day = str(datetime.date.today().day - int(args.day[1:]))
elif len(args.day) == 1:
day = "0" + args.day
else:
day = args.day
historydir = os.path.expanduser(args.histdir)
if not os.path.isdir(historydir):
sys.stderr.write(historydir + " does not exist.\n")
exit(2)
files = sorted(glob.glob(historydir + "/*" +
year + "/*" + month + "/" + day + "*"))
if args.color == "always" or args.color =="auto" and sys.stdout.isatty():
colourarg = "--color=always"
else:
colourarg = "--color=never"
if len(files) > 0:
try:
output = subprocess.check_output(["grep", colourarg, args.string] + files)
except subprocess.CalledProcessError as e:
# grep finds nothing
exit(e.returncode)
else:
exit(1)
matches = output.strip().split("\n")
for match in matches:
[location,string] = match.split(":", 1)
[year,month,daytimehost] = location.split("/")[-3:]
[daytime,host,pid] = daytimehost.split("_",2)
[day,hour,minute,second] = daytime.split(".",3)
timestamp = "({:s}-{:s}-{:s} {:s}:{:s})".format(
year, month, day, hour, minute)
if args.color == "always" or args.color =="auto" and sys.stdout.isatty():
sys.stdout.write(colours.GREEN + host + colours.ENDC + " ")
sys.stdout.write(colours.BLUE + timestamp + colours.ENDC)
else:
sys.stdout.write(host + " ")
sys.stdout.write(timestamp)
sys.stdout.write(": " + string + "\n")