-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu-fan-meister.py
executable file
·292 lines (260 loc) · 10.8 KB
/
gpu-fan-meister.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
#!/usr/bin/env python3
##############################################################################
# gpu-fan-meister.py - main application launcher #
# #
# Copyright (c) 2018 Alexey Parfenov <zxed@alkatrazstudio.net> #
# #
# This file is part of GPU Fan Meister. #
# #
# GPU Fan Meister is free software: you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, #
# or (at your option) any later version. #
# #
# GPU Fan Meister is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details: https://gnu.org/licenses/gpl.html #
##############################################################################
import os
import re
import subprocess
import sys
import multiprocessing.pool
import argparse
import importlib
THISFILE = os.path.realpath(__file__)
THISDIR = os.path.dirname(THISFILE)
MSG_TITLE = "GPU Fan Meister"
MAIN_EXE_NAME = os.path.splitext(os.path.basename(THISFILE))[0]
ENV_VAR_PREFIX = MAIN_EXE_NAME.replace("-", "").upper()
ERR_OK = 0
ERR_MAIN = 10
ERR_PLUGIN_INIT = 11
ERR_PLUGIN_DEINIT = 12
ERR_KEYBOARD_INTERRUPT = 130
sys.path.append(os.path.realpath(THISDIR + "/pynotify2")) # use system packages if available
notify2 = importlib.import_module("notify2")
def show_msg(s):
if not hasattr(show_msg, "inited"):
notify2.init(MAIN_EXE_NAME)
show_msg.inited = True
n = notify2.Notification(MSG_TITLE, s)
n.set_timeout(notify2.EXPIRES_NEVER)
n.set_urgency(notify2.URGENCY_CRITICAL)
n.show()
def run_exe(args, pass_through=False, suppress_msg=False):
try:
res = subprocess.run(
args,
stdout=subprocess.PIPE if not pass_through else None,
stderr=subprocess.PIPE if not pass_through else None,
check=True,
universal_newlines=True)
return res.stdout
except subprocess.CalledProcessError as e:
if not suppress_msg:
show_msg("{} exited with code {}: {}".format(" ".join(args), e.returncode, e.stderr))
raise RuntimeError(e.stderr)
def main(args):
#
# setup the environment
#
os.chdir(THISDIR)
if "QT_AUTO_SCREEN_SCALE_FACTOR" not in os.environ and "QT_SCREEN_SCALE_FACTORS" not in os.environ:
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
main_exe = os.path.join(THISDIR, MAIN_EXE_NAME)
exit_code = ERR_OK
#
# pass all unknown args straight to the main app
#
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--plugins", nargs="?", const="", default="")
known_args, unknown_args = parser.parse_known_args(args)
if len(unknown_args):
try:
# assuming that the app has been started via command line
# so do not show notifications
stdout = run_exe([main_exe] + args, suppress_msg=True).strip()
if "APPIMAGE" in os.environ and "ARGV0" in os.environ:
argv0 = os.environ["ARGV0"]
else:
argv0 = sys.argv[0]
print(stdout.replace(main_exe, argv0))
except RuntimeError as e:
print(str(e))
exit_code = ERR_MAIN
return exit_code
#
# get enabled plugins
#
if known_args.plugins:
plugins_via_cmdline = True
plugin_names = map(str.strip, known_args.plugins.split(","))
plugin_names = list(set(plugin_names)) # get unique values
stdout = run_exe([main_exe, "--available-plugins"]).strip()
lines = map(lambda line: line.strip().split(":", 1), stdout.split("\n")) if stdout else []
available_plugins = {name: dir_name for name, dir_name in lines}
plugin_items = {}
try:
for plugin_name in plugin_names:
if plugin_name in available_plugins:
plugin_items[plugin_name] = available_plugins[plugin_name]
else:
raise RuntimeError("Unavailable plugin: {}".format(plugin_name))
except RuntimeError:
return ERR_PLUGIN_INIT
else:
try:
plugins_via_cmdline = False
stdout = run_exe([main_exe, "--enabled-plugins"]).strip()
lines = list(map(lambda line: line.strip().split(":", 1), stdout.split("\n")) if stdout else [])
plugin_items = {name: dir_name for name, dir_name in lines}
plugin_names = [name for name, _ in lines]
except RuntimeError:
return ERR_MAIN
#
# validate enabled plugins
#
plugins = []
cats = {}
added_plugins = []
for plugin_name in plugin_names:
try:
if plugin_name in added_plugins:
raise RuntimeError("plugin is specified more than once")
added_plugins.append(plugin_name)
plugin = {
"name": plugin_name
}
plugin["dir"] = plugin_items[plugin["name"]]
if not os.path.isdir(plugin["dir"]):
raise RuntimeError("plugin directory not found")
# check for conflicting plugins
parts = plugin["name"].split("-", 1)
if len(parts) != 2:
raise RuntimeError("plugin category not set")
cat = parts[0]
if cat in cats:
raise RuntimeError(
"another plugin from the same category: {}"
.format(cats[cat]))
cats[cat] = plugin["name"]
# search for init and deinit executables
modes = ["init", "deinit"]
for plugin_dir_item in os.scandir(plugin["dir"]):
if not plugin_dir_item.is_file():
continue
basename, ext = os.path.splitext(plugin_dir_item.name)
for mode in modes:
if basename.startswith(plugin["name"] + "-" + mode):
if mode in plugin:
raise RuntimeError(
"more than one {} entry: {} and {}"
.format(mode, plugin[mode+"_name"], plugin_dir_item.name))
plugin[mode] = plugin_dir_item.path
plugin[mode+"_name"] = plugin_dir_item.name
if not os.access(plugin[mode], os.R_OK):
raise RuntimeError("{} entry is not readable: {}".format(mode, plugin[mode]))
if not os.access(plugin[mode], os.X_OK):
raise RuntimeError("{} entry is not executable: {}".format(mode, plugin[mode]))
for mode in modes:
if mode not in plugin:
raise RuntimeError("{} executable not found".format(mode))
plugins.append(plugin)
except (OSError, RuntimeError) as e:
show_msg("Plugin \"{}\": {}".format(plugin_name, e))
exit_code = ERR_PLUGIN_INIT
plugins = sorted(plugins, key=lambda plugin : plugin["name"])
if not plugins:
if plugins_via_cmdline:
raise RuntimeError("no plugins loaded")
else:
try:
run_exe([main_exe, "--plugin-selector"])
exit_code = ERR_OK
except RuntimeError:
exit_code = ERR_MAIN
return exit_code
#
# set program version in environment
#
try:
stdout = run_exe([main_exe, "--version"])
except RuntimeError:
return ERR_MAIN
try:
ver_str = stdout.split(" ", 2)[1].strip()
m = re.match(r"^(\d+)\.(\d+)\.(\d+)$", ver_str)
if not m:
raise RuntimeError
ver_maj, ver_min, ver_pat = ver_str.split(".", 3)
except (IndexError, RuntimeError, ValueError):
show_msg("invalid version output: {}".format(stdout))
return ERR_MAIN
os.environ[ENV_VAR_PREFIX + "_VER_MAJ"] = ver_maj
os.environ[ENV_VAR_PREFIX + "_VER_MIN"] = ver_min
os.environ[ENV_VAR_PREFIX + "_VER_PAT"] = ver_pat
#
# run init executables
#
inited_plugins = []
for plugin in plugins:
try:
stdout = run_exe([plugin["init"]])
lines = stdout.split("\n")
lines = map(str.strip, lines)
lines = filter(lambda s: s != "" and s[0] != "#", lines)
keyvals = map(lambda s: s.split("=", 2), lines)
keyvals = filter(lambda kv: len(kv) == 2, keyvals)
keyvals = {kv[0].rstrip(): kv[1].lstrip() for kv in keyvals}
plugin["env"] = {key: val for key, val in keyvals.items() if val != ""}
inited_plugins.append(plugin)
except RuntimeError as e:
show_msg("Plugin \"{}\" init: {}".format(plugin["name"], e))
exit_code = ERR_PLUGIN_INIT
plugins = inited_plugins
#
# set environment based on plugins output
#
for plugin in plugins:
for key, val in plugin["env"].items():
if key not in os.environ:
os.environ[key] = val.strip(os.pathsep)
else:
if val[-1] == os.pathsep:
os.environ[key] = val + os.environ[key]
elif val[0] == os.pathsep:
os.environ[key] = os.environ[key] + val
else:
os.environ[key] = val
#
# run the main executable
#
if exit_code == ERR_OK:
try:
plugin_names = map(lambda p: p["name"], plugins)
run_exe([main_exe, "--plugins=" + str.join(",", plugin_names)], True)
except RuntimeError:
exit_code = ERR_MAIN
#
# run deinit executables
#
for plugin in plugins:
try:
run_exe([plugin["deinit"]])
except RuntimeError:
exit_code = ERR_PLUGIN_DEINIT
return exit_code
if __name__ == "__main__":
try:
# run in a separate thread to defer KeyboardInterrupt
try:
pool = multiprocessing.pool.ThreadPool() # chaining everything in one line
exit_code = pool.apply_async(main, args=[sys.argv[1:]]).get() # hangs the application for some reason
except KeyboardInterrupt:
exit_code = ERR_KEYBOARD_INTERRUPT
sys.exit(exit_code)
except Exception as e:
show_msg(str(e))
raise