-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushwrap.py
More file actions
117 lines (92 loc) · 2.65 KB
/
pushwrap.py
File metadata and controls
117 lines (92 loc) · 2.65 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
#!/usr/bin/env python3
import argparse
import subprocess
import sys
import time
import os
import requests
import shlex
SIMPLEPUSH_URL = "https://simplepu.sh"
def send_simplepush(key, title, msg, event):
payload = {
"key": key,
"title": title,
"msg": msg,
"event": event
}
try:
r = requests.post(SIMPLEPUSH_URL, json=payload, timeout=10)
return r.status_code == 200
except Exception:
return False
def main():
parser = argparse.ArgumentParser(
description="Run any command and send Simplepush notification when finished"
)
parser.add_argument(
"--push-key",
help="Simplepush key (or via SIMPLEPUSH_KEY env var)"
)
parser.add_argument(
"--min-seconds",
type=int,
default=0,
help="Only send push if runtime >= this value"
)
parser.add_argument(
"--no-push",
action="store_true",
help="Disable push (dry wrapper run)"
)
parser.add_argument(
"--title-success",
default="Command finished",
help="Push title on success"
)
parser.add_argument(
"--title-failure",
default="Command error",
help="Push title on failure"
)
parser.add_argument(
"command",
nargs=argparse.REMAINDER,
help="Command to run (use -- before command)"
)
args = parser.parse_args()
if not args.command or args.command[0] != "--":
print("[!] You must separate command with --", file=sys.stderr)
sys.exit(2)
cmd = args.command[1:]
if not cmd:
print("[!] No command specified", file=sys.stderr)
sys.exit(2)
push_key = args.push_key or os.getenv("SIMPLEPUSH_KEY")
if not args.no_push and not push_key:
print("[!] No Simplepush key provided (use --push-key or SIMPLEPUSH_KEY)", file=sys.stderr)
sys.exit(2)
start = time.time()
try:
proc = subprocess.Popen(cmd)
rc = proc.wait()
except KeyboardInterrupt:
rc = 130
except FileNotFoundError:
print(f"[!] Command not found: {cmd[0]}", file=sys.stderr)
sys.exit(127)
duration = int(time.time() - start)
if args.no_push or duration < args.min_seconds:
sys.exit(rc)
cmd_str = shlex.join(cmd)
if rc == 0:
title = args.title_success
event = "success"
msg = f"{cmd_str}\nDuration: {duration}s"
else:
title = args.title_failure
event = "error"
msg = f"{cmd_str}\nExit-Code: {rc}\nDuration: {duration}s"
send_simplepush(push_key, title, msg, event)
sys.exit(rc)
if __name__ == "__main__":
main()