|
| 1 | +import type { Pep440Version } from '@renovatebot/pep440'; |
| 2 | +import { AvailableVersionsCommand, type AvailableVersionsExecuteArgs } from '../../base/commands/index'; |
| 3 | +import { runPython, runUV } from '../helpers'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Pip available versions command. |
| 7 | + * Parsed command: `python -m pip index versions <package> --json --python-version <version>` |
| 8 | + * Official documentation: https://pip.pypa.io/en/stable/cli/pip_index/ |
| 9 | + */ |
| 10 | +export class PipAvailableVersionsCommand extends AvailableVersionsCommand { |
| 11 | + protected buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[] { |
| 12 | + return [ |
| 13 | + '-m', |
| 14 | + 'pip', |
| 15 | + 'index', |
| 16 | + 'versions', |
| 17 | + executeArgs.packageName, |
| 18 | + '--json', |
| 19 | + '--python-version', |
| 20 | + executeArgs.pythonVersion, |
| 21 | + ]; |
| 22 | + } |
| 23 | + |
| 24 | + async execute(executeArgs: AvailableVersionsExecuteArgs): Promise<Pep440Version[]> { |
| 25 | + const output = await runPython( |
| 26 | + this.pythonExecutable, |
| 27 | + this.buildCommand(executeArgs), |
| 28 | + undefined, |
| 29 | + this.log, |
| 30 | + executeArgs.cancellationToken, |
| 31 | + this.timeout, |
| 32 | + ); |
| 33 | + const match = output.match(/{[\s\S]*}/); |
| 34 | + if (!match) { |
| 35 | + return []; |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + const parsed = JSON.parse(match[0]) as { versions?: string[] }; |
| 40 | + return this.parseVersions( |
| 41 | + Array.isArray(parsed.versions) ? parsed.versions : [], |
| 42 | + executeArgs.includePrerelease, |
| 43 | + ); |
| 44 | + } catch { |
| 45 | + return []; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * UV available versions command. |
| 52 | + * Parsed command: `uv tool run pip index versions <package> --json --python-version <version>` |
| 53 | + * Official documentation: https://docs.astral.sh/uv/pip/ |
| 54 | + */ |
| 55 | +export class UvAvailableVersionsCommand extends AvailableVersionsCommand { |
| 56 | + protected buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[] { |
| 57 | + return [ |
| 58 | + 'tool', |
| 59 | + 'run', |
| 60 | + 'pip', |
| 61 | + 'index', |
| 62 | + 'versions', |
| 63 | + executeArgs.packageName, |
| 64 | + '--json', |
| 65 | + '--python-version', |
| 66 | + executeArgs.pythonVersion, |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + async execute(executeArgs: AvailableVersionsExecuteArgs): Promise<Pep440Version[]> { |
| 71 | + const output = await runUV( |
| 72 | + this.buildCommand(executeArgs), |
| 73 | + undefined, |
| 74 | + this.log, |
| 75 | + executeArgs.cancellationToken, |
| 76 | + this.timeout, |
| 77 | + ); |
| 78 | + const match = output.match(/{[\s\S]*}/); |
| 79 | + if (!match) { |
| 80 | + return []; |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + const parsed = JSON.parse(match[0]) as { versions?: string[] }; |
| 85 | + return this.parseVersions( |
| 86 | + Array.isArray(parsed.versions) ? parsed.versions : [], |
| 87 | + executeArgs.includePrerelease, |
| 88 | + ); |
| 89 | + } catch { |
| 90 | + return []; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments