This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbash_history_merge.py
executable file
·187 lines (148 loc) · 5.28 KB
/
bash_history_merge.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2020 Kevin Deldycke <[email protected]>
# All Rights Reserved.
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" Merge multiple `.bash_history` files and deduplicate their content.
Produces a consolidated output on STDOUT with all entries sorted by timestamps.
During the deduplication process, historized command lines are normalized by
stripping their leading and trailing blank characters. Empty history entries
will be ignored.
Are considered duplicate entries those sharing the exact same timestamp and
normalized command line.
Timestamp-less entries will be set to epoch. And their natural order will be
preserved. On deduplication, only the last occurence will be kept.
Requires the `boltons` python module, installable via `pip`:
Timestamp-less entries will be set to epoch.
$ pip install boltons
Usage example:
$ python bash_history_merge.py "~/.bash_history" ".histcopy" > .merged_hist
TODO: Add option to deduplicate lines, whatever their timestamp, by only
keeping the lastest occurence.
"""
from __future__ import print_function
from itertools import chain
from operator import itemgetter
from os import path
from io import StringIO
import sys
from textwrap import dedent
from boltons.setutils import IndexedSet
# The minimal timestamp value supported by Bash's history is (epoch + 1):
#
# $ head ~/.bash_history
# #0
# ls
# #1
# ls
# #2
# ls
#
# $ history | head
# 1 ??ls
# 2 [1970-01-01 01:00:01] ls
# 3 [1970-01-01 01:00:02] ls
#
# See bash source code at:
# https://git.savannah.gnu.org/cgit/bash.git/tree/builtins/history.def#n267
MIN_TIMESTAMP = 1
def parse_history(fd):
""" Parse an history file, normalize its timestamp and command lines. """
# Timestamp value of the line immediately above. Default to minimal
# timestamp value.
timestamp_line_above = MIN_TIMESTAMP
for line in fd:
# Normalize line.
line = line.strip()
# Ignore empty entries.
if not line:
continue
# Parse the line as a timestamp, keep it on the side and go to the next
# entry in the history.
if line.startswith('#'):
timestamp = line[1:].strip()
try:
assert timestamp == str(int(timestamp))
except ValueError:
# This line is not an integer timestamps. Ignore it.
continue
timestamp_line_above = max(int(timestamp), MIN_TIMESTAMP)
continue
# The line here is not empty nor a timestamp. It is a valid entry.
yield (timestamp_line_above, line)
def dedupe(*input_files):
""" Takes file descriptors and return deduplicated content. """
# Parse and merge all files entries.
results = chain.from_iterable(map(parse_history, input_files))
# Deduplicate entries sharing the same timestamp by removing all previous
# occurences, only keeping the last one. A reverse IndexedSet let us keep
# entries ordered by their encounter. This is important, especially to keep
# together timestamp-less entries coming from the same file.
results = IndexedSet(list(results)[::-1])
results.reverse()
# Sort entries by timestamps.
entries = []
for timestamp, cmd in sorted(results, key=itemgetter(0)):
entries.append("#{}\n{}".format(timestamp, cmd))
return '\n'.join(entries)
def test_timestampless_merging():
history_1 = StringIO(dedent("""
tail -Fn 1000 /var/log/syslog | grep "foo"
tail -Fn 10000 /var/log/syslog | grep "foo"
cat /etc/foo.yaml
ll
cat /etc/foo.yaml
tail -Fn 10000 /var/log/syslog | grep "foo"
ll
"""))
history_2 = StringIO(dedent("""
ll
history | grep foo
ll
ll
history | grep bar
ll
ll
"""))
output = dedupe(history_1, history_2)
assert output == dedent("""\
#1
tail -Fn 1000 /var/log/syslog | grep "foo"
#1
cat /etc/foo.yaml
#1
tail -Fn 10000 /var/log/syslog | grep "foo"
#1
history | grep foo
#1
history | grep bar
#1
ll""")
if __name__ == "__main__":
args = sys.argv[1:]
# Run the mini test suite.
if args[0] == '--tests':
test_timestampless_merging()
print("Success! :)")
exit()
# Run the deduplication process for real.
input_files = []
for filepath in args:
filepath = path.normpath(path.expanduser(filepath))
assert path.isfile(filepath)
input_files.append(open(filepath, 'r'))
output = dedupe(*input_files)
print(output)