-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyHDDKeepAlive.py
136 lines (124 loc) · 4.08 KB
/
PyHDDKeepAlive.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
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
129
130
131
132
133
134
135
136
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
PyHDDKeepAlive v0.1 by Manmeet Gill
© Manmeet Gill 2016
https://manmeetgill.com
contact@manmeetgill.com
https://github.com/tf2manu994/PyHDDKeepAlive
"""
# Used for getting command line arguments
import atexit
import os
import sys
import time
__author__ = "Manmeet Gill"
__contact__ = "contact@manmeetgill.com"
__website__ = "https://manmeetgill.com"
__script__ = "PyHDDKeepAlive"
__version__ = "v0.1"
__git__ = "https://github.com/tf2manu994/PyHDDKeepAlive"
# Initiate rest of variables depending on OS
if os.name == "nt":
WINDOWS = True
ROOT_DIR = "C:\\" # Used in help flag, as well as in default preferences
ROOT_NAME = "C drive" # Used in help flag
DIR_SEPARATOR = "\\"
EXAMPLE = "D:\\"
else:
WINDOWS = False
ROOT_DIR = "/"
ROOT_NAME = "root directory"
DIR_SEPARATOR = "/"
EXAMPLE = "/mnt/disk1"
# Initiate variables
SLEEP = 55
VALID_ARGS = False
FILE_NAME = ".PyHDD"
PATH = ROOT_DIR
# Get command line arguments
for argument in sys.argv[1:]:
# Remove flag markers, we don't need to process those.
argument = argument.replace("-", "")
argument = argument.replace("/", "")
# Get the first 5 characters of argument and check if they're "path"
if argument[:5] == "sleep":
try:
# Ignore the 6th one because it's an equals, we don"t need that
SLEEP = int(argument[6:])
# Tell the loop that there were valid arguments, stop the edge case
VALID_ARGS = True
except ValueError:
print('That\'s not a valid amount of time.')
print('Make sure you wrote sleep=NUMBER and not sleep NUMBER')
sys.exit()
# Get the first 4 characters
elif argument[:4] == "path":
# Ignore the 4th one, its an equals and we don't care
PATH = argument[5:]
# Tell the loop that there were valid arguments, stop the edge case
VALID_ARGS = True
elif argument == "help":
print(__script__)
print("The command line arguments you can use are:")
print("path=<Insert path to write to here>. Default is %s" % ROOT_DIR)
print("sleep=<Seconds To Sleep> Default is 55")
print("For example, you might do path=%s sleep=30" % EXAMPLE)
print(argument)
sys.exit()
elif argument == "contact" or "copyright" or "author" or "version":
print(__author__)
print(__contact__)
print(__website__)
print(__script__, __version__)
print(__git__)
print("For help, run with the argument \"help\"")
sys.exit()
else:
print("No valid arguments were given.")
print("If you would like help, run with the argument \"help\"")
sys.exit()
# If the path doesn't end with a directory separator, add one.
if not PATH.endswith(DIR_SEPARATOR):
PATH += DIR_SEPARATOR
# Windows doesn't let us deal with the root of C, so we fiddle with temp
if PATH == "C:\\":
PATH = "C:\\temp\\"
# Set the place to write the file.
FILE = PATH + FILE_NAME
@atexit.register
def cleanup():
"""
:rtype: int
"""
print("Stopping...")
if os.path.isfile(FILE):
print("Deleting %s" % FILE)
os.remove(FILE)
sys.exit()
else:
print("Done!")
sys.exit()
if os.path.isfile(FILE):
print("The file already exists.")
print("To make sure I don\"t delete something important, I have stopped.")
print("Please remove the file ", FILE)
else:
print("Running at ", PATH, "every", SLEEP, "seconds!")
# Sets what to write
INFO = ("%s %s by %s <%s>"
% (__script__, __version__, __author__, __contact__))
while True:
try:
# The w means that we can write to the file.
WRITER = open(FILE, "w")
WRITER.write(INFO)
WRITER.close()
os.remove(FILE)
time.sleep(SLEEP)
except PermissionError:
print("Oops, we don't have access to that directory.")
print("Try another directory on the same drive.")
sys.exit()
except KeyboardInterrupt:
cleanup()