* Asserts that the dependencies as listed in `requirements.txt` are installed. * * @privateRemarks This lists all installed packages with `pip` and then compares them to the * contents of our `requirements.txt`. Versions _must_ match exactly.
()
| 315 | * contents of our `requirements.txt`. Versions _must_ match exactly. |
| 316 | */ |
| 317 | protected async validatePythonDeps() { |
| 318 | log.debug(`Validating required ${NAME_PYTHON} package versions`); |
| 319 | |
| 320 | const pythonPath = this.pythonPath ?? (await findPython()); |
| 321 | if (!pythonPath) { |
| 322 | return this.fail(MESSAGE_PYTHON_MISSING); |
| 323 | } |
| 324 | |
| 325 | let pipListOutput: string; |
| 326 | try { |
| 327 | ({stdout: pipListOutput} = await exec(pythonPath, ['-m', NAME_PIP, 'list', '--format', 'json'])); |
| 328 | } catch { |
| 329 | return this.fail(`Could not find ${NAME_PIP} installation for Python at ${pythonPath}. Is it installed?`); |
| 330 | } |
| 331 | |
| 332 | let installedPkgs: PipPackage[]; |
| 333 | try { |
| 334 | installedPkgs = JSON.parse(pipListOutput) as PipPackage[]; |
| 335 | } catch { |
| 336 | return this.fail(`Could not parse output of "${NAME_PIP} list" as JSON: ${pipListOutput}`); |
| 337 | } |
| 338 | |
| 339 | const pkgsByName = Object.fromEntries(installedPkgs.map((pkg) => [pkg.name, pkg.version])); |
| 340 | log.debug('Installed Python packages: %O', pkgsByName); |
| 341 | |
| 342 | const requiredPackages = await this.parseRequirementsTxt(); |
| 343 | const missingPackages: PipPackage[] = []; |
| 344 | const invalidVersionPackages: [expected: PipPackage, actual: PipPackage][] = []; |
| 345 | for (const reqdPkg of requiredPackages) { |
| 346 | const version = pkgsByName[reqdPkg.name]; |
| 347 | if (!version) { |
| 348 | missingPackages.push(reqdPkg); |
| 349 | } else if (version !== reqdPkg.version) { |
| 350 | invalidVersionPackages.push([reqdPkg, {name: reqdPkg.name, version}]); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | const msgParts = []; |
| 355 | if (missingPackages.length) { |
| 356 | msgParts.push( |
| 357 | `The following required ${util.pluralize( |
| 358 | 'package', |
| 359 | missingPackages.length, |
| 360 | )} could not be found:\n${missingPackages.map((p) => `- ${p.name} @ ${p.version}`).join('\n')}`, |
| 361 | ); |
| 362 | } |
| 363 | if (invalidVersionPackages.length) { |
| 364 | msgParts.push( |
| 365 | `The following required ${util.pluralize( |
| 366 | 'package', |
| 367 | invalidVersionPackages.length, |
| 368 | )} are installed, but at the wrong version:\n${invalidVersionPackages |
| 369 | .map(([expected, actual]) => `- ${expected.name} @ ${expected.version} (found ${actual.version})`) |
| 370 | .join('\n')}`, |
| 371 | ); |
| 372 | } |
| 373 | if (msgParts.length) { |
| 374 | return this.fail(`Required Python dependency validation failed:\n\n${msgParts.join('\n\n')}`); |