-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PythonChecker.ts
366 lines (324 loc) · 15.8 KB
/
PythonChecker.ts
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import path from 'path';
import fs from 'fs';
import { delay, normalizePath, runCommand } from './utils';
import PrefixLogger from './PrefixLogger';
import type LogLevelLogger from './LogLevelLogger';
import type { AxiosResponse } from 'axios';
import axios from 'axios';
import { compareVersions } from 'compare-versions';
import os from 'os';
let supportedPythonVersions: string[] = [
'3.9',
'3.10',
'3.11',
'3.12',
'3.13',
];
const MIN_OPENSSL_VERSION: string = '3.0.0';
const UID: number = os.userInfo().uid;
const GID: number = os.userInfo().gid;
class PythonChecker {
private readonly customPythonExecutable: string | undefined;
private readonly log: PrefixLogger;
private readonly pluginDirPath: string;
private pythonExecutable: string = 'python3';
private requirementsPath: string = path.join(__dirname, '..', 'python_requirements', 'default', 'requirements.txt');
private readonly venvAtvremoteExecutable: string;
private readonly venvAtvscriptExecutable: string;
private readonly venvConfigPath: string;
private readonly venvPath: string;
private readonly venvPipExecutable: string;
private readonly venvPythonExecutable: string;
public constructor(logger: LogLevelLogger | PrefixLogger, storagePath: string, customPythonExecutable?: string) {
this.log = new PrefixLogger(logger, 'Python check');
this.customPythonExecutable = customPythonExecutable;
this.pluginDirPath = path.join(storagePath, 'appletv-enhanced');
this.venvPath = path.join(this.pluginDirPath, '.venv');
this.venvPythonExecutable = path.join(this.venvPath, 'bin', 'python3');
this.venvPipExecutable = path.join(this.venvPath, 'bin', 'pip3');
this.venvConfigPath = path.join(this.venvPath, 'pyvenv.cfg');
this.venvAtvremoteExecutable = path.join(this.venvPath, 'bin', 'atvremote');
this.venvAtvscriptExecutable = path.join(this.venvPath, 'bin', 'atvscript');
}
public async allInOne(forceVenvRecreate: boolean = false): Promise<void> {
this.log.info('Starting python check.');
this.pythonExecutable = this.getPythonExecutable('python3', this.customPythonExecutable);
this.log.info(`Using "${this.pythonExecutable}" as the python executable.`);
this.ensurePluginDir();
await this.openSSL();
await this.ensurePythonVersion();
await this.ensureVenvCreated(forceVenvRecreate);
await this.ensureVenvUsesCorrectPython();
await this.ensureVenvPipUpToDate();
await this.ensureVenvRequirementsSatisfied();
this.log.success('Finished');
}
private async areRequirementsSatisfied(): Promise<boolean> {
const [freezeStdout]: [string, string, number | null] =
await runCommand(this.log, this.venvPipExecutable, ['freeze'], undefined, true);
const freeze: Record<string, string> = this.freezeStringToObject(freezeStdout);
const requirements: Record<string, string> = this.freezeStringToObject(fs.readFileSync(this.requirementsPath).toString());
for (const pkg in requirements) {
if (freeze[pkg] !== requirements[pkg]) {
return false;
}
}
return true;
}
private async createVenv(): Promise<void> {
const [stdout]: [string, string, number | null] =
await runCommand(this.log, this.pythonExecutable, ['-m', 'venv', this.venvPath, '--clear'], undefined, true);
if (stdout.includes('not created successfully') || !this.isVenvCreated()) {
while (true) {
this.log.error('virtualenv python module is not installed. If you have installed homebridge via the apt package manager, \
update the homebridge apt package to 1.1.4 or above (this applies for installations based on the Raspberry Pi OS image as well). When \
using the official docker image, update the image to version 2023-11-28 or above. Otherwise install the python virtualenv module \
manually.');
await delay(300000);
}
} else if (stdout.trim() !== '') {
this.log.warn(stdout);
}
this.log.success('Virtual python environment (re)created');
}
private ensurePluginDir(): void {
if (!fs.existsSync(this.pluginDirPath)) {
this.log.info('creating plugin dir ...');
fs.mkdirSync(this.pluginDirPath);
this.log.success('plugin dir created');
} else {
this.log.info('plugin dir exists.');
}
}
private async ensurePythonVersion(): Promise<void> {
const version: string = await this.getSystemPythonVersion();
if (supportedPythonVersions.findIndex((e) => version.startsWith(e)) === -1) {
while (true) {
this.log.error(`Python ${version} is installed. However, only Python \
${supportedPythonVersions[0]} to ${supportedPythonVersions[supportedPythonVersions.length - 1]} is supported.`);
await delay(300000);
}
} else {
this.log.info(`Python ${version} is installed and supported by the plugin.`);
}
}
private async ensureVenvCreated(forceVenvRecreate: boolean): Promise<void> {
if (forceVenvRecreate) {
this.log.warn('Forcing the python virtual environment to be recreated ...');
await this.createVenv();
} else if (this.isVenvCreated() === false) {
this.log.info('Virtual python environment is not present. Creating now ...');
await this.createVenv();
} else if (this.isVenvExecutable() === false) {
while (true) {
this.log.error(`The current user ${UID}:${GID} does not have the permissions to execute the virtual python environment. \
Make sure the user has the permissions to execute the above mentioned files. \`chmod +x ./appletv-enhanced/.venv/bin/*\` should do the \
trick. Restart the plugin after fixing the permissions.`);
await delay(300000);
}
} else {
this.log.info('Virtual environment already exists.');
}
}
private async ensureVenvPipUpToDate(): Promise<void> {
const venvPipVersion: string = await this.getVenvPipVersion();
this.log.info(`Venv pip version: ${venvPipVersion}`);
this.log.info('Checking if there is an update for venv pip ...');
if (venvPipVersion === await this.getMostRecentPipVersion()) {
this.log.info('Venv pip is up to date');
} else {
this.log.warn('Venv pip is outdated. Updating now ...');
const success: boolean = await this.updatePip();
if (success === true) {
this.log.success('Venv pip successfully updated.');
} else {
this.log.warn('Failed to update venv pip. Continuing anyhow ...');
}
}
}
private async ensureVenvRequirementsSatisfied(): Promise<void> {
if (await this.areRequirementsSatisfied()) {
this.log.info('Python requirements are satisfied.');
} else {
this.log.warn('Python requirements are not satisfied. Installing them now ...');
const success: boolean = await this.installRequirements();
if (success === true) {
this.log.success('Python requirements successfully installed.');
} else {
while (true) {
this.log.error('There was an error installing the python dependencies. Cannot proceed!');
await delay(300000);
}
}
}
if (this.isPyatvExecutable() === false) {
while (true) {
this.log.error(`The current user ${UID}:${GID} does not have the permissions to execute the PyATV scripts. \
Make sure the user has the permissions to execute the above mentioned files. \`chmod +x ./appletv-enhanced/.venv/bin/*\` should do the \
trick. Restart the plugin after fixing the permissions.`);
await delay(300000);
}
}
}
private async ensureVenvUsesCorrectPython(): Promise<void> {
const systemPythonHome: string = await this.getPythonHome(this.pythonExecutable);
this.log.debug(`System python home: ${systemPythonHome}`);
const venvPythonHome: string = await this.getPythonHome(this.venvPythonExecutable);
this.log.debug(`Venv python home: ${venvPythonHome}`);
if (venvPythonHome !== systemPythonHome) {
this.log.warn('The virtual environment does not use the configured python environment. Recreating virtual environment ...');
await this.createVenv();
return;
}
const systemPythonVersion: string = await this.getSystemPythonVersion();
this.log.debug(`System python version: ${systemPythonVersion}`);
const venvPythonVersion: string = this.getVenvPythonVersion();
this.log.debug(`Venv python version: ${venvPythonVersion}`);
if (systemPythonVersion !== venvPythonVersion) {
this.log.warn('The virtual environment does not use the configured python environment. Recreating virtual environment ...');
await this.createVenv();
return;
}
this.log.info('Virtual environment is using the configured python environment. Continuing ...');
}
private freezeStringToObject(value: string): Record<string, string> {
const lines: string[] = value.trim().split('\n');
const packages: Record<string, string> = {};
for (const line of lines) {
const [pkg, version]: string[] = line.split('==');
packages[pkg.replaceAll('_', '-')] = version;
}
return packages;
}
private async getMostRecentPipVersion(): Promise<string> {
try {
const response: AxiosResponse<{ info: { version: string } }, unknown> = await axios.get('https://pypi.org/pypi/pip/json');
return response.data.info.version;
} catch (e) {
this.log.error(e as string);
return 'error';
}
}
private getPythonExecutable(defaultsTo: string, customPythonExecutable: string | undefined): string {
if (customPythonExecutable === undefined) {
this.log.debug('Using the systems default python installation since there is no custom python installation specified.');
return defaultsTo;
}
const pythonCandidate: string | undefined = normalizePath(customPythonExecutable);
if (pythonCandidate === undefined) {
this.log.warn(`Could not normalize the python executable path ${pythonCandidate}. Falling back to the systems default python \
installation.`);
return defaultsTo;
}
if (fs.existsSync(pythonCandidate)) {
this.log.debug(`The specified python installation "${pythonCandidate}" does exist.`);
try {
fs.accessSync(pythonCandidate, fs.constants.X_OK);
this.log.debug(`The current user ${UID}:${GID} has the permissions to execute "${pythonCandidate}".`);
} catch {
this.log.warn(`The current user ${UID}:${GID} does not have the permissions to execute "${pythonCandidate}". Falling back \
to the systems default python installation.`);
return defaultsTo;
}
this.log.debug(`Using the specified python installation "${pythonCandidate}".`);
return pythonCandidate;
}
this.log.warn(`The python executable "${pythonCandidate}" set in the configuration does not exist. Falling back to the \
systems default python installation.`);
return defaultsTo;
}
private async getPythonHome(executable: string): Promise<string> {
const [venvPythonHome]: [string, string, number | null] =
await runCommand(this.log, executable, [path.join(__dirname, 'determinePythonHome.py')], undefined, true);
return venvPythonHome.trim();
}
private async getSystemPythonVersion(): Promise<string> {
const [version]: [string, string, number | null] =
await runCommand(this.log, this.pythonExecutable, ['--version'], undefined, true);
return version.trim().replace('Python ', '');
}
private async getVenvPipVersion(): Promise<string> {
const [version]: [string, string, number | null] =
await runCommand(this.log, this.venvPipExecutable, ['--version'], undefined, true);
return version.trim().replace('pip ', '').split(' ')[0];
}
private getVenvPythonVersion(): string {
const pyvenvcfgContent: string = fs.readFileSync(this.venvConfigPath, 'utf-8');
const lines: string[] = pyvenvcfgContent.split('\n');
const versionLine: string | undefined = lines.find((e) => e.startsWith('version'));
if (versionLine === undefined) {
return '?';
}
return versionLine.replace('version', '').replace('=', '').trim();
}
private async installRequirements(): Promise<boolean> {
return (await runCommand(this.log, this.venvPipExecutable, ['install', '-r', this.requirementsPath])).at(2) === 0;
}
private isPyatvExecutable(): boolean {
let success: boolean = true;
for (const f of [
this.venvAtvremoteExecutable,
this.venvAtvscriptExecutable,
]) {
try {
fs.accessSync(f, fs.constants.X_OK);
} catch {
this.log.warn(`The current user ${UID}:${GID} does not have the permissions to execute "${f}".`);
success = false;
}
}
return success;
}
private isVenvCreated(): boolean {
let success: boolean = true;
for (const f of [
this.venvConfigPath,
this.venvPythonExecutable,
this.venvPipExecutable,
]) {
if (fs.existsSync(f) === false) {
this.log.debug(`${f} does not exist --> venv is not present`);
success = false;
}
}
return success;
}
private isVenvExecutable(): boolean {
let success: boolean = true;
for (const f of [
this.venvPythonExecutable,
this.venvPipExecutable,
]) {
try {
fs.accessSync(f, fs.constants.X_OK);
} catch {
this.log.warn(`The current user ${UID}:${GID} does not have the permissions to execute "${f}".`);
success = false;
}
}
return success;
}
private async openSSL(): Promise<void> {
const [openSSLVersionString]: [string, string, number | null] =
await runCommand(this.log, 'openssl', ['version'], undefined, true);
const r: RegExpMatchArray | null = openSSLVersionString.match(/\d+\.\d+\.\d+/);
if (r !== null && compareVersions(MIN_OPENSSL_VERSION, r[0]) !== 1) {
this.log.info(`OpenSSL ${r[0]} is installed and compatible.`);
return;
}
if (r === null) {
this.log.warn('Could not verify that the correct OpenSSL version is installed. Falling back to openssl legacy mode. Be aware \
that Python 3.12 or later is not compatible with openssl legacy mode.');
} else {
this.log.warn(`You are using OpenSSL ${r[0]}. However, OpenSSL ${MIN_OPENSSL_VERSION} or later is required for the most recent \
AppleTV enhanced version. Falling back to openssl legacy mode. Be aware that Python 3.12 or later is not compatible with openssl legacy \
mode.`);
}
this.requirementsPath = path.join(__dirname, '..', 'python_requirements', 'openssl_legacy', 'requirements.txt');
supportedPythonVersions = supportedPythonVersions.filter((e) => e !== '3.12' && e !== '3.13');
}
private async updatePip(): Promise<boolean> {
return (await runCommand(this.log, this.venvPipExecutable, ['install', '--upgrade', 'pip'])).at(2) === 0;
}
}
export default PythonChecker;