-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathorangetool_system.py
217 lines (181 loc) · 5.3 KB
/
orangetool_system.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding: utf-8 -*-
"""Orangetool system functions."""
import subprocess as sub
from .orangetool_params import ORANGETOOL_VERSION, UPDATE_URL, GENERAL_ERROR_MESSAGE, ROOT_ERROR_MESSAGE
from .orangetool_utils import time_convert
import time
import requests
from art import tprint
def check_update(debug=False):
"""
Check orangetool site for new version.
:param debug: flag for using debug mode
:type debug:bool
:return: True if new version is available
"""
try:
new_version = requests.get(UPDATE_URL).text
if float(new_version) > float(ORANGETOOL_VERSION):
print("New Version (" + new_version + ") Of Orangetool Is Available")
return True
print("Update!")
return False
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def get_temp(zone=0, debug=False):
"""
Read cpu temperature.
:param zone : thermal zone index
:type zone:int
:param debug : flag for using debug mode
:type debug:bool
:return: board temp as string in celsius
"""
try:
command = open("/sys/class/thermal/thermal_zone" + str(zone) + "/temp")
# command=sub.Popen(["cat","/sys/class/thermal/thermal_zone"+str(zone)+"/temp"],stderr=sub.PIPE,stdin=sub.PIPE,stdout=sub.PIPE)
# response=list(command.communicate())
response = command.read()
return response[:-1]
# if len(response[0])!=0:
# return str(response[0])[2:-3]
# else:
# return GENERAL_ERROR_MESSAGE
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def time_control(mode="uptime", debug=False):
"""
Return system time.
:param mode: time mode (uptime / idle)
:type mode: str
:param debug: flag for using debug mode
:type debug:bool
:return: system time as string
"""
index = 0
if mode in ["idle", "idletime"]:
index = 1
try:
command = open("/proc/uptime")
response = command.read()
return time_convert(response[:-1].split(" ")[index])
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def uptime(debug=False):
"""
Return system uptime.
:param debug: flag for using debug mode
:type debug:bool
:return: system uptime as string
"""
return time_control(mode="uptime", debug=debug)
def idletime(debug=False):
"""
Return system idletime.
:param debug: flag for using debug mode
:type debug:bool
:return: system idle as string
"""
return time_control(mode="idle", debug=debug)
def version():
"""
Return orangetool version.
:return: return orangetool-version number as string
"""
tprint("orangetool", font="bulbhead")
tprint("v" + ORANGETOOL_VERSION, font="bulbhead")
def wakeup(day=0, hour=0, minute=0, sync=True, debug=False):
"""
Set wakeup time for kernel RTC (need sudo).
:param day: days for wakeup
:type day:int
:param hour: hour for wakeup
:type hour:int
:param minute: minute for wakeup
:type minute:int
:param sync: RTC sync flag
:type sync: bool
:param debug: flag for using debug mode
:type debug:bool
:return: bool
"""
try:
if sync:
_ = sub.Popen(
["hwclock", "-w"],
stderr=sub.PIPE,
stdout=sub.PIPE,
stdin=sub.PIPE)
total_time = day * 24 * 60 + hour * 60 + minute
epoch = time.time() + total_time * 60
file = open("/sys/class/rtc/rtc0/wakealarm", "w")
file.write("0")
file.close()
file = open("/sys/class/rtc/rtc0/wakealarm", "w")
file.write(str(epoch))
file.close()
return True
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def power_control(command, debug=False):
"""
Control different power options.
:param command: input command
:type command: str
:param debug: flag for using debug mode
:type debug: bool
:return: None
"""
try:
command = sub.Popen(
command,
stderr=sub.PIPE,
stdout=sub.PIPE,
stdin=sub.PIPE)
response = list(command.communicate())
if len(response[1]) > 0:
raise Exception(ROOT_ERROR_MESSAGE)
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def sleep(debug=False):
"""
Shortcut for sleep command (need sudo).
:param debug: flag for using debug mode
:type debug:bool
:return: None
"""
power_control("pm-suspend", debug)
def hibernate(debug=False):
"""
Shortcut for hibernate command (need sudo).
:param debug: flag for using debug mode
:type debug:bool
:return: None
"""
power_control("pm-hibernate", debug)
def halt(debug=False):
"""
Shortcut for poweroff (need sudo).
:param debug: flag for using debug mode
:type debug:bool
:return: None
"""
power_control("poweroff", debug)
def restart(debug=False):
"""
Shortcut for reboot (need sudo).
:param debug: flag for using debug mode
:type debug:bool
:return: None
"""
power_control("reboot", debug)