forked from barosl/hdd-no-sleep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdd-no-sleep.py
executable file
·55 lines (45 loc) · 1.37 KB
/
hdd-no-sleep.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
#!/usr/bin/env python3
import ctypes
import sys
import random
import os
import atexit
import time
import string
import traceback
def on_exit():
input('* Press enter to continue: ')
def get_drives():
drive_bits = ctypes.windll.kernel32.GetLogicalDrives()
drives = []
for name in string.ascii_uppercase:
if drive_bits & 1:
drives.append(name + ':')
drive_bits >>= 1
return drives
def main():
if sys.platform != 'win32':
os.execvp('py.exe', ['py.exe', sys.argv[0]])
if not ctypes.windll.shell32.IsUserAnAdmin():
ret = ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, f'"{sys.argv[0]}"', None, 1)
if ret <= 32:
print(f'* ShellExecuteW error: {ret}')
return
return
atexit.register(on_exit)
print('* Reading random data to prevent hard disks from sleeping...')
while True:
for drive in get_drives():
try:
with open(rf'\\.\{drive}', 'rb') as fp:
fp.seek(512 * random.randrange(1024 * 1024))
fp.read(1)
except:
print('* Error while reading random data:', file=sys.stderr)
traceback.print_exc()
time.sleep(30 - 1)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
atexit.unregister(on_exit)