1
|
|
|
import datetime |
2
|
|
|
import time |
3
|
|
|
import subprocess as sub |
4
|
|
|
import sys |
5
|
|
|
|
6
|
|
|
flag_dict={"-r":"Restart","-h":"Hibernate","-s":"Shutdown"} |
7
|
|
|
def cancel(): |
8
|
|
|
c_input=input("Press Ctrl+C to cancel process") |
9
|
|
|
sub.Popen("shutdown -a",shell=True) |
10
|
|
|
def get_method(): |
11
|
|
|
flag="" |
12
|
|
|
get_method=input("Please Enter Method , Shutdown[1] , Hibernate[2] , Restart[3]") |
13
|
|
|
if get_method=="2": |
14
|
|
|
flag="-h" |
15
|
|
|
elif get_method=="3": |
16
|
|
|
flag="-r" |
17
|
|
|
else: |
18
|
|
|
flag="-s" |
19
|
|
|
return flag |
20
|
|
|
def print_time(sleep_list,method): |
21
|
|
|
print("Your Computer Will Be ",flag_dict[method],"In ",str(sleep_list[0]),"Hour and",str(sleep_list[1]),"Minute") |
22
|
|
|
def duration(): |
23
|
|
|
try: |
24
|
|
|
get_hour=int(input("Please Enter Hour :")) |
25
|
|
|
except ValueError: |
26
|
|
|
get_hour=0 |
27
|
|
|
try: |
28
|
|
|
get_minute=int(input("Please Enter Minute :")) |
29
|
|
|
except ValueError: |
30
|
|
|
get_minute=0 |
31
|
|
|
sleep_time=get_hour*3600+get_minute*60 |
32
|
|
|
flag=get_method() |
33
|
|
|
print_time([get_hour,get_minute],flag) |
34
|
|
|
if flag=="-h": |
35
|
|
|
time.sleep(float(sleep_time)) |
36
|
|
|
sub.Popen("shutdown -h -f",shell=True) |
37
|
|
|
sys.exit() |
38
|
|
|
else: |
39
|
|
|
sub.Popen("shutdown "+flag+" -f -t "+str(sleep_time),shell=True) |
40
|
|
|
cancel() |
41
|
|
|
|
42
|
|
|
def instantly(): |
43
|
|
|
flag=get_method() |
44
|
|
|
inp=input('Are you sure?[y]') |
45
|
|
|
if inp.upper()=="Y": |
46
|
|
|
sub.Popen("shutdown "+flag+" -f",shell=True) |
47
|
|
|
else: |
48
|
|
|
sys.exit() |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def sleep_time_convert(get_hour,get_minute): |
52
|
|
|
date_time=datetime.datetime.now() |
53
|
|
|
now=str(date_time.time()).split(":") |
54
|
|
|
sleep_hour=get_hour-int(now[0]) |
55
|
|
|
if sleep_hour<0: |
56
|
|
|
sleep_hour=sleep_hour+24 |
57
|
|
|
sleep_minute=get_minute-int(now[1]) |
58
|
|
|
if sleep_minute<0: |
59
|
|
|
sleep_minute=sleep_minute+60 |
60
|
|
|
sleep_hour=sleep_hour-1 |
61
|
|
|
sleep_time=sleep_hour*3600+sleep_minute*60 |
62
|
|
|
return [sleep_time,sleep_hour,sleep_minute] |
63
|
|
|
|
64
|
|
|
def localtime(): |
65
|
|
|
try: |
66
|
|
|
get_hour=int(input("Please Enter Hour :")) |
67
|
|
|
except ValueError: |
68
|
|
|
get_hour=0 |
69
|
|
|
try: |
70
|
|
|
get_minute=int(input("Please Enter Minute :")) |
71
|
|
|
except ValueError: |
72
|
|
|
get_minute=0 |
73
|
|
|
sleep_time=sleep_time_convert(get_hour,get_minute) |
74
|
|
|
flag=get_method() |
75
|
|
|
print_time(sleep_time[1:],flag) |
76
|
|
|
if flag=="-h": |
77
|
|
|
time.sleep(sleep_time[0]) |
78
|
|
|
sub.Popen("shutdown -h -f",shell=True) |
79
|
|
|
else: |
80
|
|
|
sub.Popen("shutdown "+flag+" -f -t "+str(sleep_time[0]),shell=True) |
81
|
|
|
|
82
|
|
|
cancel() |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
if __name__=="__main__": |
86
|
|
|
try: |
87
|
|
|
input1=int(input("Duration[1] or LocalTime[2] or Instantly[3]")) |
88
|
|
|
except ValueError: |
89
|
|
|
input1=1 |
90
|
|
|
except KeyboardInterrupt: |
91
|
|
|
print("pyshut terminated!!") |
92
|
|
|
sys.exit() |
93
|
|
|
try: |
94
|
|
|
if input1==1: |
95
|
|
|
duration() |
96
|
|
|
elif input1==2: |
97
|
|
|
localtime() |
98
|
|
|
else: |
99
|
|
|
instantly() |
100
|
|
|
except KeyboardInterrupt: |
101
|
|
|
print("pyshut terminated!!") |
102
|
|
|
sub.Popen("shutdown -a",shell=True) |
103
|
|
|
sys.exit() |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|