-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (76 loc) · 3.23 KB
/
main.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
#!/usr/bin/env python3
import argparse
import time
import platform
import subprocess
import sys
class NOSLEEP:
def __init__(self):
self.system = platform.system()
self.process = None
def prevent_sleep(self, min_req):
try:
if self.system == "Linux":
self.process = subprocess.Popen(["systemd-inhibit", "--what=sleep", "--why=Running important task", "--who=NOSLEEP", "sleep", str(min_req * 60)])
elif self.system == "Darwin": # apple being apple smh
self.process = subprocess.Popen(["caffeinate", "-i"])
elif self.system == "Windows": # 💀
command = r"""powershell Start-Process powershell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command "
$powerShell = Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Prevention {
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);
public enum ExecutionState : uint {
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
}
}
"@ -PassThru;
while ($true) {
[Prevention]::SetThreadExecutionState([Prevention+ExecutionState]::ES_CONTINUOUS -bor [Prevention+ExecutionState]::ES_SYSTEM_REQUIRED -bor [Prevention+ExecutionState]::ES_DISPLAY_REQUIRED);
Start-Sleep -Seconds 30;
}' -WindowStyle Hidden"""
self.process = subprocess.Popen(command, shell=True)
print(f"won't sleep for {min_req} minutes")
print("ctrl+c to exit early")
# time.sleep(min_req * 60) # not a pleasant KeyboardInterrupt hence the following
finish = time.time() + (min_req * 60)
try:
while time.time() < finish:
time.sleep(1)
except KeyboardInterrupt:
self.cleanup()
print("\nmake sure its done else rerun")
sys.exit(0)
except Exception as e:
self.cleanup()
print("\nmake sure its done else rerun")
sys.exit(0)
finally:
self.cleanup()
sys.exit(0)
def cleanup(self): # sorry for the nest
try:
if self.process:
try:
self.process.terminate()
self.process.wait(timeout=1)
except:
try:
self.process.kill()
except:
pass
print("sleep prevention deactivated")
except:
pass
def main():
parser = argparse.ArgumentParser()
parser.add_argument("minutes", type = int, help = "minutes to prevent sleep")
args = parser.parse_args()
nether_bed = NOSLEEP()
nether_bed.prevent_sleep(args.minutes)
if __name__ == "__main__":
main()