-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysdroid.py
More file actions
329 lines (252 loc) · 9.01 KB
/
sysdroid.py
File metadata and controls
329 lines (252 loc) · 9.01 KB
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
#!/data/data/com.termux/files/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import psutil
import signal
import os
from configparser import ConfigParser
from colorama import Fore, Back, Style
import subprocess
config = os.path.isfile(os.path.expanduser("~") + "/.config/sysdroid/sysdroid.cfg")
__ = os.system("clear")
print(Style.BRIGHT)
if os.geteuid() != 0: #Checking whether root user
exit("App doesn't seem to be running as root \n Exiting.")
if config == False: #Creates config file if missing
print("Config file not found. Generating one with default Values")
os.system("mkdir -p ~/.config/sysdroid/")
config = open(os.path.expanduser("~") + "/.config/sysdroid/sysdroid.cfg", "w+")
config.write("[MAIN]\n\n")
config.write ("CPU Name = Unspecified CPU\n")
config.write ("GPU Name = Unspecified GPU\n")
config.write ("Automatically get CPU and GPU name = 1\n\n")
config.write ("CPU Temperature Probe = thermal_zone9\n")
config.write ("GPU Temperature Probe = thermal_zone12\n\n")
config.write ("Progress Bar Length = 40\n")
config.write ("Update Interval = 2\n")
config.write ("Splash Screen = 1\n\n")
config.close()
if True: #Loads Config File
configfile = ConfigParser()
configfile.read(os.path.expanduser("~") + "/.config/sysdroid/sysdroid.cfg")
config = configfile["MAIN"]
getcgpuname = int(config["Automatically get CPU and GPU name"])
if getcgpuname != 1:
cpu_name = str(config["CPU Name"])
gpu_name = str(config["GPU Name"])
cpu_probe = str(config["CPU Temperature Probe"])
gpu_probe = str(config["GPU Temperature Probe"])
pslength = int(config["Progress Bar Length"])
interval = int(config["Update Interval"])
splash = int(config["Splash Screen"])
#Checks whether loaded probe files exist.
if os.path.isfile(f"/sys/class/thermal/{cpu_probe}/temp") == False:
print( Fore.RED + "Thermal Probe for CPU not found",\
Fore.GREEN + f" (/sys/class/thermal/{cpu_probe}/temp)")
print(Fore.RED + "Check the sysdroid.cfg file.",\
Style.RESET_ALL + "Program Exiting.")
exit(1)
if os.path.isfile(f"/sys/class/thermal/{gpu_probe}/temp") == False:
print( Fore.RED + "Thermal Probe for CPU not found",\
Fore.GREEN + f" (/sys/class/thermal/{gpu_probe}/temp)")
print(Fore.RED + "Check the sysdroid.cfg file.",\
Style.RESET_ALL + "Program Exiting.")
exit()
#Initializing all required Variables
t_update = 0
y = 0
sleepintv = interval / 10
if splash == 1:
print(Fore.GREEN + "sysdroid", Fore.CYAN + " - alou S", Fore.YELLOW + " (https://github.com/alou-S) \n" + Fore.WHITE)
print("A simple Python program to display CPU, GPU and Battery info on an Android device via Termux.")
print("You can check the sysdroid.cfg file for changing the programs configuration. \n\n")
if config == False:
print(Back.RED + "Warning : Config File not found. Using default values.\n\n" + Back.RESET)
input("Press Enter to continue...")
#Opening required files
pcpu_t = open(f"/sys/class/thermal/{cpu_probe}/temp", "r")
pbat_u = open("/sys/class/power_supply/battery/capacity", "r")
pbat_v = open("/sys/class/power_supply/battery/voltage_now", "r")
pbat_mAH = open("/sys/class/power_supply/battery/charge_counter", "r")
pbat_t = open("/sys/class/power_supply/battery/temp", "r")
pbat_mA = open("/sys/class/power_supply/battery/current_now", "r")
pbat_s = open("/sys/class/power_supply/battery/status", "r")
pgpu_u = open("/sys/kernel/gpu/gpu_busy", "r")
pgpu_t = open(f"/sys/class/thermal/{gpu_probe}/temp", "r")
pgpu_c = open("/sys/kernel/gpu/gpu_clock", "r")
try:
psc_v = open("/sys/class/power_supply/sc8551-standalone/sc_bus_voltage", "r")
psc_t = open("/sys/class/power_supply/sc8551-standalone/sc_die_temperature", "r")
psc_mA = open("/sys/class/power_supply/sc8551-standalone/sc_bus_current", "r")
fastcharge = 1
except:
fastcharge = 0
cc = len(psutil.cpu_percent(percpu=True)) #This gets the number of CPU Cores
temp = 0
while temp != cc: #Simple Loop to detect all CPU cores in sysfs
globals()[f"pcpu_c{temp}"] = \
open(f"/sys/devices/system/cpu/cpu{temp}/cpufreq/cpuinfo_cur_freq", "r")
temp += 1
if getcgpuname == 1:
temp = open("/proc/cpuinfo", "r")
#Yes there are better ways to filter out the unneeded stuff, But I was really lazy
cpu_name = temp.readlines()[-1].replace("Technologies", "").\
replace("Hardware", "").replace(":", "").replace(",", "").\
replace("Inc", "").replace("\t ", "").replace("\t", "").\
replace("\n", "").replace(" ", "")
temp.close()
gpu_name = str(subprocess.check_output("dumpsys SurfaceFlinger | grep GLES",\
shell=True).decode("utf-8")).replace("GLES: ", "").replace(",", "").\
replace(" ", " ").replace("(TM)", "").split("OpenGL", 1)[0]
def KBInterruptHandler(signal, frame): #Function that is called on Interrupt Signal
print(f"KeyboardInterrupt (ID: {signal}) has been caught. Closing...")
exit(0)
def mkpstring(rawval): #Function that generates the Percentage Progress Bar.
val = int(rawval/100 * pslength)
if rawval>0 and rawval<(100/pslength):
val=1
pstring=Fore.YELLOW + "[" + Fore.BLUE
pstring+= "|" * val
pstring+= " " * (pslength - val)
pstring += Fore.YELLOW + "] " + Fore.CYAN + \
str(rawval) + "%" + Fore.WHITE
return pstring
def printcgpu():
global superchar
if(cpu_t < 35):
tempcolor = Fore.CYAN
elif(cpu_t < 50):
tempcolor = Fore.GREEN
elif(cpu_t < 60):
tempcolor = Fore.YELLOW
elif(cpu_t > 59):
tempcolor = Fore.RED
superchar += Fore.GREEN + cpu_name
superchar += Fore.CYAN + f" {psutil.cpu_percent()}% "
superchar += tempcolor + str(cpu_t) + "°C \n"
temp = 0
while temp != cc:
superchar += Fore.GREEN + "CPU"
superchar += str(temp + 1)
superchar += Fore.YELLOW + " @ "
superchar += Fore.CYAN + str(globals()[f"cpu_c{temp}"]) + "MHz "
superchar += mkpstring(usage[temp]) + "\n"
temp += 1
superchar += ("\n\n")
if(gpu_t < 35):
tempcolor = Fore.CYAN
elif(gpu_t < 50):
tempcolor = Fore.GREEN
elif(gpu_t < 60):
tempcolor = Fore.YELLOW
elif(gpu_t > 59):
tempcolor = Fore.RED
superchar += Fore.GREEN + gpu_name + tempcolor + str(gpu_t) + "°C \n"
superchar += Fore.CYAN + str(gpu_c) + "MHz " + mkpstring(gpu_u)
def printmem():
global superchar
superchar += Fore.GREEN + "Memory " + mkpstring(psutil.virtual_memory()[2])
superchar += "\n"
superchar += Fore.GREEN + "Swap " + mkpstring(psutil.swap_memory()[3])
def printbat():
global superchar
superchar += Fore.GREEN + "Battery " + mkpstring(bat_u) + "\n"
superchar += Fore.CYAN + str(int(bat_v / 1000)) + " mV \n"
if(bat_t < 27):
tempcolor = Fore.CYAN
elif(bat_t < 35):
tempcolor = Fore.GREEN
elif(bat_t < 45):
tempcolor = Fore.YELLOW
elif(bat_t > 44):
tempcolor = Fore.RED
superchar += Fore.CYAN + str(int(bat_mA / 1000)) + plusplus + " mA \n"
superchar += str(int(bat_mA * bat_v / 1000000000)) + plusplus + " mW \n"
superchar += str(bat_mAH) + " mAH \n"
superchar += tempcolor + str(bat_t) + "°C \n\n"
if fastcharge == 1 and "++" in plusplus:
superchar += Fore.GREEN + "Fast Charge\n"
superchar += Fore.CYAN + str(sc_v) + " mV \n"
superchar += Fore.CYAN + str(sc_mA) + " mA \n"
superchar += Fore.CYAN + str(int(sc_v * sc_mA / 1000)) + " mW \n"
if(sc_t < 32):
tempcolor = Fore.CYAN
elif(sc_t < 40):
tempcolor = Fore.GREEN
elif(sc_t < 51):
tempcolor = Fore.YELLOW
elif(sc_t > 50):
tempcolor = Fore.RED
superchar += tempcolor + str(sc_t) + "°C \n"
def reloadall(): #Syncs all file pointer values to variables each cycle
global cpu_t
global bat_u
global bat_v
global bat_mAH
global bat_t
global bat_mA
global sc_mA
global sc_t
global sc_v
global gpu_u
global gpu_t
global gpu_c
global plusplus #This is basically used as a Charging Indicator
global usage
temp = 0
while temp != cc: #Simple loop to update all CPU frequency values
globals()[f"cpu_c{temp}"] = int( int(globals()[f"pcpu_c{temp}"]\
.read()) / 1000 )
temp += 1
cpu_t = int(pcpu_t.read()) / 1000
usage = psutil.cpu_percent(percpu=True) #Updates CPU usage value
#Battery Values Update
if pbat_s.read() == "Charging\n": #This checks whether battery is charging and changes plusplus value
plusplus = Fore.YELLOW + "++" + Fore.CYAN
else:
plusplus = ""
bat_u = int(pbat_u.read())
bat_v = int(pbat_v.read())
bat_mAH = int(pbat_mAH.read())
bat_t = int(pbat_t.read()) / 10
bat_mA = abs(int(pbat_mA.read()))
sc_mA = int(psc_mA.read())
sc_v = int(psc_v.read())
sc_t = int(psc_t.read())
#GPU Values Update
gpu_u = int( pgpu_u.read().replace("%", "") )
gpu_t = int(pgpu_t.read()) / 1000
gpu_c = int(pgpu_c.read())
temp = 0
while temp != cc:
globals()[f"pcpu_c{temp}"].seek(0)
temp += 1
#Seek all file pointers
pcpu_t.seek(0)
pbat_mAH.seek(0)
pbat_v.seek(0)
pbat_u.seek(0)
pbat_t.seek(0)
pbat_mA.seek(0)
pbat_s.seek(0)
psc_mA.seek(0)
psc_v.seek(0)
psc_t.seek(0)
pgpu_t.seek(0)
pgpu_u.seek(0)
pgpu_c.seek(0)
while True:
t = int( (time.time() * 100) / (interval * 100) )
if t != t_update:
t_update = t
reloadall()
superchar = ""
printcgpu()
superchar += ("\n\n")
printmem()
superchar += ("\n\n")
printbat()
_ = os.system("clear")
print(superchar)
signal.signal(signal.SIGINT, KBInterruptHandler)
time.sleep(sleepintv)