-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlastCommands.py
More file actions
executable file
·128 lines (99 loc) · 3.64 KB
/
lastCommands.py
File metadata and controls
executable file
·128 lines (99 loc) · 3.64 KB
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
#!/usr/bin/env python3
#
# lastCommands.py: Re-un the last N commands from the shell history.
# 2014-06-19: Written by Steven J. DeRose.
#
import sys
import os
import re
import codecs
from subprocess import check_output
__metadata__ = {
"title" : "lastCommands",
"description" : "Re-un the last N commands from the shell history.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.7",
"created" : "2014-06-19",
"modified" : "2021-02-23",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__["modified"]
descr = """
=Usage=
Extract and execute the last N bash commands. This should be invoked via
a shell function that saves the history somewhere, and then passes the
path to it. Otherwise it's hard to get the truly up-to-date history.
=Related Commands=
=Known bugs and Limitations=
This can't change state of the calling shell, such as environment variables.
=History=
* 2014-06-19: Written by Steven J. DeRose.
* 2021-02-23: New layout. Default to asking shell for history.
=Rights=
Copyright 2014-06-19 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share-alike 3.0 unported license.
For further information on this license, see
[https://creativecommons.org/licenses/by-sa/3.0].
For the most recent version, see [http://www.derose.net/steve/utilities]
or [https://github.com/sderose].
=Options=
"""
###############################################################################
# Main
#
if __name__ == "__main__":
import argparse
def processOptions():
try:
from BlockFormatter import BlockFormatter
parser = argparse.ArgumentParser(
description=descr, formatter_class=BlockFormatter)
except ImportError:
parser = argparse.ArgumentParser(description=descr)
parser = argparse.ArgumentParser(
description=descr)
parser.add_argument(
"--histfile", type=str,
help="What history file to use.")
parser.add_argument(
"--quiet", "-q", action="store_true", dest="quiet",
help="Suppress most messages.")
parser.add_argument(
"--verbose", "-v", action="count", default=0,
help="Add more messages (repeatable).")
parser.add_argument(
"--version", action="version", version="Version of "+__version__,
help="Display version information, then exit.")
parser.add_argument(
"n", type=int,
help="How many commands to re-do.")
args0 = parser.parse_args()
return args0
args = processOptions()
ignoreList = [
"lastCommands", "lastCommands.py"
]
if (args.histfile):
with codecs.open(args.histfile, "rb", encoding="utf-8") as ifh:
hist = ifh.read()
else:
hist = check_output("history %d" % (args.n), shell=True)
todo = []
while (True):
rec = hist.split().strip()
if (rec == ""): break
if (len(todo) >= args.n): todo.pop()
rec = re.sub(r"^\s*\d+\s+","",rec)
cmd = re.sub(r"\s.*","",rec)
if (cmd not in ignoreList): todo.append(rec)
if (args.verbose):
sys.stderr.write("\n".join(todo) + "\n")
for c in (todo):
if (True or args.verbose):
sys.stderr.write("\nRunning: %s\n" % (c))
os.system(c)
if (args.verbose):
sys.stderr.write("\nlastCommand.py: %d one.\n" % (args.n))