-
Notifications
You must be signed in to change notification settings - Fork 1
/
clisw.py
executable file
·331 lines (258 loc) · 8.88 KB
/
clisw.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#! /usr/bin/python3
import os
import re
import time
import argparse
import subprocess
import configparser
from argparse import RawTextHelpFormatter
EC_IO_FILE = '/sys/kernel/debug/ec/ec0/io'
PC_CODE_FILE = '/etc/pc_code'
CFG_FILE = '/etc/isw.conf'
CLISW_CFG_FILE = '/etc/clisw.conf'
VERSION = '1.1'
list_cpu_temp = []
list_cpu_fan_speed = []
list_gpu_temp = []
list_gpu_fan_speed = []
#Class used for argument -s, set cpu_fan_speed_0 quickly
class RunScript(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
pc_code = read_name()
if pc_code is None:
print("An error has occurred with isw config!")
return None
read_config(pc_code)
address = list_cpu_fan_speed[0][1]
if values == 'start':
temperature = 45
else:
temperature = 0
setattr(namespace, self.dest, values)
bash = "isw -s "+str(address)+" "+str(temperature)
subprocess.Popen(bash.split(),stdout=subprocess.DEVNULL)
def main():
check_sudo() #it is necessary for read and write the operations
parser = argparse.ArgumentParser(formatter_class = RawTextHelpFormatter)
parser.add_argument("-s", action = RunScript, choices = ['start','stop'],
help = 'Shortcut for set cpu_fan_speed_0:\n'
'|start -> 45\n'
'|stop -> 0')
parser.add_argument("-v", action='store_const', const=1, help = 'clisw version')
args = parser.parse_args()
if args.v is not None:
print("Clisw version: "+str(VERSION))
return None
elif args.s is not None:
return None
os.system('clear')
print("Welcome to clisw\n")
pc_code = read_name()
if pc_code is None:
print("An error has occurred with isw config!")
return None
print("This is your pc code: "+pc_code)
read_config(pc_code)
parameter = ["0) Exit","1) Cpu","2) Gpu"]
finish = False
while not finish:
print("Choose which parameter you want change:")
for par in parameter:
print(par)
try:
try:
value = int(input("\nParameter: "))
except ValueError:
value = -1
if value >= 0 and value<len(parameter):
if value == 0:
finish = True
elif value == 1:
setting(list_cpu_fan_speed,list_cpu_temp,"cpu")
elif value == 2:
setting(list_gpu_fan_speed,list_gpu_temp,"gpu")
else:
print("Wrong input. Try again!\n")
except KeyboardInterrupt:
print("\n")
finish = True
write_config(pc_code)
# check if the program has root privileges
def check_sudo():
if os.geteuid() != 0:
exit("[!] The program was not executed as root.\n[!] Please try again, this time using 'sudo'.")
# read the code of pc (i.e. for MSI PS63 8SC is 16S2EMS1)
def read_name():
pc_code = read_name_file()
if pc_code is None: #if there isn't file yet
pc_code = read_name_ec()
if pc_code is not None:
with open(PC_CODE_FILE,"w") as temp:
temp.write(pc_code)
return pc_code
# read the pc code from pc_code file
def read_name_file():
pc_code = None
try:
with open(PC_CODE_FILE,"r") as temp:
pc_code=temp.read()
except:
pass
return pc_code
# read the code from EC file
def read_name_ec():
ec = os.popen('od -A x -t x1z '+str(EC_IO_FILE)).read() #read ec
if(ec == ''):
return None
ec_split = re.split("0000a0",ec) #split ec for read pc code
ec_split1 = re.split("[><]", ec_split[1])
pc_code =ec_split1[1].split(".")[0]
return pc_code
def read_config(pc_code):
file = CFG_FILE
if(os.path.exists(CLISW_CFG_FILE)):
file = CLISW_CFG_FILE
with open(file) as cfgfile:
cfgp = configparser.ConfigParser()
cfgp.read_file(cfgfile)
address_profile = cfgp.get(pc_code,"address_profile")
for i in range(0,7):
if i<6:
cpu_temp = cfgp.get(pc_code,"cpu_temp_"+str(i))
cpu_temp_add = cfgp.get(address_profile,"cpu_temp_address_"+str(i))
list_cpu_temp.append([cpu_temp,cpu_temp_add])
gpu_temp = cfgp.get(pc_code,"gpu_temp_"+str(i))
gpu_temp_add = cfgp.get(address_profile,"gpu_temp_address_"+str(i))
list_gpu_temp.append([gpu_temp,gpu_temp_add])
cpu_value = cfgp.get(pc_code,"cpu_fan_speed_"+str(i))
cpu_address = cfgp.get(address_profile,"cpu_fan_speed_address_"+str(i))
list_cpu_fan_speed.append([cpu_value,cpu_address])
gpu_value = cfgp.get(pc_code,"gpu_fan_speed_"+str(i))
gpu_address = cfgp.get(address_profile,"gpu_fan_speed_address_"+str(i))
list_gpu_fan_speed.append([gpu_value,gpu_address])
def setting(fan_speed_list,temp_list,component):
parameter = ["0) Fan speed","1) Temperature","2) Go back"]
finish = False
while not finish:
print("\nChoose which "+component+" parameter you want chage:")
for i in parameter:
print(i)
try:
value = int(input("\nParameter: "))
except ValueError:
value = -1
if value >= 0 and value<len(parameter):
if value == 0:
finish = fan_speed_settings(fan_speed_list,component)
elif value == 1:
finish = temp_setting(temp_list,component)
else:
finish = True
print('')
else:
print("Wrong input. Try again!")
# Section for regulate the speed of the fans
def fan_speed_settings(fan_speed_list,component):
len_list = len(fan_speed_list)
i = 0
for value in fan_speed_list:
print(str(i)+") "+component+" fan speed "+str(i)+": "+str(value[0])+"\t "+str(value[1])) #print option
i+=1
print(str(len_list)+") Go back")
ok = False
go_back = False
while not ok:
try:
index_value = int(input("\nInsert index of the value that you want edit: "))
except ValueError:
index_value = -1
if index_value == len_list:
ok = True
go_back = True
print('')
elif index_value>-1 and index_value<len_list:
while not ok:
try:
new_value = int(input("Insert value: "))
except ValueError:
new_value = -1
if new_value>=0 and new_value<=100:
ok = True
else:
print("\nWrong input. Insert a value between 0 and 100.\nTry again!\n")
else:
print("\nWrong input. Insert a index between 0 and "+str(len_list)+"\nTry again!")
if not go_back:
address = fan_speed_list[index_value][1]
fan_speed_list[index_value][0] = str(new_value)
# print("hai scelto "+str(address)+" col valore "+str(new_value)+"\n")
bash = "isw -s "+str(address)+" "+str(new_value)
subprocess.Popen(bash.split(),stdout=subprocess.DEVNULL)
return True
else:
return False
#Section for regulate the temperature
def temp_setting(temp_list,component):
len_list = len(temp_list)
i = 0
for value in temp_list:
print(str(i)+") "+component+" temp "+str(i)+": "+str(value[0])+"\t "+str(value[1])) #print option
i+=1
print(str(len_list)+") Go back")
ok = False
go_back = False
while not ok:
try:
index_value = int(input("\nInsert index of the value that you want edit: "))
except ValueError:
index_value = -1
if index_value == len_list:
ok = True
go_back = True
print('')
elif index_value>-1 and index_value<len_list:
while not ok:
try:
new_value = int(input("Insert value: "))
except ValueError:
new_value = -1
if new_value>=0 and new_value<=100:
ok = True
else:
print("\nWrong input. Insert a value between 0 and 100.\nTry again!\n")
else:
print("\nWrong input. Insert a index between 0 and "+str(len_list)+"\nTry again!")
if not go_back:
address = temp_list[index_value][1]
temp_list[index_value][0] = str(new_value)
# print("hai scelto "+str(address)+" col valore "+str(new_value)+"\n")
bash = "isw -s "+str(address)+" "+str(new_value)
subprocess.Popen(bash.split(),stdout=subprocess.DEVNULL)
return True
else:
return False
#Save the configuration
def write_config(pc_code):
address_profile = 'address_profile'
mad = 'MSI_ADDRESS_DEFAULT'
cfgp = configparser.ConfigParser()
cfgp[pc_code] = {}
cfgp[mad] = {}
cfgp[pc_code][address_profile] = mad
create_configparser(cfgp,pc_code,'cpu_temp_',list_cpu_temp,0)
create_configparser(cfgp,pc_code,'cpu_fan_speed_',list_cpu_fan_speed,0)
create_configparser(cfgp,pc_code,'gpu_temp_',list_gpu_temp,0)
create_configparser(cfgp,pc_code,'gpu_fan_speed_',list_gpu_fan_speed,0)
create_configparser(cfgp,mad,'cpu_temp_address_',list_cpu_temp,1)
create_configparser(cfgp,mad,'cpu_fan_speed_address_',list_cpu_fan_speed,1)
create_configparser(cfgp,mad,'gpu_temp_address_',list_gpu_temp,1)
create_configparser(cfgp,mad,'gpu_fan_speed_address_',list_gpu_fan_speed,1)
with open(CLISW_CFG_FILE,'w') as test:
cfgp.write(test)
def create_configparser(parser,pc_code,strn,list,pos):
i=0
for value in list:
parser[pc_code][strn+str(i)] = value[pos]
i=i+1
if __name__ == '__main__':
main()