-
Notifications
You must be signed in to change notification settings - Fork 0
/
act.py
executable file
·139 lines (118 loc) · 4.98 KB
/
act.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
137
138
139
#!/usr/bin/python3
import os
try:
import psutil
except ImportError:
import pip
print("Installing missing packages (this will be done the first time only) :")
if os.getuid() == 0:
pip.main(["install", "psutil"])
else:
pip.main(["install", "psutil", "--user"])
print("Done.\nRerun the command now.")
exit(0)
import argparse
from time import sleep, localtime
from distutils.spawn import find_executable
# This function will execute until the end of the copy
def copy(time, verbose):
while True:
bytes_read = psutil.disk_io_counters().read_bytes / (1024*1024)
bytes_wrote = psutil.disk_io_counters().write_bytes / (1024*1024)
sleep(time)
bytes_read = (psutil.disk_io_counters().read_bytes / (1024*1024) - bytes_read) / time
bytes_wrote = (psutil.disk_io_counters().write_bytes / (1024*1024) - bytes_wrote) / time
if verbose:
t = localtime()
print("[{:02}:{:02}:{:02}] R : {:.2f} Mb / W : {:.2f} Mb".format(
t.tm_hour, t.tm_min, t.tm_sec, bytes_read, bytes_wrote))
if bytes_read < 0.1 and bytes_wrote < 0.1:
return
# This function will execute until the end of the cpu usage.
def cpu(time, verbose):
while True:
cpu_usage = psutil.cpu_percent(time)
if verbose:
t = localtime()
print("[{:02}:{:02}:{:02}] cpu usage : {}%".format(t.tm_hour, t.tm_min, t.tm_sec, cpu_usage))
if cpu_usage < 2:
return
# This function will execute until the end of the download.
def download(time, verbose):
while True:
bytes_recv = psutil.net_io_counters().bytes_recv / 1000
sleep(time)
speed = (psutil.net_io_counters().bytes_recv/1000 - bytes_recv)/time
if verbose:
t = localtime()
print("[{:02}:{:02}:{:02}] Download speed : {:.1f} KB/sec".format(
t.tm_hour, t.tm_min, t.tm_sec, speed))
if speed < 3:
return
# This function will execute until the end of the upload.
def upload(time, verbose):
while True:
bytes_sent = psutil.net_io_counters().bytes_sent / 1000
sleep(time)
speed = (psutil.net_io_counters().bytes_sent/1000 - bytes_sent)/time
if verbose:
t = localtime()
print("[{:02}:{:02}:{:02}] Upload speed : {:.1f} KB/sec".format(t.tm_hour, t.tm_min, t.tm_sec, speed))
if speed < 3:
return
def action(action):
if find_executable('systemctl'):
if action == 'poweroff':
os.system('systemctl poweroff')
elif action == 'reboot':
os.system('systemctl reboot')
elif action == 'hibernate':
os.system('systemctl hibernate')
elif action == 'nothing':
pass
else:
if action == 'poweroff':
os.system('poweroff')
elif action == 'reboot':
os.system('reboot')
elif action == 'hibernate':
os.system('pm-hibernate')
elif action == 'nothing':
pass
def main():
args_parser = argparse.ArgumentParser(description="This program is used to perform an operation after an action.")
# Adding the available arguments with the ArgumentParser() object.
args_parser.add_argument('-o', '--operation', type=str, required=True, choices=['cpu', 'copy', 'download', 'upload'],
help="Type of operation to wait for")
args_parser.add_argument('-a', '--action', type=str, required=False, choices=['poweroff', 'reboot', 'hibernate',
'nothing'], default="poweroff", help="The action to performe after the end of the operation\
(default : poweroff)")
args_parser.add_argument('-t', '--time', type=int, default=10,
help='Interval to test if the operation is still running')
args_parser.add_argument('-v', '--verbose', action='store_true', help='Verbose')
args_parser.add_argument('--version', action='version', version='%(prog)s 0.2')
args = args_parser.parse_args()
# Find out which privilage this script needs to continue running.
if os.getuid() == 0 and find_executable('poweroff')[:5] == '/snap':
print("This script can't run using root privilages.")
print("Try to run it without sudo.")
exit(-1)
elif (os.getuid() != 0 and os.path.realpath('/sbin/poweroff') != '/bin/systemctl'
and args.action in ['poweroff', 'reboot', 'hibernate']):
print("need to be root to {}".format(args.action))
exit(-1)
# Waiting for the asked operation.
if args.operation == 'copy':
copy(args.time, args.verbose)
elif args.operation =='cpu':
cpu(args.time, args.verbose)
elif args.operation == 'download':
download(args.time, args.verbose)
elif args.operation == 'upload':
upload(args.time, args.verbose)
action(args.action)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
exit()