1
|
|
|
import subprocess as sub |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
def shutdown(time=0,force=False,warning_off=False): |
5
|
|
|
# TODO: solve unix version problem |
6
|
|
|
''' |
7
|
|
|
|
8
|
|
|
:param time: int , Time in second for shutdown |
9
|
|
|
:param force: bool ,True for Force shutdown |
10
|
|
|
:param warning_off: bool , True for shutdown without any warning , this option ignore time |
11
|
|
|
:return: bool , True (Successfully) False(Unsuccessfully) |
12
|
|
|
''' |
13
|
|
|
if warning_off==True: |
14
|
|
|
command="shutdown -p " |
15
|
|
|
else: |
16
|
|
|
command="shutdown -s " |
17
|
|
|
try: |
18
|
|
|
if force==True: |
19
|
|
|
command+="-f " |
20
|
|
|
if warning_off==False: |
21
|
|
|
response=sub.Popen(command+"-t "+str(time),shell=True,stdin=sub.PIPE,stderr=sub.PIPE,stdout=sub.PIPE) |
22
|
|
|
else: |
23
|
|
|
response = sub.Popen(command ,shell=True, stdin=sub.PIPE, stderr=sub.PIPE,stdout=sub.PIPE) |
24
|
|
|
response=list(response.communicate()) |
25
|
|
|
if len(response[0])!=0 or (str(response[1]).find("1190")!=-1): |
26
|
|
|
return False |
27
|
|
|
else: |
28
|
|
|
return True |
29
|
|
View Code Duplication |
except: |
|
|
|
|
30
|
|
|
return False |
31
|
|
|
def restart(time=0,force=False): |
32
|
|
|
# TODO: solve unix version problem |
33
|
|
|
''' |
34
|
|
|
|
35
|
|
|
:param time: int , Time in second for restart |
36
|
|
|
:param force: bool ,True for Force restart |
37
|
|
|
:return: bool , True (Successfully) False(Unsuccessfully) |
38
|
|
|
''' |
39
|
|
|
command="shutdown -r " |
40
|
|
|
try: |
41
|
|
|
if force==True: |
42
|
|
|
command+="-f " |
43
|
|
|
response=sub.Popen(command+"-t "+str(time),shell=True,stdin=sub.PIPE,stdout=sub.PIPE,stderr=sub.PIPE) |
44
|
|
|
response = list(response.communicate()) |
45
|
|
|
if len(response[0])!=0 or (str(response[1]).find("1190")!=-1): |
46
|
|
|
return False |
47
|
|
|
else: |
48
|
|
View Code Duplication |
return True |
|
|
|
|
49
|
|
|
except : |
50
|
|
|
return False |
51
|
|
|
def hibernate(force=False): |
52
|
|
|
# TODO: solve unix version problem |
53
|
|
|
''' |
54
|
|
|
|
55
|
|
|
:param force: bool ,True for Force hibernate |
56
|
|
|
:return: bool , True (Successfully) False(Unsuccessfully) |
57
|
|
|
''' |
58
|
|
|
command="shutdown -h" |
59
|
|
|
try: |
60
|
|
|
if force==True: |
61
|
|
|
command+=" -f" |
62
|
|
|
response=sub.Popen(command,shell=True,stderr=sub.PIPE,stdout=sub.PIPE,stdin=sub.PIPE) |
63
|
|
|
response = list(response.communicate()) |
64
|
|
|
if len(response[0])!=0 or (str(response[1]).find("1190")!=-1): |
65
|
|
|
return False |
66
|
|
|
else: |
67
|
|
|
return True |
68
|
|
|
except: |
69
|
|
|
return False |
70
|
|
|
def logoff(force=False): |
71
|
|
|
# TODO: solve unix version problem |
72
|
|
|
''' |
73
|
|
|
|
74
|
|
|
:param force: bool ,True for Force logoff |
75
|
|
|
:return: bool , True (Successfully) False(Unsuccessfully) |
76
|
|
|
''' |
77
|
|
|
command="shutdown -l" |
78
|
|
|
try: |
79
|
|
|
if force==True: |
80
|
|
|
command+=" -f" |
81
|
|
|
response=sub.Popen(command,shell=True,stdin=sub.PIPE,stdout=sub.PIPE,stderr=sub.PIPE) |
82
|
|
|
response = list(response.communicate()) |
83
|
|
|
if len(response[0]) != 0 or (str(response[1]).find("1190") != -1): |
84
|
|
|
return False |
85
|
|
View Code Duplication |
else: |
|
|
|
|
86
|
|
|
return True |
87
|
|
|
except: |
88
|
|
|
return False |
89
|
|
|
|
90
|
|
|
def cancel(): |
91
|
|
|
# TODO: solve unix version problem |
92
|
|
|
''' |
93
|
|
|
|
94
|
|
|
:return: bool , True (Successfully) False(Unsuccessfully) |
95
|
|
|
''' |
96
|
|
|
command="shutdown -a" |
97
|
|
|
try: |
98
|
|
|
response=sub.Popen(command,shell=True,stderr=sub.PIPE,stdin=sub.PIPE,stdout=sub.PIPE) |
99
|
|
|
response = list(response.communicate()) |
100
|
|
|
if len(response[0]) != 0 or (str(response[1]).find("1116") != -1): |
101
|
|
|
return False |
102
|
|
|
else: |
103
|
|
|
return True |
104
|
|
|
except: |
105
|
|
|
return False |