-
Notifications
You must be signed in to change notification settings - Fork 2
/
runcat.py
89 lines (78 loc) · 2.48 KB
/
runcat.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
import sys
import os
import argparse
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed
# import keyboard as k
# k.add_hotkey("ctrl+c", lambda: quit())
def help():
print('''
|\ _,,,---,,_
/,`.-'`' -. ;-;;,_ RUN CAT RUN!!
|,4- ) )-,_..;\ ( `'-'
'---''(_/--' `-'\_)
OPTIONS:
-h Kill your system
-t Threads
-m Work with threads, either 0/1 (default 0)
0 - ThreadPool/Lightweight
1 - ProcessPool/Heavyweight
eg. - screenshotting tools
- tool using huge files
-seq Print output sequence-wise
Command 1 > Output 1
Command 2 > Output 2
.. ..
.. ..
Command n > Output n
-skip Skip commands to print with output
ASCII ART:
https://user.xmission.com/~emailbox/ascii_cats.htm''')
sys.exit(0)
def parser_error(errmsg):
print("Usage: python " + sys.argv[0] + " [Options] use -h for help")
print("Error: " + errmsg)
sys.exit()
def parse_args():
parser = argparse.ArgumentParser(add_help=False)
parser.error = parser_error
parser._optionals.title = "OPTIONS"
parser.add_argument('-t', type=int, default=1)
parser.add_argument('-m', type=int, default=0)
parser.add_argument('-seq', default=False, action='store_true')
parser.add_argument('-h', default=False, action='store_true')
return parser.parse_args()
def do(cmd):
try:
print("Running... ",cmd)
data = os.popen(cmd).read()
return data
except (KeyboardInterrupt, SystemExit):
print('Closing Thread')
return
def run(executor ,seq):
if seq:
results = executor.map(do, sys.stdin)
for r in results:
print(r)
else:
results = [executor.submit(do, cmd) for cmd in sys.stdin]
for f in as_completed(results):
print(f.result())
def main():
args = parse_args()
if args.h:
help()
if args.t == 1:
for cmd in sys.stdin:
print(do(cmd))
else:
if args.m == 0:
with ThreadPoolExecutor(args.t) as executor:
run(executor, args.seq)
elif args.m == 1:
with ProcessPoolExecutor(args.t) as executor:
run(executor, args.seq)
else:
print("m can be 0 or 1, check usage")
if __name__ == '__main__':
main()