diff --git a/package-lock.json b/package-lock.json index 1ac3bcbf..0d079c48 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6913,7 +6913,8 @@ "node_modules/tslib": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", - "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==" + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "peer": true }, "node_modules/tunnel": { "version": "0.0.6", @@ -12366,7 +12367,8 @@ "tslib": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", - "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==" + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "peer": true }, "tunnel": { "version": "0.0.6", diff --git a/package.json b/package.json index 5830cc3e..d2caf0a1 100644 --- a/package.json +++ b/package.json @@ -144,6 +144,156 @@ "description": "%python-envs.packageWatchers.description%", "default": true, "scope": "machine" + }, + "python-envs.packageManager.installCommandArgs": { + "type": "object", + "markdownDescription": "%python-envs.packageManager.installCommandArgs.description%", + "scope": "machine", + "properties": { + "verboseOutput": { + "type": "boolean", + "description": "%python-envs.packageManager.installCommandArgs.verboseOutput.description%", + "default": false + }, + "retryOnFailure": { + "type": "boolean", + "description": "%python-envs.packageManager.installCommandArgs.retryOnFailure.description%", + "default": true + }, + "maxRetries": { + "type": "integer", + "description": "%python-envs.packageManager.maxRetries.description%", + "default": 1, + "minimum": 0, + "maximum": 3 + } + }, + "additionalProperties": false + }, + "python-envs.packageManager.uninstallCommandArgs": { + "type": "object", + "markdownDescription": "%python-envs.packageManager.uninstallCommandArgs.description%", + "scope": "machine", + "properties": { + "verboseOutput": { + "type": "boolean", + "description": "%python-envs.packageManager.uninstallCommandArgs.verboseOutput.description%", + "default": false + }, + "retryOnFailure": { + "type": "boolean", + "description": "%python-envs.packageManager.uninstallCommandArgs.retryOnFailure.description%", + "default": true + }, + "maxRetries": { + "type": "integer", + "description": "%python-envs.packageManager.maxRetries.description%", + "default": 1, + "minimum": 0, + "maximum": 3 + } + }, + "additionalProperties": false + }, + "python-envs.packageManager.listCommandArgs": { + "type": "object", + "markdownDescription": "%python-envs.packageManager.listCommandArgs.description%", + "scope": "machine", + "properties": { + "verboseOutput": { + "type": "boolean", + "description": "%python-envs.packageManager.listCommandArgs.verboseOutput.description%", + "default": false + }, + "retryOnFailure": { + "type": "boolean", + "description": "%python-envs.packageManager.listCommandArgs.retryOnFailure.description%", + "default": true + }, + "maxRetries": { + "type": "integer", + "description": "%python-envs.packageManager.maxRetries.description%", + "default": 1, + "minimum": 0, + "maximum": 3 + } + }, + "additionalProperties": false + }, + "python-envs.packageManager.versionCommandArgs": { + "type": "object", + "markdownDescription": "%python-envs.packageManager.versionCommandArgs.description%", + "scope": "machine", + "properties": { + "verboseOutput": { + "type": "boolean", + "description": "%python-envs.packageManager.versionCommandArgs.verboseOutput.description%", + "default": false + }, + "retryOnFailure": { + "type": "boolean", + "description": "%python-envs.packageManager.versionCommandArgs.retryOnFailure.description%", + "default": true + }, + "maxRetries": { + "type": "integer", + "description": "%python-envs.packageManager.maxRetries.description%", + "default": 1, + "minimum": 0, + "maximum": 3 + } + }, + "additionalProperties": false + }, + "python-envs.packageManager.availableVersionsCommandArgs": { + "type": "object", + "markdownDescription": "%python-envs.packageManager.availableVersionsCommandArgs.description%", + "scope": "machine", + "properties": { + "verboseOutput": { + "type": "boolean", + "description": "%python-envs.packageManager.availableVersionsCommandArgs.verboseOutput.description%", + "default": false + }, + "retryOnFailure": { + "type": "boolean", + "description": "%python-envs.packageManager.availableVersionsCommandArgs.retryOnFailure.description%", + "default": true + }, + "maxRetries": { + "type": "integer", + "description": "%python-envs.packageManager.maxRetries.description%", + "default": 1, + "minimum": 0, + "maximum": 3 + } + }, + "additionalProperties": false + }, + "python-envs.packageManager.listDirectNamesCommandArgs": { + "type": "object", + "markdownDescription": "%python-envs.packageManager.listDirectNamesCommandArgs.description%", + "scope": "machine", + "properties": { + "verboseOutput": { + "type": "boolean", + "description": "%python-envs.packageManager.listDirectNamesCommandArgs.verboseOutput.description%", + "default": false + }, + "retryOnFailure": { + "type": "boolean", + "description": "%python-envs.packageManager.listDirectNamesCommandArgs.retryOnFailure.description%", + "default": true + }, + "maxRetries": { + "type": "integer", + "description": "%python-envs.packageManager.maxRetries.description%", + "default": 1, + "minimum": 0, + "maximum": 3 + } + }, + "additionalProperties": false } } }, diff --git a/package.nls.json b/package.nls.json index 483ecfd2..1ef41b43 100644 --- a/package.nls.json +++ b/package.nls.json @@ -47,5 +47,24 @@ "python-envs.runPetInTerminal.title": "Run Python Environment Tool (PET) in Terminal...", "python-envs.managePackageVersion.title": "Manage Package Version", "python-envs.alwaysUseUv.description": "When set to true, uv will be used to manage all virtual environments if available. When set to false, uv will only manage virtual environments explicitly created by uv.", - "python-envs.packageWatchers.description": "When enabled, file system watchers monitor site-packages for install/uninstall changes and automatically refresh the package list. Disable this if you experience performance issues in large workspaces." + "python-envs.packageWatchers.description": "When enabled, file system watchers monitor site-packages for install/uninstall changes and automatically refresh the package list. Disable this if you experience performance issues in large workspaces.", + "python-envs.packageManager.installCommandArgs.description": "Arguments and settings for the install command (pip/uv install).", + "python-envs.packageManager.uninstallCommandArgs.description": "Arguments and settings for the uninstall command (pip/uv uninstall).", + "python-envs.packageManager.listCommandArgs.description": "Arguments and settings for the list command (pip/uv list).", + "python-envs.packageManager.versionCommandArgs.description": "Arguments and settings for the version command (pip/uv --version).", + "python-envs.packageManager.availableVersionsCommandArgs.description": "Arguments and settings for the availableVersions command (pip index versions).", + "python-envs.packageManager.listDirectNamesCommandArgs.description": "Arguments and settings for the listDirectNames command (pip list --not-required).", + "python-envs.packageManager.maxRetries.description": "Maximum number of retry attempts (0-3).", + "python-envs.packageManager.installCommandArgs.verboseOutput.description": "Display full output from the install command.", + "python-envs.packageManager.installCommandArgs.retryOnFailure.description": "Retry the install command once on failure.", + "python-envs.packageManager.uninstallCommandArgs.verboseOutput.description": "Display full output from the uninstall command.", + "python-envs.packageManager.uninstallCommandArgs.retryOnFailure.description": "Retry the uninstall command once on failure.", + "python-envs.packageManager.listCommandArgs.verboseOutput.description": "Display full output from the list command.", + "python-envs.packageManager.listCommandArgs.retryOnFailure.description": "Retry the list command once on failure.", + "python-envs.packageManager.versionCommandArgs.verboseOutput.description": "Display full output from the version command.", + "python-envs.packageManager.versionCommandArgs.retryOnFailure.description": "Retry the version command once on failure.", + "python-envs.packageManager.availableVersionsCommandArgs.verboseOutput.description": "Display full output from the availableVersions command.", + "python-envs.packageManager.availableVersionsCommandArgs.retryOnFailure.description": "Retry the availableVersions command once on failure.", + "python-envs.packageManager.listDirectNamesCommandArgs.verboseOutput.description": "Display full output from the listDirectNames command.", + "python-envs.packageManager.listDirectNamesCommandArgs.retryOnFailure.description": "Retry the listDirectNames command once on failure." } diff --git a/src/managers/base/commands/availableVersions.ts b/src/managers/base/commands/availableVersions.ts new file mode 100644 index 00000000..ed495664 --- /dev/null +++ b/src/managers/base/commands/availableVersions.ts @@ -0,0 +1,22 @@ +import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand'; + +/** + * Arguments for available versions command execution (change per execution). + */ +export interface AvailableVersionsExecuteArgs extends BaseExecuteArgs { + packageName: string; + pythonVersion: string; + includePrerelease?: boolean; +} + +/** + * Template class for availableVersions commands. + * Subclasses implement concrete package-manager-specific logic. + */ +export abstract class AvailableVersionsCommand extends PackageManagerCommand { + protected static readonly configSection = 'availableVersionsCommandArgs'; + + protected abstract buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[]; + + abstract execute(executeArgs: AvailableVersionsExecuteArgs): Promise; +} diff --git a/src/managers/base/commands/index.ts b/src/managers/base/commands/index.ts new file mode 100644 index 00000000..41a16fd4 --- /dev/null +++ b/src/managers/base/commands/index.ts @@ -0,0 +1,7 @@ +export { AvailableVersionsCommand, type AvailableVersionsExecuteArgs } from './availableVersions'; +export { InstallCommand, type InstallExecuteArgs } from './install'; +export { ListCommand } from './list'; +export { ListDirectNamesCommand } from './listDirectNames'; +export { BaseExecuteArgs, CommandConstructorOptions, PackageManagerCommand } from './packageManagerCommand'; +export { UninstallCommand, type UninstallExecuteArgs } from './uninstall'; +export { VersionCommand } from './version'; diff --git a/src/managers/base/commands/install.ts b/src/managers/base/commands/install.ts new file mode 100644 index 00000000..83290aba --- /dev/null +++ b/src/managers/base/commands/install.ts @@ -0,0 +1,21 @@ +import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand'; + +/** + * Arguments for install command execution (change per execution). + */ +export interface InstallExecuteArgs extends BaseExecuteArgs { + packages: { packageName: string; version?: string }[]; + upgrade?: boolean; +} + +/** + * Template class for install commands. + * Subclasses implement concrete package-manager-specific logic. + */ +export abstract class InstallCommand extends PackageManagerCommand { + protected static readonly configSection = 'installCommandArgs'; + + protected abstract buildCommand(executeArgs: InstallExecuteArgs): string[]; + + abstract execute(executeArgs: InstallExecuteArgs): Promise; +} diff --git a/src/managers/base/commands/list.ts b/src/managers/base/commands/list.ts new file mode 100644 index 00000000..d857a74e --- /dev/null +++ b/src/managers/base/commands/list.ts @@ -0,0 +1,12 @@ +import { PackageInfo } from '../../../api'; +import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand'; + +/** + * Template class for list commands. + * Subclasses implement concrete package-manager-specific logic. + */ +export abstract class ListCommand extends PackageManagerCommand { + protected static readonly configSection = 'listCommandArgs'; + + abstract execute(executeArgs?: BaseExecuteArgs): Promise; +} diff --git a/src/managers/base/commands/listDirectNames.ts b/src/managers/base/commands/listDirectNames.ts new file mode 100644 index 00000000..fcd418f8 --- /dev/null +++ b/src/managers/base/commands/listDirectNames.ts @@ -0,0 +1,11 @@ +import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand'; + +/** + * Template class for listDirectNames commands. + * Subclasses implement concrete package-manager-specific logic. + */ +export abstract class ListDirectNamesCommand extends PackageManagerCommand { + protected static readonly configSection = 'listDirectNamesCommandArgs'; + + abstract execute(executeArgs?: BaseExecuteArgs): Promise; +} diff --git a/src/managers/base/commands/packageManagerCommand.ts b/src/managers/base/commands/packageManagerCommand.ts new file mode 100644 index 00000000..d670e0fa --- /dev/null +++ b/src/managers/base/commands/packageManagerCommand.ts @@ -0,0 +1,77 @@ +import { CancellationToken, l10n, LogOutputChannel, ProgressLocation, window, WorkspaceConfiguration } from 'vscode'; +import { getConfiguration } from '../../../common/workspace.apis'; + +/** + * Base interface for all command execute arguments. + * Provides optional cancellation token that all commands can use. + */ +export interface BaseExecuteArgs { + cancellationToken?: CancellationToken; + showProgress?: boolean; +} + +/** + * Constructor options shared by all package manager commands. + */ +export interface CommandConstructorOptions { + pythonExecutable: string; + configSection?: string; + log?: LogOutputChannel; +} + +/** + * Base class for all package manager commands. + * Provides common properties and minimal interface for subclasses. + */ +export abstract class PackageManagerCommand { + protected static readonly configSection?: string; + + protected pythonExecutable: string; + protected log?: LogOutputChannel; + protected timeout: number = 300000; + protected config?: WorkspaceConfiguration; + + constructor(options: CommandConstructorOptions) { + this.pythonExecutable = options.pythonExecutable; + this.log = options.log; + const configSection = options.configSection ?? (this.constructor as typeof PackageManagerCommand).configSection; + this.config = configSection ? getConfiguration(`python-envs.packageManager.${configSection}`) : undefined; + } + + /** + * Executes this command and optionally wraps execution with a progress indicator. + */ + public executeWithProgress( + executeArgs?: A, + title?: string, + ): Promise { + if (!executeArgs?.showProgress) { + return this.execute(executeArgs) as Promise; + } + + return Promise.resolve( + window.withProgress( + { + location: ProgressLocation.Notification, + title: title ?? l10n.t('Running package manager command'), + cancellable: true, + }, + (_progress, token) => + this.execute({ + ...executeArgs, + cancellationToken: executeArgs.cancellationToken ?? token, + }) as Promise, + ), + ); + } + + /** + * Subclasses implement command execution. + */ + abstract execute(executeArgs?: BaseExecuteArgs): Promise; + + /** + * Subclasses implement to build the command arguments. + */ + protected abstract buildCommand(executeArgs: BaseExecuteArgs): string[]; +} diff --git a/src/managers/base/commands/uninstall.ts b/src/managers/base/commands/uninstall.ts new file mode 100644 index 00000000..b9724f39 --- /dev/null +++ b/src/managers/base/commands/uninstall.ts @@ -0,0 +1,20 @@ +import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand'; + +/** + * Arguments for uninstall command execution (change per execution). + */ +export interface UninstallExecuteArgs extends BaseExecuteArgs { + packages: { packageName: string; version?: string }[]; +} + +/** + * Template class for uninstall commands. + * Subclasses implement concrete package-manager-specific logic. + */ +export abstract class UninstallCommand extends PackageManagerCommand { + protected static readonly configSection = 'uninstallCommandArgs'; + + protected abstract buildCommand(executeArgs: UninstallExecuteArgs): string[]; + + abstract execute(executeArgs: UninstallExecuteArgs): Promise; +} diff --git a/src/managers/base/commands/version.ts b/src/managers/base/commands/version.ts new file mode 100644 index 00000000..15c05856 --- /dev/null +++ b/src/managers/base/commands/version.ts @@ -0,0 +1,12 @@ +import type { Pep440Version } from '@renovatebot/pep440'; +import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand'; + +/** + * Template class for version commands. + * Subclasses implement concrete package-manager-specific logic. + */ +export abstract class VersionCommand extends PackageManagerCommand { + protected static readonly configSection = 'versionCommandArgs'; + + abstract execute(executeArgs?: BaseExecuteArgs): Promise; +} diff --git a/src/managers/builtin/commands/availableVersions.ts b/src/managers/builtin/commands/availableVersions.ts new file mode 100644 index 00000000..21e0b876 --- /dev/null +++ b/src/managers/builtin/commands/availableVersions.ts @@ -0,0 +1,121 @@ +import { + AvailableVersionsCommand, + CommandConstructorOptions, + type AvailableVersionsExecuteArgs, +} from '../../base/commands/index'; +import { runPython, runUV, shouldUseUv } from '../helpers'; + +/** + * Pip available versions command. + * + * Parsed Command: `python -m pip index versions --json --python-version ` + * + * Official Documentation: https://pip.pypa.io/en/stable/cli/pip_index/ + * The `pip index versions` command lists all available versions of a package on PyPI. + * The `--python-version` flag filters versions compatible with the specified Python version. + * The `--json` flag outputs results in JSON format for structured parsing. + */ +export class PipAvailableVersionsCommand extends AvailableVersionsCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + protected buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[] { + const baseVersion = executeArgs.pythonVersion.split('.').slice(0, 2).join('.'); + return ['-m', 'pip', 'index', 'versions', executeArgs.packageName, '--json', '--python-version', baseVersion]; + } + + async execute(executeArgs: AvailableVersionsExecuteArgs): Promise { + let availableVersions: string[] = []; + + const parser = (output: string): void => { + const match = output.match(/{[\s\S]*}/); + if (!match) { + availableVersions = []; + return; + } + try { + const parsed = JSON.parse(match[0]) as { versions?: string[] }; + let versions = Array.isArray(parsed.versions) ? parsed.versions.filter((v) => !!v.trim()) : []; + if (!executeArgs.includePrerelease) { + versions = versions.filter((version) => !/[ab]|rc|dev/i.test(version)); + } + availableVersions = versions; + } catch { + availableVersions = []; + } + }; + + const args = this.buildCommand(executeArgs); + + const output = await runPython( + this.pythonExecutable, + args, + undefined, + this.log, + executeArgs.cancellationToken, + this.timeout, + ); + + parser(output); + return availableVersions; + } +} + +/** + * UV available versions command. + * + * Parsed Command: `uv pip index versions --json --python-version ` + * + * Official Documentation: https://docs.astral.sh/uv/pip/ + * The `uv pip index versions` command lists all available versions of a package. + * The `--python-version` flag filters versions compatible with the specified Python version. + * The `--json` flag outputs results in JSON format for structured parsing. + */ +export class UvAvailableVersionsCommand extends AvailableVersionsCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[] { + const baseVersion = executeArgs.pythonVersion.split('.').slice(0, 2).join('.'); + return ['pip', 'index', 'versions', executeArgs.packageName, '--json', '--python-version', baseVersion]; + } + + async execute(executeArgs: AvailableVersionsExecuteArgs): Promise { + let availableVersions: string[] = []; + + const parser = (output: string): void => { + const match = output.match(/{[\s\S]*}/); + if (!match) { + availableVersions = []; + return; + } + try { + const parsed = JSON.parse(match[0]) as { versions?: string[] }; + let versions = Array.isArray(parsed.versions) ? parsed.versions.filter((v) => !!v.trim()) : []; + if (!executeArgs.includePrerelease) { + versions = versions.filter((version) => !/[ab]|rc|dev/i.test(version)); + } + availableVersions = versions; + } catch { + availableVersions = []; + } + }; + + const args = this.buildCommand(executeArgs); + + const output = await runUV(args, undefined, this.log, executeArgs.cancellationToken, this.timeout); + + parser(output); + return availableVersions; + } +} + +export async function BuiltinAvailableVersionsCommandFactory( + options: CommandConstructorOptions, +): Promise { + if (await shouldUseUv(options.log, options.pythonExecutable)) { + return new UvAvailableVersionsCommand(options); + } + return new PipAvailableVersionsCommand(options); +} diff --git a/src/managers/builtin/commands/index.ts b/src/managers/builtin/commands/index.ts new file mode 100644 index 00000000..0f15cf97 --- /dev/null +++ b/src/managers/builtin/commands/index.ts @@ -0,0 +1,6 @@ +export { PipAvailableVersionsCommand, UvAvailableVersionsCommand } from './availableVersions'; +export { PipInstallCommand, UvInstallCommand } from './install'; +export { PipListCommand, UvListCommand } from './list'; +export { PipListDirectNamesCommand, UvListDirectNamesCommand } from './listDirectNames'; +export { PipUninstallCommand, UvUninstallCommand } from './uninstall'; +export { PipVersionCommand, UvVersionCommand } from './version'; diff --git a/src/managers/builtin/commands/install.ts b/src/managers/builtin/commands/install.ts new file mode 100644 index 00000000..37ef96ee --- /dev/null +++ b/src/managers/builtin/commands/install.ts @@ -0,0 +1,96 @@ +import { getConfiguration } from '../../../common/workspace.apis'; +import { CommandConstructorOptions, InstallCommand, type InstallExecuteArgs } from '../../base/commands/index'; +import { runPython, runUV, shouldUseUv } from '../helpers'; +import { processEditableInstallArgs } from '../utils'; + +/** + * Pip install command. + * + * Parsed Command: `python -m pip install [--upgrade] [--index-url ] ` + * + * Official Documentation: https://pip.pypa.io/en/stable/cli/pip_install/ + * The `pip install` command installs packages from the Python Package Index (PyPI). + * Supports version pinning via `package==version` syntax and index URL configuration. + */ +export class PipInstallCommand extends InstallCommand { + private indexUrl?: string; + + constructor(options: CommandConstructorOptions) { + super(options); + const config = getConfiguration('python-envs.packageManager'); + this.indexUrl = config.get('indexUrl'); + } + + protected buildCommand(executeArgs: InstallExecuteArgs): string[] { + let args = ['-m', 'pip', 'install']; + + if (this.indexUrl) { + args.push('--index-url', this.indexUrl); + } + + if (executeArgs.upgrade) { + args.push('--upgrade'); + } + + const processedArgs = processEditableInstallArgs(executeArgs.packages.map((pkg) => pkg.packageName)); + args.push(...processedArgs); + + return args; + } + + async execute(executeArgs: InstallExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + + await runPython(this.pythonExecutable, args, undefined, this.log, executeArgs.cancellationToken, this.timeout); + } +} + +/** + * UV install command. + * + * Parsed Command: `uv pip install --python [--upgrade] [--index-url ] ` + * + * Official Documentation: https://docs.astral.sh/uv/pip/ + * The `uv pip install` command is UV's high-performance Python package installer. + * UV is a Rust-based pip replacement that's faster than traditional pip. + * The `--python` flag specifies the target Python interpreter. + */ +export class UvInstallCommand extends InstallCommand { + private indexUrl?: string; + + constructor(options: CommandConstructorOptions) { + super(options); + const config = getConfiguration('python-envs.packageManager'); + this.indexUrl = config.get('indexUrl'); + } + + protected buildCommand(executeArgs: InstallExecuteArgs): string[] { + let args = ['pip', 'install', '--python', this.pythonExecutable]; + + if (this.indexUrl) { + args.push('--index-url', this.indexUrl); + } + + if (executeArgs.upgrade) { + args.push('--upgrade'); + } + + const processedArgs = processEditableInstallArgs(executeArgs.packages.map((pkg) => pkg.packageName)); + args.push(...processedArgs); + + return args; + } + + async execute(executeArgs: InstallExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + + await runUV(args, undefined, this.log, executeArgs.cancellationToken, this.timeout); + } +} + +export async function BuiltinInstallCommandFactory(options: CommandConstructorOptions): Promise { + if (await shouldUseUv(options.log, options.pythonExecutable)) { + return new UvInstallCommand(options); + } + return new PipInstallCommand(options); +} diff --git a/src/managers/builtin/commands/list.ts b/src/managers/builtin/commands/list.ts new file mode 100644 index 00000000..1745111a --- /dev/null +++ b/src/managers/builtin/commands/list.ts @@ -0,0 +1,123 @@ +import { PackageInfo } from '../../../api'; +import { CommandConstructorOptions, ListCommand, type BaseExecuteArgs } from '../../base/commands/index'; +import { runPython, runUV, shouldUseUv } from '../helpers'; + +/** + * Pip list command. + * + * Parsed Command: `python -m pip list --format=json` + * + * Official Documentation: https://pip.pypa.io/en/stable/cli/pip_list/ + * The `pip list` command shows all installed packages in the current environment. + * The `--format=json` flag outputs the list in JSON format for structured parsing. + */ +export class PipListCommand extends ListCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + protected buildCommand(): string[] { + return ['-m', 'pip', 'list', '--format=json']; + } + + async execute(executeArgs?: BaseExecuteArgs): Promise { + const packages: PackageInfo[] = []; + + const parser = (output: string): void => { + let json: unknown; + try { + json = JSON.parse(output); + } catch (e) { + this.log?.error(`Failed to parse pip list output: ${e}`); + return; + } + if (!Array.isArray(json)) { + this.log?.error('Invalid output from pip list command'); + return; + } + const parsed = json + .filter(({ name, version }) => name && version) + .map(({ name, version }) => ({ + name, + version, + displayName: name, + description: version, + })); + packages.push(...parsed); + }; + + const args = this.buildCommand(); + + const output = await runPython( + this.pythonExecutable, + args, + undefined, + this.log, + executeArgs?.cancellationToken, + this.timeout, + ); + + parser(output); + return packages; + } +} + +/** + * UV list command. + * + * Parsed Command: `uv pip list --format=json --python ` + * + * Official Documentation: https://docs.astral.sh/uv/pip/ + * The `uv pip list` command shows all installed packages via UV's pip interface. + * The `--python` flag specifies the target Python interpreter. + * The `--format=json` flag outputs the list in JSON format for structured parsing. + */ +export class UvListCommand extends ListCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(): string[] { + return ['pip', 'list', '--format=json', '--python', this.pythonExecutable]; + } + + async execute(executeArgs?: BaseExecuteArgs): Promise { + const packages: PackageInfo[] = []; + + const parser = (output: string): void => { + let json: unknown; + try { + json = JSON.parse(output); + } catch (e) { + this.log?.error(`Failed to parse uv pip list output: ${e}`); + return; + } + if (!Array.isArray(json)) { + this.log?.error('Invalid output from uv pip list command'); + return; + } + const parsed = json + .filter(({ name, version }) => name && version) + .map(({ name, version }) => ({ + name, + version, + displayName: name, + description: version, + })); + packages.push(...parsed); + }; + + const args = this.buildCommand(); + + const output = await runUV(args, undefined, this.log, executeArgs?.cancellationToken, this.timeout); + + parser(output); + return packages; + } +} + +export async function BuiltinListCommandFactory(options: CommandConstructorOptions): Promise { + if (await shouldUseUv(options.log, options.pythonExecutable)) { + return new UvListCommand(options); + } + return new PipListCommand(options); +} diff --git a/src/managers/builtin/commands/listDirectNames.ts b/src/managers/builtin/commands/listDirectNames.ts new file mode 100644 index 00000000..e834d5ca --- /dev/null +++ b/src/managers/builtin/commands/listDirectNames.ts @@ -0,0 +1,110 @@ +import { CommandConstructorOptions, ListDirectNamesCommand, type BaseExecuteArgs } from '../../base/commands/index'; +import { runPython, runUV, shouldUseUv } from '../helpers'; + +/** + * Pip list direct names command. + * + * Parsed Command: `python -m pip list --format=json --not-required` + * + * Official Documentation: https://pip.pypa.io/en/stable/cli/pip_list/ + * The `pip list --not-required` command lists only top-level (directly installed) packages. + * Excludes transitive dependencies that are installed as requirements of other packages. + * The `--format=json` flag outputs results in JSON format for structured parsing. + */ +export class PipListDirectNamesCommand extends ListDirectNamesCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + protected buildCommand(): string[] { + return ['-m', 'pip', 'list', '--format=json', '--not-required']; + } + + async execute(executeArgs?: BaseExecuteArgs): Promise { + let directNames: string[] = []; + + const parser = (output: string): void => { + let packages: unknown; + try { + packages = JSON.parse(output); + } catch (e) { + this.log?.error(`Failed to parse pip list output: ${e}`); + return; + } + if (!Array.isArray(packages)) { + this.log?.error('Invalid output from pip list command'); + return; + } + directNames = packages.filter(({ name }) => name).map(({ name }) => name); + }; + + const args = this.buildCommand(); + + const output = await runPython( + this.pythonExecutable, + args, + undefined, + this.log, + executeArgs?.cancellationToken, + this.timeout, + ); + + parser(output); + return directNames; + } +} + +/** + * UV list direct names command. + * + * Parsed Command: `uv pip list --format=json --not-required --python ` + * + * Official Documentation: https://docs.astral.sh/uv/pip/ + * The `uv pip list --not-required` command lists only top-level (directly installed) packages. + * Excludes transitive dependencies that are installed as requirements of other packages. + * The `--format=json` flag outputs results in JSON format for structured parsing. + * The `--python` flag specifies the target Python interpreter. + */ +export class UvListDirectNamesCommand extends ListDirectNamesCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(): string[] { + return ['pip', 'list', '--format=json', '--not-required', '--python', this.pythonExecutable]; + } + + async execute(executeArgs?: BaseExecuteArgs): Promise { + let directNames: string[] = []; + + const parser = (output: string): void => { + let packages: unknown; + try { + packages = JSON.parse(output); + } catch (e) { + this.log?.error(`Failed to parse uv pip list output: ${e}`); + return; + } + if (!Array.isArray(packages)) { + this.log?.error('Invalid output from uv pip list command'); + return; + } + directNames = packages.filter(({ name }) => name).map(({ name }) => name); + }; + + const args = this.buildCommand(); + + const output = await runUV(args, undefined, this.log, executeArgs?.cancellationToken, this.timeout); + + parser(output); + return directNames; + } +} + +export async function BuiltinListDirectNamesCommandFactory( + options: CommandConstructorOptions, +): Promise { + if (await shouldUseUv(options.log, options.pythonExecutable)) { + return new UvListDirectNamesCommand(options); + } + return new PipListDirectNamesCommand(options); +} diff --git a/src/managers/builtin/commands/uninstall.ts b/src/managers/builtin/commands/uninstall.ts new file mode 100644 index 00000000..c06ee38a --- /dev/null +++ b/src/managers/builtin/commands/uninstall.ts @@ -0,0 +1,61 @@ +import { CommandConstructorOptions, UninstallCommand, type UninstallExecuteArgs } from '../../base/commands/index'; +import { runPython, runUV, shouldUseUv } from '../helpers'; + +/** + * Pip uninstall command. + * + * Parsed Command: `python -m pip uninstall -y ` + * + * Official Documentation: https://pip.pypa.io/en/stable/cli/pip_uninstall/ + * The `pip uninstall` command uninstalls installed packages from the current environment. + * The `-y` flag automatically confirms the uninstallation without prompting. + */ +export class PipUninstallCommand extends UninstallCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + protected buildCommand(executeArgs: UninstallExecuteArgs): string[] { + return ['-m', 'pip', 'uninstall', '-y', ...executeArgs.packages.map((pkg) => pkg.packageName)]; + } + + async execute(executeArgs: UninstallExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + + await runPython(this.pythonExecutable, args, undefined, this.log, executeArgs.cancellationToken, this.timeout); + } +} + +/** + * UV uninstall command. + * + * Parsed Command: `uv pip uninstall -y --python ` + * + * Official Documentation: https://docs.astral.sh/uv/pip/ + * The `uv pip uninstall` command removes packages from the Python environment via UV. + * The `-y` flag automatically confirms uninstallation without prompting. + * The `--python` flag specifies the target Python interpreter. + */ +export class UvUninstallCommand extends UninstallCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(executeArgs: UninstallExecuteArgs): string[] { + const args = ['pip', 'uninstall', '-y', '--python', this.pythonExecutable]; + args.push(...executeArgs.packages.map((pkg) => pkg.packageName)); + return args; + } + + async execute(executeArgs: UninstallExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + + await runUV(args, undefined, this.log, executeArgs.cancellationToken, this.timeout); + } +} + +export async function BuiltinUninstallCommandFactory(options: CommandConstructorOptions): Promise { + if (await shouldUseUv(options.log, options.pythonExecutable)) { + return new UvUninstallCommand(options); + } + return new PipUninstallCommand(options); +} diff --git a/src/managers/builtin/commands/version.ts b/src/managers/builtin/commands/version.ts new file mode 100644 index 00000000..5393103e --- /dev/null +++ b/src/managers/builtin/commands/version.ts @@ -0,0 +1,89 @@ +import type { Pep440Version } from '@renovatebot/pep440'; +import { explain as parsePep440Version } from '@renovatebot/pep440'; +import { CommandConstructorOptions, VersionCommand, type BaseExecuteArgs } from '../../base/commands/index'; +import { runPython, runUV, shouldUseUv } from '../helpers'; + +/** + * Pip version command. + * + * Parsed Command: `python -m pip --version` + * + * Official Documentation: https://pip.pypa.io/en/stable/cli/pip/ + * The `pip --version` command displays the current version of pip. + * Output format: "pip X.Y.Z from /path/to/pip (python X.Y)" + */ +export class PipVersionCommand extends VersionCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + protected buildCommand(): string[] { + return ['-m', 'pip', '--version']; + } + + async execute(executeArgs?: BaseExecuteArgs): Promise { + let parsedVersion: Pep440Version | undefined; + + const parser = (output: string): void => { + // "pip X.Y.Z from /path/to/pip (python X.Y)" + const match = output.match(/^pip\s+(\d+\.\d+(?:\.\d+)*)/); + parsedVersion = match ? (parsePep440Version(match[1]) ?? undefined) : undefined; + }; + + const args = this.buildCommand(); + + const output = await runPython( + this.pythonExecutable, + args, + undefined, + this.log, + executeArgs?.cancellationToken, + this.timeout, + ); + + parser(output); + return parsedVersion; + } +} + +/** + * UV version command. + * + * Parsed Command: `uv --version` + * + * Official Documentation: https://docs.astral.sh/uv/ + * The `uv --version` command displays the current version of UV. + * Output format: "uv X.Y.Z" + */ +export class UvVersionCommand extends VersionCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(): string[] { + return ['--version']; + } + + async execute(executeArgs?: BaseExecuteArgs): Promise { + let parsedVersion: Pep440Version | undefined; + + const parser = (output: string): void => { + // "uv X.Y.Z" format + const match = output.match(/(\d+\.\d+(?:\.\d+)*)/); + parsedVersion = match ? (parsePep440Version(match[1]) ?? undefined) : undefined; + }; + + const args = this.buildCommand(); + + const output = await runUV(args, undefined, this.log, executeArgs?.cancellationToken, this.timeout); + + parser(output); + return parsedVersion; + } +} + +export async function BuiltinVersionCommandFactory(options: CommandConstructorOptions): Promise { + if (await shouldUseUv(options.log, options.pythonExecutable)) { + return new UvVersionCommand(options); + } + return new PipVersionCommand(options); +} diff --git a/src/managers/builtin/helpers.ts b/src/managers/builtin/helpers.ts index 911bc603..74a13908 100644 --- a/src/managers/builtin/helpers.ts +++ b/src/managers/builtin/helpers.ts @@ -133,7 +133,7 @@ export async function runPython( proc.stderr?.on('data', (data) => { const s = data.toString('utf-8'); builder += s; - log?.append(`python: ${s}`); + log?.append(s); }); proc.on('close', () => { resolve(builder); diff --git a/src/managers/builtin/pipListUtils.ts b/src/managers/builtin/pipListUtils.ts deleted file mode 100644 index 80519d89..00000000 --- a/src/managers/builtin/pipListUtils.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { LogOutputChannel } from 'vscode'; - -export interface PipPackage { - name: string; - version: string; - displayName: string; - description: string; -} -export function parseUvTree(data: string): string[] { - return data - .split('\n') - .map((line) => line.trim()) - .map((line) => line.split(/\s+/, 1)[0]) - .filter((name) => !!name); -} - -export function parsePipListJson(data: string, log?: LogOutputChannel): PipPackage[] { - try { - const json = JSON.parse(data); - if (Array.isArray(json)) { - return json - .filter((item) => item.name && item.version) - .map(({ name, version }) => ({ - name, - version, - displayName: name, - description: version, - })); - } - } catch (ex) { - log?.error('Failed to parse pip list JSON output', ex); - } - return []; -} diff --git a/src/managers/builtin/pipPackageManager.ts b/src/managers/builtin/pipPackageManager.ts index f0e09a7d..8b87a05c 100644 --- a/src/managers/builtin/pipPackageManager.ts +++ b/src/managers/builtin/pipPackageManager.ts @@ -1,5 +1,5 @@ import type { Pep440Version } from '@renovatebot/pep440'; -import { compare, explain as parse, rcompare } from '@renovatebot/pep440'; +import { compare, explain as parse } from '@renovatebot/pep440'; import { CancellationError, Disposable, @@ -21,10 +21,17 @@ import { PythonEnvironment, PythonEnvironmentApi, } from '../../api'; +import { CommandConstructorOptions } from '../base/commands/index'; import { updatePackagesAndNotify } from '../common/packageChanges'; -import { runPython, runUV, shouldUseUv } from './helpers'; +import { BuiltinAvailableVersionsCommandFactory } from './commands/availableVersions'; +import { PipAvailableVersionsCommand } from './commands/index'; +import { BuiltinInstallCommandFactory } from './commands/install'; +import { BuiltinListCommandFactory } from './commands/list'; +import { BuiltinListDirectNamesCommandFactory } from './commands/listDirectNames'; +import { BuiltinUninstallCommandFactory } from './commands/uninstall'; +import { BuiltinVersionCommandFactory } from './commands/version'; import { getWorkspacePackagesToInstall } from './pipUtils'; -import { managePackages, normalizePackageName, refreshPipDirectPackageNames, refreshPipPackages } from './utils'; +import { normalizePackageName, parsePackageSpecs } from './utils'; import { VenvManager } from './venvManager'; export class PipPackageManager implements PackageManager, Disposable { @@ -65,43 +72,52 @@ export class PipPackageManager implements PackageManager, Disposable { } } - const manageOptions = { - ...options, - install: toInstall, - uninstall: toUninstall, - }; - await window.withProgress( - { - location: ProgressLocation.Notification, - title: 'Installing packages', - cancellable: true, - }, - async (_progress, token) => { - try { - await managePackages(environment, manageOptions, this, token); - await updatePackagesAndNotify( - this, - environment, - this.packages.get(environment.envId.id), - (changes) => { - this._onDidChangePackages.fire({ environment, manager: this, changes }); - }, - ); - } catch (e) { - if (e instanceof CancellationError) { - throw e; - } - this.log.error('Error managing packages', e); - setImmediate(async () => { - const result = await window.showErrorMessage('Error managing packages', 'View Output'); - if (result === 'View Output') { - this.log.show(); - } - }); - throw e; + try { + const pythonExecutable = environment.execInfo?.run?.executable; + if (!pythonExecutable) { + throw new Error('Unable to determine Python executable path'); + } + + // Centralize command options for install/uninstall operations + const manageCommandOptions: CommandConstructorOptions = { + pythonExecutable, + log: this.log, + }; + + // Execute uninstall if needed + if (toUninstall.length > 0) { + const uninstallCmd = await BuiltinUninstallCommandFactory(manageCommandOptions); + const packages = parsePackageSpecs(toUninstall); + await uninstallCmd.executeWithProgress({ packages, showProgress: true }, 'Installing packages'); + } + + // Execute install if needed + if (toInstall.length > 0) { + const installCmd = await BuiltinInstallCommandFactory(manageCommandOptions); + const packages = parsePackageSpecs(toInstall); + await installCmd.executeWithProgress( + { packages, upgrade: options.upgrade, showProgress: true }, + 'Installing packages', + ); + } + + await updatePackagesAndNotify(this, environment, this.packages.get(environment.envId.id), (changes) => { + this._onDidChangePackages.fire({ environment, manager: this, changes }); + }); + } catch (e) { + if (e instanceof CancellationError) { + // Cancellation is a normal control-flow exit; do not surface an error. + return; + } + this.log.error('Error managing packages', e); + setImmediate(async () => { + const result = await window.showErrorMessage('Error managing packages', 'View Output'); + if (result === 'View Output') { + this.log.show(); } - }, - ); + }); + throw e; + } } async refresh(environment: PythonEnvironment): Promise { @@ -125,7 +141,15 @@ export class PipPackageManager implements PackageManager, Disposable { async getPackages(environment: PythonEnvironment, options?: GetPackagesOptions): Promise { if (options?.skipCache || !this.packages.has(environment.envId.id)) { - const data = await refreshPipPackages(environment, this.log); + const pythonExecutable = environment.execInfo?.run?.executable; + if (!pythonExecutable) { + return undefined; + } + const listCmd = await BuiltinListCommandFactory({ + pythonExecutable, + log: this.log, + }); + const data = await listCmd.execute(); const packages = (data ?? []).map((pkg) => this.api.createPackageItem(pkg, environment, this)); this.packages.set(environment.envId.id, packages); return packages; @@ -135,22 +159,15 @@ export class PipPackageManager implements PackageManager, Disposable { async getVersion(environment: PythonEnvironment): Promise { try { - const useUv = await shouldUseUv(this.log, environment.environmentPath.fsPath); - if (useUv) { - const result = await runUV(['--version'], undefined, this.log); - // "uv X.Y.Z" - const match = result.match(/^uv\s+(\d+\.\d+(?:\.\d+)*)/); - return match ? (parse(match[1]) ?? undefined) : undefined; + const pythonExecutable = environment.execInfo?.run?.executable; + if (!pythonExecutable) { + return undefined; } - const result = await runPython( - environment.execInfo?.run?.executable ?? 'python', - ['-m', 'pip', '--version'], - undefined, - this.log, - ); - // "pip X.Y.Z from /path/to/pip (python X.Y)" - const match = result.match(/^pip\s+(\d+\.\d+(?:\.\d+)*)/); - return match ? (parse(match[1]) ?? undefined) : undefined; + const versionCmd = await BuiltinVersionCommandFactory({ + pythonExecutable, + log: this.log, + }); + return await versionCmd.execute(); } catch { return undefined; } @@ -161,8 +178,8 @@ export class PipPackageManager implements PackageManager, Disposable { packageName: string, ): Promise { try { - const python = environment.execInfo?.run?.executable; - if (!python) { + const pythonExecutable = environment.execInfo?.run?.executable; + if (!pythonExecutable) { return undefined; } @@ -170,30 +187,29 @@ export class PipPackageManager implements PackageManager, Disposable { if (!baseVersion) { return undefined; } - // uv - Run pip via `uv tool run pip` - const useUv = await shouldUseUv(this.log, environment.environmentPath.fsPath); - if (useUv) { - const output = await runUV( - ['tool', 'run', 'pip', 'index', 'versions', packageName, '--json', '--python-version', baseVersion], - undefined, - this.log, - ); - return parsePipIndexVersionsJson(output); - } - // pip >= 21.2.0 - use `pip index versions --json` to get available versions in a machine readable format. - const pipVersion = await this.getVersion(environment); - if (pipVersion && compare(pipVersion.public, '21.2.0') >= 0) { - const output = await runPython( - python, - ['-m', 'pip', 'index', 'versions', packageName, '--json', '--python-version', baseVersion], - undefined, - this.log, - ); - return parsePipIndexVersionsJson(output); + const availableVersionsCmd = await BuiltinAvailableVersionsCommandFactory({ + pythonExecutable, + log: this.log, + }); + + // For pip < 21.2.0, check version first + if (availableVersionsCmd instanceof PipAvailableVersionsCommand) { + const pipVersion = await this.getVersion(environment); + if (!pipVersion || compare(pipVersion.public, '21.2.0') < 0) { + // pip <= 20.3.4 - version picking is undefined; no reliable machine-readable API exists. + return undefined; + } } - // pip <= 20.3.4 - version picking is undefined; no reliable machine-readable API exists. + const versionStrings = await availableVersionsCmd.execute({ + packageName, + pythonVersion: environment.version, + }); + return versionStrings + .map((v) => parse(v)) + .filter((parsed): parsed is Pep440Version => parsed !== null) + .sort((a, b) => compare(b.public, a.public)); } catch { return undefined; } @@ -205,38 +221,21 @@ export class PipPackageManager implements PackageManager, Disposable { } /** - * Returns direct (non-transitive) package names using `pip list --not-required` or `uv pip tree --depth=0`. + * Returns direct (non-transitive) package names using `pip list --not-required` or `uv pip list --not-required`. * * Note: These commands return packages with no installed dependents (leaf packages), not packages * the user explicitly installed. pip/uv do not track install intent. */ async getDirectPackageNames(environment: PythonEnvironment): Promise | undefined> { - const data = await refreshPipDirectPackageNames(environment, this.log); - return data ? new Set(data.map(normalizePackageName)) : undefined; - } -} - -/** - * Parses JSON output from `pip index versions --json`. - * Expected format: { "name": "...", "versions": ["1.2.3", "1.2.2", ...] } - */ -export function parsePipIndexVersionsJson(output: string): Pep440Version[] | undefined { - // Only capture output between braces - const match = output.match(/{[\s\S]*}/); - if (!match) { - return undefined; - } - try { - const parsed = JSON.parse(match[0]); - if (parsed && Array.isArray(parsed.versions) && parsed.versions.length > 0) { - return (parsed.versions as string[]) - .filter((v) => !!v.trim()) - .map((v) => parse(v.trim())) - .filter((v): v is Pep440Version => v !== null) - .sort((a, b) => rcompare(a.public, b.public)); + const pythonExecutable = environment.execInfo?.run?.executable; + if (!pythonExecutable) { + return undefined; } - return undefined; - } catch { - return undefined; + const listDirectNamesCmd = await BuiltinListDirectNamesCommandFactory({ + pythonExecutable, + log: this.log, + }); + const data = await listDirectNamesCmd.execute(); + return data ? new Set(data.map(normalizePackageName)) : undefined; } } diff --git a/src/managers/builtin/pipUtils.ts b/src/managers/builtin/pipUtils.ts index 055ddb96..99a311bd 100644 --- a/src/managers/builtin/pipUtils.ts +++ b/src/managers/builtin/pipUtils.ts @@ -13,7 +13,8 @@ import { findFiles } from '../../common/workspace.apis'; import { selectFromCommonPackagesToInstall, selectFromInstallableToInstall } from '../common/pickers'; import { Installable } from '../common/types'; import { mergePackages } from '../common/utils'; -import { normalizePackageName, refreshPipPackages } from './utils'; +import { BuiltinListCommandFactory } from './commands/list'; +import { normalizePackageName } from './utils'; export interface PyprojectToml { project?: { @@ -275,7 +276,12 @@ export async function getWorkspacePackagesToInstall( let common = await getCommonPackages(); let installed: string[] | undefined; if (environment) { - installed = (await refreshPipPackages(environment, log, { showProgress: true }))?.map((pkg) => pkg.name); + const pythonExecutable = environment.execInfo?.run?.executable; + if (pythonExecutable) { + const listCmd = await BuiltinListCommandFactory({ pythonExecutable, log }); + const data = await listCmd.executeWithProgress<{ name: string }[]>({ showProgress: true }); + installed = data?.map((pkg) => pkg.name); + } common = mergePackages(common, installed ?? []); } return selectWorkspaceOrCommon(installableResult, common, !!options.showSkipOption, installed ?? []); @@ -394,10 +400,7 @@ export async function getProjectInstallable( !path.isAbsolute(relative)) ); }) - .sort( - (a, b) => - path.dirname(b.fsPath).length - path.dirname(a.fsPath).length, - ) + .sort((a, b) => path.dirname(b.fsPath).length - path.dirname(a.fsPath).length) : []; if (preferredCandidates.length > 0) { @@ -502,17 +505,3 @@ export async function shouldProceedAfterPyprojectValidation( return false; } - -export function isPipInstallCommand(command: string): boolean { - // Regex to match pip install commands, capturing variations like: - // pip install package - // python -m pip install package - // pip3 install package - // py -m pip install package - // pip install -r requirements.txt - // uv pip install package - // poetry run pip install package - // pipx run pip install package - // Any other tool that might wrap pip install - return /(?:^|\s)(?:\S+\s+)*(?:pip\d*)\s+(install|uninstall)\b/.test(command); -} diff --git a/src/managers/builtin/utils.ts b/src/managers/builtin/utils.ts index dc44fe75..04634803 100644 --- a/src/managers/builtin/utils.ts +++ b/src/managers/builtin/utils.ts @@ -1,19 +1,10 @@ -import { CancellationToken, LogOutputChannel, ProgressLocation, QuickPickItem, Uri, window } from 'vscode'; -import { - EnvironmentManager, - Package, - PackageManagementOptions, - PackageManager, - PythonEnvironment, - PythonEnvironmentApi, - PythonEnvironmentInfo, -} from '../../api'; -import { showErrorMessageWithLogs } from '../../common/errors/utils'; +import { LogOutputChannel, QuickPickItem, Uri, window } from 'vscode'; +import { EnvironmentManager, Package, PythonEnvironment, PythonEnvironmentApi, PythonEnvironmentInfo } from '../../api'; import { getExtension } from '../../common/extension.apis'; import { Common, PixiStrings, SysManagerStrings } from '../../common/localize'; import { traceInfo, traceVerbose } from '../../common/logging'; import { getGlobalPersistentState } from '../../common/persistentState'; -import { showInformationMessage, withProgress } from '../../common/window.apis'; +import { showInformationMessage } from '../../common/window.apis'; import { openExtension } from '../../common/workbenchCommands'; import { isNativeEnvInfo, @@ -22,13 +13,22 @@ import { NativePythonFinder, } from '../common/nativePythonFinder'; import { shortenVersionString, sortEnvironments } from '../common/utils'; -import { runPython, runUV, shouldUseUv } from './helpers'; -import { parsePipListJson, parseUvTree, PipPackage } from './pipListUtils'; const PIXI_EXTENSION_ID = 'renan-r-santos.pixi-code'; const PIXI_RECOMMEND_DONT_ASK_KEY = 'pixi-extension-recommend-dont-ask'; let pixiRecommendationShown = false; +/** + * Parse package specifications (strings) into package objects. + * Each string becomes a package object with packageName and empty version. + */ +export function parsePackageSpecs(packageStrings: string[]): { packageName: string; version?: string }[] { + return packageStrings.map((pkg) => ({ + packageName: pkg, + version: undefined, + })); +} + function asPackageQuickPickItem(name: string, version?: string): QuickPickItem { return { label: name, @@ -183,153 +183,6 @@ export async function refreshPythons( return sortEnvironments(collection); } -const PIP_LIST_TIMEOUT_MS = 30_000; - -async function execPipList(environment: PythonEnvironment, log?: LogOutputChannel, args?: string[]): Promise { - // Use environmentPath directly for consistency with UV environment tracking - const useUv = await shouldUseUv(log, environment.environmentPath.fsPath); - if (useUv) { - return await runUV( - ['pip', 'list', '--python', environment.execInfo.run.executable, '--format=json', ...(args ?? [])], - undefined, - log, - undefined, - PIP_LIST_TIMEOUT_MS, - ); - } - try { - return await runPython( - environment.execInfo.run.executable, - ['-m', 'pip', 'list', '--format=json', ...(args ?? [])], - undefined, - log, - undefined, - PIP_LIST_TIMEOUT_MS, - ); - } catch (ex) { - log?.error('Error running pip list', ex); - log?.info( - 'Package list retrieval attempted using pip, action can be done with uv if installed and setting `alwaysUseUv` is enabled.', - ); - throw ex; - } -} - -export async function refreshPipPackages( - environment: PythonEnvironment, - log?: LogOutputChannel, - options?: { showProgress: boolean }, -): Promise { - let data: string; - try { - if (options?.showProgress) { - data = await withProgress( - { - location: ProgressLocation.Notification, - }, - async () => { - return await execPipList(environment, log); - }, - ); - } else { - data = await execPipList(environment, log); - } - - return parsePipListJson(data, log); - } catch (e) { - log?.error('Error refreshing packages', e); - showErrorMessageWithLogs(SysManagerStrings.packageRefreshError, log); - return undefined; - } -} - -/** - * Returns names of packages with no installed dependents (leaf packages). - * - * Uses `pip list --not-required` (pip) or `uv pip tree --depth=0` (uv). These report - * packages that nothing else depends on, which is a proxy for "directly installed" but - * not equivalent — e.g., `pip install flask werkzeug` will report werkzeug as having - * dependents (flask) even though the user installed it explicitly. - */ -export async function refreshPipDirectPackageNames( - environment: PythonEnvironment, - log?: LogOutputChannel, -): Promise { - const useUv = await shouldUseUv(log, environment.environmentPath.fsPath); - if (useUv) { - const treeOutput = await runUV( - ['pip', 'tree', '--python', environment.execInfo.run.executable, '--depth=0'], - undefined, - log, - undefined, - PIP_LIST_TIMEOUT_MS, - ); - return parseUvTree(treeOutput); - } - const data = await execPipList(environment, log, ['--not-required']); - const packages = parsePipListJson(data); - return packages.map((pkg) => pkg.name); -} - -export async function managePackages( - environment: PythonEnvironment, - options: PackageManagementOptions, - manager: PackageManager, - token?: CancellationToken, -): Promise { - if (environment.version.startsWith('2.')) { - throw new Error('Python 2.* is not supported (deprecated)'); - } - - // Use environmentPath directly for consistency with UV environment tracking - const useUv = await shouldUseUv(manager.log, environment.environmentPath.fsPath); - const uninstallArgs = ['pip', 'uninstall']; - if (options.uninstall && options.uninstall.length > 0) { - if (useUv) { - await runUV( - [...uninstallArgs, '--python', environment.execInfo.run.executable, ...options.uninstall], - undefined, - manager.log, - token, - ); - } else { - uninstallArgs.push('--yes'); - await runPython( - environment.execInfo.run.executable, - ['-m', ...uninstallArgs, ...options.uninstall], - undefined, - manager.log, - token, - ); - } - } - - const installArgs = ['pip', 'install']; - if (options.upgrade) { - installArgs.push('--upgrade'); - } - if (options.install && options.install.length > 0) { - const processedInstallArgs = processEditableInstallArgs(options.install); - - if (useUv) { - await runUV( - [...installArgs, '--python', environment.execInfo.run.executable, ...processedInstallArgs], - undefined, - manager.log, - token, - ); - } else { - await runPython( - environment.execInfo.run.executable, - ['-m', ...installArgs, ...processedInstallArgs], - undefined, - manager.log, - token, - ); - } - } -} - /** * Process pip install arguments to correctly handle editable installs with extras * This function will combine consecutive -e arguments that represent the same package with extras diff --git a/src/managers/conda/commands/availableVersions.ts b/src/managers/conda/commands/availableVersions.ts new file mode 100644 index 00000000..0181c2db --- /dev/null +++ b/src/managers/conda/commands/availableVersions.ts @@ -0,0 +1,52 @@ +import { + AvailableVersionsCommand, + CommandConstructorOptions, + type AvailableVersionsExecuteArgs, +} from '../../base/commands/index'; +import { runCondaExecutable } from '../condaUtils'; + +/** + * Conda available versions command. + * + * Parsed Command: `conda search --json` + * + * Official Documentation: https://conda.io/projects/conda/en/latest/commands/search.html + * The `conda search` command searches for packages in the conda channels. + * The `--json` flag outputs results in JSON format for structured parsing. + * Returns all builds of all versions available; deduplication is performed in the command. + * NOTE: The pythonVersion parameter is ignored for conda (unlike pip) as conda doesn't filter by Python version. + */ +export class CondaAvailableVersionsCommand extends AvailableVersionsCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[] { + return ['search', executeArgs.packageName, '--json']; + } + + async execute(executeArgs: AvailableVersionsExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + const output = await runCondaExecutable(args, this.log, executeArgs.cancellationToken); + + try { + const parsed = JSON.parse(output); + if (parsed && typeof parsed === 'object' && Array.isArray(parsed[executeArgs.packageName])) { + const uniqueVersions = new Map(); + (parsed[executeArgs.packageName] as Array<{ version?: string }>) + .filter((entry) => !!entry.version?.trim()) + .forEach((entry) => { + const version = entry.version!.trim(); + if (!uniqueVersions.has(version)) { + uniqueVersions.set(version, version); + } + }); + + return Array.from(uniqueVersions.values()); + } + return []; + } catch { + return []; + } + } +} diff --git a/src/managers/conda/commands/index.ts b/src/managers/conda/commands/index.ts new file mode 100644 index 00000000..705ba8d0 --- /dev/null +++ b/src/managers/conda/commands/index.ts @@ -0,0 +1,5 @@ +export { CondaAvailableVersionsCommand } from './availableVersions'; +export { CondaInstallCommand } from './install'; +export { CondaListCommand } from './list'; +export { CondaUninstallCommand } from './uninstall'; +export { CondaVersionCommand } from './version'; diff --git a/src/managers/conda/commands/install.ts b/src/managers/conda/commands/install.ts new file mode 100644 index 00000000..4bc3e6cf --- /dev/null +++ b/src/managers/conda/commands/install.ts @@ -0,0 +1,53 @@ +import { CommandConstructorOptions, InstallCommand, type InstallExecuteArgs } from '../../base/commands/index'; +import { runCondaExecutable } from '../condaUtils'; + +/** + * Conda install command execute arguments (includes the target environment path). + */ +export interface CondaInstallExecuteArgs extends InstallExecuteArgs { + environmentPath: string; +} + +/** + * Conda install command. + * + * Parsed Command: `conda install -y -p ` + * Parsed Command (upgrade): `conda update -y -p ` + * + * Official Documentation: https://conda.io/projects/conda/en/latest/commands/install.html + * The `conda install` command installs packages in the specified conda environment. + * The `-y` flag automatically confirms the installation without prompting. + * The `-p` flag targets a specific environment by prefix path. + * Uses `conda update` when upgrade is requested. + */ +export class CondaInstallCommand extends InstallCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(executeArgs: InstallExecuteArgs): string[] { + const args = [executeArgs.upgrade ? 'update' : 'install', '-y']; + + const { environmentPath } = executeArgs as CondaInstallExecuteArgs; + if (environmentPath) { + args.push('-p', environmentPath); + } + + args.push( + ...executeArgs.packages.map((pkg) => { + if (pkg.version) { + return `${pkg.packageName}=${pkg.version}`; + } + return pkg.packageName; + }), + ); + + return args; + } + + async execute(executeArgs: InstallExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + + await runCondaExecutable(args, this.log, executeArgs.cancellationToken); + } +} diff --git a/src/managers/conda/commands/list.ts b/src/managers/conda/commands/list.ts new file mode 100644 index 00000000..2458eec5 --- /dev/null +++ b/src/managers/conda/commands/list.ts @@ -0,0 +1,60 @@ +import { PackageInfo } from '../../../api'; +import { CommandConstructorOptions, ListCommand, type BaseExecuteArgs } from '../../base/commands/index'; +import { runCondaExecutable } from '../condaUtils'; + +/** + * Conda list command execute arguments (includes environment path and cancellation token). + */ +export interface CondaListExecuteArgs extends BaseExecuteArgs { + environmentPath: string; +} + +/** + * Conda list command. + * + * Parsed Command: `conda list -p --json` + * + * Official Documentation: https://conda.io/projects/conda/en/latest/commands/list.html + * The `conda list` command shows all installed packages in a conda environment. + * The `-p` flag specifies the environment path (can be absolute or relative). + * The `--json` flag outputs the package list in JSON format for structured parsing. + */ +export class CondaListCommand extends ListCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(executeArgs: CondaListExecuteArgs): string[] { + return ['list', '-p', executeArgs.environmentPath, '--json']; + } + + async execute(executeArgs?: CondaListExecuteArgs): Promise { + if (!executeArgs?.environmentPath) { + return []; + } + + const cmdArgs = this.buildCommand(executeArgs); + const output = await runCondaExecutable(cmdArgs, this.log, executeArgs.cancellationToken); + + let condaPackages: { name: string; version: string }[]; + try { + condaPackages = JSON.parse(output) as { name: string; version: string }[]; + } catch { + return []; + } + + const packages: PackageInfo[] = []; + for (const condaPkg of condaPackages) { + if (condaPkg.name && condaPkg.version) { + packages.push({ + name: condaPkg.name, + displayName: condaPkg.name, + version: condaPkg.version, + description: condaPkg.version, + }); + } + } + + return packages; + } +} diff --git a/src/managers/conda/commands/uninstall.ts b/src/managers/conda/commands/uninstall.ts new file mode 100644 index 00000000..be2f32d7 --- /dev/null +++ b/src/managers/conda/commands/uninstall.ts @@ -0,0 +1,44 @@ +import { CommandConstructorOptions, UninstallCommand, type UninstallExecuteArgs } from '../../base/commands/index'; +import { runCondaExecutable } from '../condaUtils'; + +/** + * Conda uninstall command execute arguments (includes the target environment path). + */ +export interface CondaUninstallExecuteArgs extends UninstallExecuteArgs { + environmentPath: string; +} + +/** + * Conda uninstall command. + * + * Parsed Command: `conda remove -y -p ` + * + * Official Documentation: https://conda.io/projects/conda/en/latest/commands/remove.html + * The `conda remove` command (alias `conda uninstall`) removes packages from the specified environment. + * The `-y` flag automatically confirms the removal without prompting. + * The `-p` flag targets a specific environment by prefix path. + * Removes both the package and its unused dependencies by default. + */ +export class CondaUninstallCommand extends UninstallCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(executeArgs: UninstallExecuteArgs): string[] { + const args = ['remove', '-y']; + + const { environmentPath } = executeArgs as CondaUninstallExecuteArgs; + if (environmentPath) { + args.push('-p', environmentPath); + } + + args.push(...executeArgs.packages.map((pkg) => pkg.packageName)); + return args; + } + + async execute(executeArgs: UninstallExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + + await runCondaExecutable(args, this.log, executeArgs.cancellationToken); + } +} diff --git a/src/managers/conda/commands/version.ts b/src/managers/conda/commands/version.ts new file mode 100644 index 00000000..5b8fed0e --- /dev/null +++ b/src/managers/conda/commands/version.ts @@ -0,0 +1,32 @@ +import type { Pep440Version } from '@renovatebot/pep440'; +import { explain as parsePep440Version } from '@renovatebot/pep440'; +import { CommandConstructorOptions, VersionCommand, type BaseExecuteArgs } from '../../base/commands/index'; +import { runCondaExecutable } from '../condaUtils'; + +/** + * Conda version command. + * + * Parsed Command: `conda --version` + * + * Official Documentation: https://conda.io/projects/conda/en/latest/commands.html + * The `conda --version` command displays the current version of conda. + * Output format: "conda X.Y.Z" where X.Y.Z is the semantic version. + */ +export class CondaVersionCommand extends VersionCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(): string[] { + return ['--version']; + } + + async execute(executeArgs?: BaseExecuteArgs): Promise { + const args = this.buildCommand(); + const output = await runCondaExecutable(args, this.log, executeArgs?.cancellationToken); + + // "conda X.Y.Z" + const match = output.match(/conda\s+(\d+\.\d+(?:\.\d+)*)/i); + return match ? (parsePep440Version(match[1]) ?? undefined) : undefined; + } +} diff --git a/src/managers/conda/condaPackageManager.ts b/src/managers/conda/condaPackageManager.ts index 4105800b..c29317f7 100644 --- a/src/managers/conda/condaPackageManager.ts +++ b/src/managers/conda/condaPackageManager.ts @@ -1,5 +1,5 @@ import type { Pep440Version } from '@renovatebot/pep440'; -import { explain as parse, rcompare } from '@renovatebot/pep440'; +import { compare, explain as parse } from '@renovatebot/pep440'; import * as path from 'path'; import { CancellationError, @@ -23,10 +23,18 @@ import { } from '../../api'; import { showErrorMessageWithLogs } from '../../common/errors/utils'; import { CondaStrings } from '../../common/localize'; -import { traceError } from '../../common/logging'; import { withProgress } from '../../common/window.apis'; +import { CommandConstructorOptions } from '../base/commands/index'; +import { parsePackageSpecs } from '../builtin/utils'; import { updatePackagesAndNotify } from '../common/packageChanges'; -import { getCommonCondaPackagesToInstall, managePackages, runCondaExecutable } from './condaUtils'; +import { + CondaAvailableVersionsCommand, + CondaInstallCommand, + CondaListCommand, + CondaUninstallCommand, + CondaVersionCommand, +} from './commands/index'; +import { getCommonCondaPackagesToInstall } from './condaUtils'; export class CondaPackageManager implements PackageManager, Disposable { private readonly _onDidChangePackages = new EventEmitter(); @@ -63,40 +71,50 @@ export class CondaPackageManager implements PackageManager, Disposable { } } - const manageOptions = { - ...options, - install: toInstall, - uninstall: toUninstall, - }; - await withProgress( - { - location: ProgressLocation.Notification, - title: CondaStrings.condaInstallingPackages, - cancellable: true, - }, - async (_progress, token) => { - try { - await managePackages(environment, manageOptions, token, this.log); - await updatePackagesAndNotify( - this, - environment, - this.packages.get(environment.envId.id), - (changes) => { - this._onDidChangePackages.fire({ environment, manager: this, changes }); - }, - ); - } catch (e) { - if (e instanceof CancellationError) { - throw e; - } - - this.log.error('Error installing packages', e); - setImmediate(async () => { - await showErrorMessageWithLogs(CondaStrings.condaInstallError, this.log); - }); - } - }, - ); + try { + // Centralize command options for install/uninstall operations + const manageCommandOptions: CommandConstructorOptions = { + pythonExecutable: 'conda', + log: this.log, + }; + + // Execute uninstall if needed + if (toUninstall.length > 0) { + const uninstallCmd = new CondaUninstallCommand(manageCommandOptions); + const packages = parsePackageSpecs(toUninstall); + await uninstallCmd.executeWithProgress( + { packages, environmentPath: environment.environmentPath.fsPath, showProgress: true }, + CondaStrings.condaInstallingPackages, + ); + } + + // Execute install if needed + if (toInstall.length > 0) { + const installCmd = new CondaInstallCommand(manageCommandOptions); + const packages = parsePackageSpecs(toInstall); + await installCmd.executeWithProgress( + { + packages, + upgrade: options.upgrade, + environmentPath: environment.environmentPath.fsPath, + showProgress: true, + }, + CondaStrings.condaInstallingPackages, + ); + } + + await updatePackagesAndNotify(this, environment, this.packages.get(environment.envId.id), (changes) => { + this._onDidChangePackages.fire({ environment, manager: this, changes }); + }); + } catch (e) { + if (e instanceof CancellationError) { + throw e; + } + this.log.error('Error installing packages', e); + setImmediate(async () => { + await showErrorMessageWithLogs(CondaStrings.condaInstallError, this.log); + }); + } } async refresh(environment: PythonEnvironment): Promise { @@ -120,34 +138,12 @@ export class CondaPackageManager implements PackageManager, Disposable { async getPackages(environment: PythonEnvironment, options?: GetPackagesOptions): Promise { if (options?.skipCache || !this.packages.has(environment.envId.id)) { - const args = ['list', '-p', environment.environmentPath.fsPath, '--json']; - const data = await runCondaExecutable(args); - - let condaPackages: { name: string; version: string }[]; - try { - condaPackages = JSON.parse(data) as { name: string; version: string }[]; - } catch (e) { - traceError(`Failed to parse conda list JSON output: ${data}`, e); - return []; - } - - const packages: Package[] = []; - for (const condaPkg of condaPackages) { - if (condaPkg.name && condaPkg.version) { - packages.push( - this.api.createPackageItem( - { - name: condaPkg.name, - displayName: condaPkg.name, - version: condaPkg.version, - description: condaPkg.version, - }, - environment, - this, - ), - ); - } - } + const listCmd = new CondaListCommand({ + pythonExecutable: 'conda', + log: this.log, + }); + const data = await listCmd.execute({ environmentPath: environment.environmentPath.fsPath }); + const packages = (data ?? []).map((pkg) => this.api.createPackageItem(pkg, environment, this)); this.packages.set(environment.envId.id, packages); return packages; } @@ -161,10 +157,11 @@ export class CondaPackageManager implements PackageManager, Disposable { async getVersion(_environment: PythonEnvironment): Promise { try { - const output = await runCondaExecutable(['--version'], this.log); - // "conda X.Y.Z" - const match = output.match(/conda\s+(\d+\.\d+(?:\.\d+)*)/i); - return match ? (parse(match[1]) ?? undefined) : undefined; + const versionCmd = new CondaVersionCommand({ + pythonExecutable: 'conda', + log: this.log, + }); + return await versionCmd.execute(); } catch { return undefined; } @@ -175,25 +172,15 @@ export class CondaPackageManager implements PackageManager, Disposable { packageName: string, ): Promise { try { - const output = await runCondaExecutable(['search', packageName, '--json'], this.log); - const parsed = JSON.parse(output); - if (parsed && typeof parsed === 'object' && Array.isArray(parsed[packageName])) { - const uniqueVersions = new Map(); - parsed[packageName] - .filter((entry: { version?: string }) => !!entry.version?.trim()) - .map((entry: { version?: string }) => parse(entry.version!)) - .filter((v: Pep440Version | null): v is Pep440Version => v !== null) - .forEach((version: Pep440Version) => { - if (!uniqueVersions.has(version.public)) { - uniqueVersions.set(version.public, version); - } - }); - - return Array.from(uniqueVersions.values()).sort((a: Pep440Version, b: Pep440Version) => - rcompare(a.public, b.public), - ); - } - return undefined; + const availableVersionsCmd = new CondaAvailableVersionsCommand({ + pythonExecutable: 'conda', + log: this.log, + }); + const versionStrings = await availableVersionsCmd.execute({ packageName, pythonVersion: '' }); + return versionStrings + .map((v) => parse(v)) + .filter((parsed): parsed is Pep440Version => parsed !== null) + .sort((a, b) => compare(a.public, b.public)); } catch { return undefined; } diff --git a/src/managers/conda/condaUtils.ts b/src/managers/conda/condaUtils.ts index 06d3ec54..eee067ce 100644 --- a/src/managers/conda/condaUtils.ts +++ b/src/managers/conda/condaUtils.ts @@ -1250,29 +1250,6 @@ export async function deleteCondaEnvironment(environment: PythonEnvironment, log ); } -export async function managePackages( - environment: PythonEnvironment, - options: PackageManagementOptions, - token: CancellationToken, - log: LogOutputChannel, -): Promise { - if (options.uninstall && options.uninstall.length > 0) { - await runCondaExecutable( - ['remove', '--prefix', environment.environmentPath.fsPath, '--yes', ...options.uninstall], - log, - token, - ); - } - if (options.install && options.install.length > 0) { - const args = ['install', '--prefix', environment.environmentPath.fsPath, '--yes']; - if (options.upgrade) { - args.push('--update-all'); - } - args.push(...options.install); - await runCondaExecutable(args, log, token); - } -} - async function getCommonPackages(): Promise { try { const pipData = path.join(EXTENSION_ROOT_DIR, 'files', 'conda_packages.json'); diff --git a/src/managers/poetry/commands/add.ts b/src/managers/poetry/commands/add.ts new file mode 100644 index 00000000..999a97f4 --- /dev/null +++ b/src/managers/poetry/commands/add.ts @@ -0,0 +1,41 @@ +import { CommandConstructorOptions, InstallCommand, type InstallExecuteArgs } from '../../base/commands/index'; +import { runPoetry } from '../poetryUtils'; + +/** + * Poetry add command. + * + * Parsed Command: `poetry add [--allow-prereleases] [ ...]` + * + * Official Documentation: https://python-poetry.org/docs/cli/#add + * The `poetry add` command adds required packages to your pyproject.toml and installs them. + * It's the primary way to add dependencies to a Poetry project. + */ +export class PoetryAddCommand extends InstallCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(executeArgs: InstallExecuteArgs): string[] { + const args = ['add']; + + if (executeArgs.upgrade) { + args.push('--allow-prereleases'); + } + + args.push( + ...executeArgs.packages.map((pkg) => { + if (pkg.version) { + return `${pkg.packageName}@${pkg.version}`; + } + return pkg.packageName; + }), + ); + + return args; + } + + async execute(executeArgs: InstallExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + await runPoetry(args, undefined, this.log, executeArgs.cancellationToken); + } +} diff --git a/src/managers/poetry/commands/index.ts b/src/managers/poetry/commands/index.ts new file mode 100644 index 00000000..19a89134 --- /dev/null +++ b/src/managers/poetry/commands/index.ts @@ -0,0 +1,5 @@ +export { PoetryAddCommand } from './add'; +export { PoetryRemoveCommand } from './remove'; +export { PoetryShowCommand } from './show'; +export { PoetryShowTopLevelCommand } from './showTopLevel'; +export { PoetryVersionCommand } from './version'; diff --git a/src/managers/poetry/commands/remove.ts b/src/managers/poetry/commands/remove.ts new file mode 100644 index 00000000..b85ea757 --- /dev/null +++ b/src/managers/poetry/commands/remove.ts @@ -0,0 +1,26 @@ +import { CommandConstructorOptions, UninstallCommand, type UninstallExecuteArgs } from '../../base/commands/index'; +import { runPoetry } from '../poetryUtils'; + +/** + * Poetry remove command. + * + * Parsed Command: `poetry remove [ ...]` + * + * Official Documentation: https://python-poetry.org/docs/cli/#remove + * The `poetry remove` command removes packages from your pyproject.toml and uninstalls them + * from your virtual environment. It removes both the dependency declaration and the installed package. + */ +export class PoetryRemoveCommand extends UninstallCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(executeArgs: UninstallExecuteArgs): string[] { + return ['remove', ...executeArgs.packages.map((pkg) => pkg.packageName)]; + } + + async execute(executeArgs: UninstallExecuteArgs): Promise { + const args = this.buildCommand(executeArgs); + await runPoetry(args, undefined, this.log, executeArgs.cancellationToken); + } +} diff --git a/src/managers/poetry/commands/show.ts b/src/managers/poetry/commands/show.ts new file mode 100644 index 00000000..98c902d5 --- /dev/null +++ b/src/managers/poetry/commands/show.ts @@ -0,0 +1,57 @@ +import { PackageInfo } from '../../../api'; +import { CommandConstructorOptions, ListCommand, type BaseExecuteArgs } from '../../base/commands/index'; +import { runPoetry } from '../poetryUtils'; + +export interface PoetryShowExecuteArgs extends BaseExecuteArgs { + cwd?: string; +} + +/** + * Poetry show command. + * + * Parsed Command: `poetry show --no-ansi` + * + * Official Documentation: https://python-poetry.org/docs/cli/#show + * The `poetry show` command displays information about the installed packages. + * The `--no-ansi` flag disables ANSI color output for easier parsing. + */ +export class PoetryShowCommand extends ListCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(): string[] { + return ['show', '--no-ansi']; + } + + async execute(executeArgs?: PoetryShowExecuteArgs): Promise { + const args = this.buildCommand(); + const output = await runPoetry(args, executeArgs?.cwd, this.log, executeArgs?.cancellationToken); + + const packages: PackageInfo[] = []; + + try { + // Parse poetry show output + // Format: name version description + const lines = output.split('\n'); + for (const line of lines) { + // Updated regex to properly handle lines with the format: + // "package (!) version description" + const match = line.match(/^(\S+)(?:\s+\([!]\))?\s+(\S+)\s+(.*)/); + if (match) { + const [, name, version, description] = match; + packages.push({ + name, + displayName: name, + version, + description: `${version} - ${description?.trim() || ''}`, + }); + } + } + } catch { + return []; + } + + return packages; + } +} diff --git a/src/managers/poetry/commands/showTopLevel.ts b/src/managers/poetry/commands/showTopLevel.ts new file mode 100644 index 00000000..7767b3ba --- /dev/null +++ b/src/managers/poetry/commands/showTopLevel.ts @@ -0,0 +1,42 @@ +import { CommandConstructorOptions, ListDirectNamesCommand, type BaseExecuteArgs } from '../../base/commands/index'; +import { runPoetry } from '../poetryUtils'; + +export interface PoetryShowTopLevelExecuteArgs extends BaseExecuteArgs { + cwd?: string; +} + +/** + * Poetry show --top-level command. + * + * Parsed Command: `poetry show --no-ansi --top-level` + * + * Official Documentation: https://python-poetry.org/docs/cli/#show + * The `poetry show` command with `--top-level` flag displays only the top-level (directly installed) + * packages. The `--no-ansi` flag disables ANSI color output for easier parsing. + * This is useful for determining which packages were explicitly installed vs. dependencies. + */ +export class PoetryShowTopLevelCommand extends ListDirectNamesCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(): string[] { + return ['show', '--no-ansi', '--top-level']; + } + + async execute(executeArgs?: PoetryShowTopLevelExecuteArgs): Promise { + const args = this.buildCommand(); + const output = await runPoetry(args, executeArgs?.cwd, this.log, executeArgs?.cancellationToken); + + try { + const names = output + .split('\n') + .map((line) => line.trim()) + .map((line) => line.match(/^([a-zA-Z0-9._-]+)/)?.[1] ?? '') + .filter((name) => !!name); + return names; + } catch { + return []; + } + } +} diff --git a/src/managers/poetry/commands/version.ts b/src/managers/poetry/commands/version.ts new file mode 100644 index 00000000..bad77b94 --- /dev/null +++ b/src/managers/poetry/commands/version.ts @@ -0,0 +1,34 @@ +import type { Pep440Version } from '@renovatebot/pep440'; +import { explain as parsePep440Version } from '@renovatebot/pep440'; +import { CommandConstructorOptions, VersionCommand, type BaseExecuteArgs } from '../../base/commands/index'; +import { getPoetryVersion } from '../poetryUtils'; + +/** + * Poetry version command. + * + * Parsed Command: `poetry --version` + * + * Official Documentation: https://python-poetry.org/docs/cli/#options + * The `--version` option displays the current version of Poetry. + * Returns output in format: "Poetry (version X.Y.Z)" which is parsed to extract the version string. + */ +export class PoetryVersionCommand extends VersionCommand { + constructor(options: CommandConstructorOptions) { + super(options); + } + + protected buildCommand(): string[] { + return ['--version']; + } + + async execute(_executeArgs?: BaseExecuteArgs): Promise { + try { + // Poetry version is obtained via getPoetryVersion utility which handles poetry --version + // We pass the pythonExecutable as the poetry path since it was set to the poetry executable + const versionString = await getPoetryVersion(this.pythonExecutable); + return versionString ? (parsePep440Version(versionString) ?? undefined) : undefined; + } catch { + return undefined; + } + } +} diff --git a/src/managers/poetry/poetryPackageManager.ts b/src/managers/poetry/poetryPackageManager.ts index 54845a20..7d3eedd2 100644 --- a/src/managers/poetry/poetryPackageManager.ts +++ b/src/managers/poetry/poetryPackageManager.ts @@ -1,10 +1,8 @@ import type { Pep440Version } from '@renovatebot/pep440'; -import { explain as parse } from '@renovatebot/pep440'; import * as fsapi from 'fs-extra'; import * as path from 'path'; import { CancellationError, - CancellationToken, Event, EventEmitter, l10n, @@ -24,12 +22,18 @@ import { PythonEnvironment, PythonEnvironmentApi, } from '../../api'; -import { spawnProcess } from '../../common/childProcess.apis'; import { showErrorMessage, showInputBox, withProgress } from '../../common/window.apis'; -import { normalizePackageName } from '../builtin/utils'; +import { normalizePackageName, parsePackageSpecs } from '../builtin/utils'; import { updatePackagesAndNotify } from '../common/packageChanges'; +import { + PoetryAddCommand, + PoetryRemoveCommand, + PoetryShowCommand, + PoetryShowTopLevelCommand, + PoetryVersionCommand, +} from './commands/index'; import { PoetryManager } from './poetryManager'; -import { getPoetry, getPoetryVersion } from './poetryUtils'; +import { getPoetry } from './poetryUtils'; export class PoetryPackageManager implements PackageManager, Disposable { private readonly _onDidChangePackages = new EventEmitter(); @@ -77,38 +81,25 @@ export class PoetryPackageManager implements PackageManager, Disposable { } } - await withProgress( - { - location: ProgressLocation.Notification, - title: 'Managing packages with Poetry', - cancellable: true, - }, - async (_progress, token) => { - try { - await this.runPoetryManage({ install: toInstall, uninstall: toUninstall }, token); - await updatePackagesAndNotify( - this, - environment, - this.packages.get(environment.envId.id), - (changes) => { - this._onDidChangePackages.fire({ environment, manager: this, changes }); - }, - ); - } catch (e) { - if (e instanceof CancellationError) { - throw e; - } - this.log.error('Error managing packages with Poetry', e); - setImmediate(async () => { - const result = await showErrorMessage('Error managing packages with Poetry', 'View Output'); - if (result === 'View Output') { - this.log.show(); - } - }); - throw e; + try { + await this.runPoetryManage({ install: toInstall, uninstall: toUninstall }); + await updatePackagesAndNotify(this, environment, this.packages.get(environment.envId.id), (changes) => { + this._onDidChangePackages.fire({ environment, manager: this, changes }); + }); + } catch (e) { + if (e instanceof CancellationError) { + // Cancellation is not an error; rethrow without surfacing an error message. + throw e; + } + this.log.error('Error managing packages with Poetry', e); + setImmediate(async () => { + const result = await showErrorMessage('Error managing packages with Poetry', 'View Output'); + if (result === 'View Output') { + this.log.show(); } - }, - ); + }); + throw e; + } } async refresh(environment: PythonEnvironment): Promise { @@ -156,8 +147,11 @@ export class PoetryPackageManager implements PackageManager, Disposable { if (!poetry) { return undefined; } - const versionStr = await getPoetryVersion(poetry); - return versionStr ? (parse(versionStr) ?? undefined) : undefined; + const versionCmd = new PoetryVersionCommand({ + pythonExecutable: poetry, + log: this.log, + }); + return await versionCmd.execute(); } async getPackageAvailableVersions( @@ -180,10 +174,7 @@ export class PoetryPackageManager implements PackageManager, Disposable { this.packages.clear(); } - private async runPoetryManage( - options: { install?: string[]; uninstall?: string[] }, - token?: CancellationToken, - ): Promise { + private async runPoetryManage(options: { install?: string[]; uninstall?: string[] }): Promise { const poetry = await getPoetry(); if (!poetry) { throw new Error( @@ -195,28 +186,22 @@ export class PoetryPackageManager implements PackageManager, Disposable { // Handle uninstalls first if (options.uninstall && options.uninstall.length > 0) { - try { - const args = ['remove', ...options.uninstall]; - this.log.info(`Running: poetry ${args.join(' ')}`); - const result = await runPoetry(args, undefined, this.log, token); - this.log.info(result); - } catch (err) { - this.log.error(`Error removing packages with Poetry: ${err}`); - throw err; - } + const removeCmd = new PoetryRemoveCommand({ + pythonExecutable: poetry, + log: this.log, + }); + const packages = parsePackageSpecs(options.uninstall); + await removeCmd.executeWithProgress({ packages, showProgress: true }, 'Managing packages with Poetry'); } // Handle installs if (options.install && options.install.length > 0) { - try { - const args = ['add', ...options.install]; - this.log.info(`Running: poetry ${args.join(' ')}`); - const result = await runPoetry(args, undefined, this.log, token); - this.log.info(result); - } catch (err) { - this.log.error(`Error adding packages with Poetry: ${err}`); - throw err; - } + const addCmd = new PoetryAddCommand({ + pythonExecutable: poetry, + log: this.log, + }); + const packages = parsePackageSpecs(options.install); + await addCmd.executeWithProgress({ packages, showProgress: true }, 'Managing packages with Poetry'); } } @@ -230,126 +215,73 @@ export class PoetryPackageManager implements PackageManager, Disposable { ); } - let cwd = process.cwd(); - const projects = this.api.getPythonProjects(); - if (projects.length === 1) { - const stat = await fsapi.stat(projects[0].uri.fsPath); - if (stat.isDirectory()) { - cwd = projects[0].uri.fsPath; - } else { - cwd = path.dirname(projects[0].uri.fsPath); - } - } else if (projects.length > 1) { - const dirs = new Set(); - await Promise.all( - projects.map(async (project) => { - const e = await this.api.getEnvironment(project.uri); - if (e?.envId.id === environment.envId.id) { - const stat = await fsapi.stat(projects[0].uri.fsPath); - const dir = stat.isDirectory() ? projects[0].uri.fsPath : path.dirname(projects[0].uri.fsPath); - if (dirs.has(dir)) { - dirs.add(dir); - } - } - }), - ); - if (dirs.size > 0) { - // ensure we have the deepest directory node picked - cwd = Array.from(dirs.values()).sort((a, b) => (a.length - b.length) * -1)[0]; - } - } - - const poetryPackages: { name: string; version: string; displayName: string; description: string }[] = []; + const showCmd = new PoetryShowCommand({ + pythonExecutable: poetry, + log: this.log, + }); + const cwd = await this.getPoetryCwdForEnvironment(environment); + const data = await showCmd.execute({ cwd }); + return (data ?? []).map((pkg) => this.api.createPackageItem(pkg, environment, this)); + } + async getDirectPackageNames(environment: PythonEnvironment): Promise | undefined> { try { - this.log.info(`Running: ${await getPoetry()} show --no-ansi`); - const result = await runPoetry(['show', '--no-ansi'], cwd, this.log); - - // Parse poetry show output - // Format: name version description - const lines = result.split('\n'); - for (const line of lines) { - // Updated regex to properly handle lines with the format: - // "package (!) version description" - const match = line.match(/^(\S+)(?:\s+\([!]\))?\s+(\S+)\s+(.*)/); - if (match) { - const [, name, version, description] = match; - poetryPackages.push({ - name, - version, - displayName: name, - description: `${version} - ${description?.trim() || ''}`, - }); - } + const poetry = await getPoetry(); + if (!poetry) { + return undefined; } + const showTopLevelCmd = new PoetryShowTopLevelCommand({ + pythonExecutable: poetry, + log: this.log, + }); + const cwd = await this.getPoetryCwdForEnvironment(environment); + const names = await showTopLevelCmd.execute({ cwd }); + return names ? new Set(names.map(normalizePackageName)) : undefined; } catch (err) { - this.log.error(`Error refreshing packages with Poetry: ${err}`); - // Return empty array instead of throwing to avoid breaking the UI - return []; + this.log.error(`Error fetching direct package names with Poetry: ${err}`); + return undefined; } - - // Convert to Package objects using the API - return poetryPackages.map((pkg) => this.api.createPackageItem(pkg, environment, this)); } - async getDirectPackageNames(_environment: PythonEnvironment): Promise | undefined> { - try { - const topLevelResult = await runPoetry(['show', '--no-ansi', '--top-level'], undefined, this.log); - const names = topLevelResult - .split('\n') - .map((line) => line.trim()) - .map((line) => line.match(/^([a-zA-Z0-9._-]+)/)?.[1] ?? '') - .filter((name) => !!name) - .map(normalizePackageName); - return new Set(names); - } catch (err) { - this.log.error(`Error fetching direct package names with Poetry: ${err}`); + /** + * Compute the best working directory for poetry commands for a given environment. + * Poetry command behavior depends on running inside the project containing pyproject.toml. + */ + private async getPoetryCwdForEnvironment(environment: PythonEnvironment): Promise { + const projects = this.api.getPythonProjects(); + if (projects.length === 0) { return undefined; } - } -} -export async function runPoetry( - args: string[], - cwd?: string, - log?: LogOutputChannel, - token?: CancellationToken, -): Promise { - const poetry = await getPoetry(); - if (!poetry) { - throw new Error('Poetry executable not found'); - } + const toDirectory = async (fsPath: string): Promise => { + try { + const stat = await fsapi.stat(fsPath); + return stat.isDirectory() ? fsPath : path.dirname(fsPath); + } catch { + return path.dirname(fsPath); + } + }; - log?.info(`Running: ${poetry} ${args.join(' ')}`); + if (projects.length === 1) { + return toDirectory(projects[0].uri.fsPath); + } - return new Promise((resolve, reject) => { - const proc = spawnProcess(poetry, args, { cwd }); - token?.onCancellationRequested(() => { - proc.kill(); - reject(new CancellationError()); - }); - let builder = ''; - proc.stdout?.on('data', (data) => { - const s = data.toString('utf-8'); - builder += s; - log?.append(`poetry: ${s}`); - }); - proc.stderr?.on('data', (data) => { - const s = data.toString('utf-8'); - builder += s; - log?.append(`poetry: ${s}`); - }); - proc.on('close', () => { - resolve(builder); - }); - proc.on('error', (error) => { - log?.error(`Error executing poetry command: ${error}`); - reject(error); - }); - proc.on('exit', (code) => { - if (code !== 0) { - reject(new Error(`Failed to run poetry ${args.join(' ')}`)); - } - }); - }); + const dirs = new Set(); + await Promise.all( + projects.map(async (project) => { + const projectEnv = await this.api.getEnvironment(project.uri); + if (projectEnv?.envId.id === environment.envId.id) { + const dir = await toDirectory(project.uri.fsPath); + dirs.add(dir); + } + }), + ); + + if (dirs.size > 0) { + // Prefer deepest matching project to handle nested workspace layouts. + return Array.from(dirs).sort((a, b) => b.length - a.length)[0]; + } + + return toDirectory(projects[0].uri.fsPath); + } } diff --git a/src/managers/poetry/poetryUtils.ts b/src/managers/poetry/poetryUtils.ts index 9d14e1cf..f6620893 100644 --- a/src/managers/poetry/poetryUtils.ts +++ b/src/managers/poetry/poetryUtils.ts @@ -1,9 +1,9 @@ import * as fs from 'fs-extra'; import * as path from 'path'; -import { Uri } from 'vscode'; +import { CancellationError, CancellationToken, LogOutputChannel, Uri } from 'vscode'; import which from 'which'; import { EnvironmentManager, PythonEnvironment, PythonEnvironmentApi, PythonEnvironmentInfo } from '../../api'; -import { execProcess } from '../../common/childProcess.apis'; +import { execProcess, spawnProcess } from '../../common/childProcess.apis'; import { ENVS_EXTENSION_ID } from '../../common/constants'; import { traceError, traceInfo } from '../../common/logging'; import { getWorkspacePersistentState } from '../../common/persistentState'; @@ -466,3 +466,46 @@ export async function resolvePoetryPath( return undefined; } } +export async function runPoetry( + args: string[], + cwd?: string, + log?: LogOutputChannel, + token?: CancellationToken, +): Promise { + const poetry = await getPoetry(); + if (!poetry) { + throw new Error('Poetry executable not found'); + } + + log?.info(`Running: ${poetry} ${args.join(' ')}`); + + return new Promise((resolve, reject) => { + const proc = spawnProcess(poetry, args, { cwd }); + token?.onCancellationRequested(() => { + proc.kill(); + reject(new CancellationError()); + }); + let builder = ''; + proc.stdout?.on('data', (data) => { + const s = data.toString('utf-8'); + builder += s; + log?.append(`poetry: ${s}`); + }); + proc.stderr?.on('data', (data) => { + const s = data.toString('utf-8'); + builder += s; + log?.append(`poetry: ${s}`); + }); + proc.on('close', (code) => { + if (code !== 0) { + reject(new Error(`Failed to run poetry ${args.join(' ')}`)); + return; + } + resolve(builder); + }); + proc.on('error', (error) => { + log?.error(`Error executing poetry command: ${error}`); + reject(error); + }); + }); +} diff --git a/src/test/managers/builtin/availableVersions.unit.test.ts b/src/test/managers/builtin/availableVersions.unit.test.ts new file mode 100644 index 00000000..3407a262 --- /dev/null +++ b/src/test/managers/builtin/availableVersions.unit.test.ts @@ -0,0 +1,89 @@ +import assert from 'assert'; +import * as sinon from 'sinon'; +import { LogOutputChannel } from 'vscode'; +import * as helpers from '../../../managers/builtin/helpers'; +import * as workspaceApis from '../../../common/workspace.apis'; +import { PipAvailableVersionsCommand } from '../../../managers/builtin/commands/availableVersions'; +import { createMockLogOutputChannel } from '../../mocks/helper'; + +suite('PipAvailableVersionsCommand - parsing', () => { + let mockLog: LogOutputChannel; + let runPythonStub: sinon.SinonStub; + + function createCommand(): PipAvailableVersionsCommand { + return new PipAvailableVersionsCommand({ pythonExecutable: 'python', log: mockLog }); + } + + setup(() => { + mockLog = createMockLogOutputChannel(); + // Avoid touching real VS Code configuration during construction. + sinon.stub(workspaceApis, 'getConfiguration').returns({ + get: () => undefined, + } as unknown as ReturnType); + runPythonStub = sinon.stub(helpers, 'runPython'); + }); + + teardown(() => { + sinon.restore(); + }); + + test('parses valid JSON with a versions array', async () => { + runPythonStub.resolves(JSON.stringify({ package: 'requests', versions: ['2.31.0', '2.30.0', '2.29.0'] })); + + const result = await createCommand().execute({ packageName: 'requests', pythonVersion: '3.11.0' }); + + assert.deepStrictEqual(result, ['2.31.0', '2.30.0', '2.29.0']); + }); + + test('filters prerelease versions by default', async () => { + runPythonStub.resolves(JSON.stringify({ versions: ['2.0.0', '2.1.0b1', '2.1.0rc1', '2.1.0.dev0', '1.9.0'] })); + + const result = await createCommand().execute({ packageName: 'pkg', pythonVersion: '3.11.0' }); + + assert.deepStrictEqual(result, ['2.0.0', '1.9.0']); + }); + + test('includes prerelease versions when requested', async () => { + runPythonStub.resolves(JSON.stringify({ versions: ['2.0.0', '2.1.0b1'] })); + + const result = await createCommand().execute({ + packageName: 'pkg', + pythonVersion: '3.11.0', + includePrerelease: true, + }); + + assert.deepStrictEqual(result, ['2.0.0', '2.1.0b1']); + }); + + test('extracts JSON embedded in surrounding text', async () => { + runPythonStub.resolves('WARNING: pip banner\n' + JSON.stringify({ versions: ['1.0.0'] }) + '\n'); + + const result = await createCommand().execute({ packageName: 'pkg', pythonVersion: '3.11.0' }); + + assert.deepStrictEqual(result, ['1.0.0']); + }); + + test('returns empty array for invalid JSON', async () => { + runPythonStub.resolves('not json at all'); + + const result = await createCommand().execute({ packageName: 'pkg', pythonVersion: '3.11.0' }); + + assert.deepStrictEqual(result, []); + }); + + test('returns empty array for an empty versions array', async () => { + runPythonStub.resolves(JSON.stringify({ versions: [] })); + + const result = await createCommand().execute({ packageName: 'pkg', pythonVersion: '3.11.0' }); + + assert.deepStrictEqual(result, []); + }); + + test('returns empty array when versions field is missing', async () => { + runPythonStub.resolves(JSON.stringify({ package: 'pkg' })); + + const result = await createCommand().execute({ packageName: 'pkg', pythonVersion: '3.11.0' }); + + assert.deepStrictEqual(result, []); + }); +}); diff --git a/src/test/managers/builtin/pipListUtils.unit.test.ts b/src/test/managers/builtin/pipListUtils.unit.test.ts deleted file mode 100644 index 0bc978e9..00000000 --- a/src/test/managers/builtin/pipListUtils.unit.test.ts +++ /dev/null @@ -1,123 +0,0 @@ -import assert from 'assert'; -import * as fs from 'fs-extra'; -import * as path from 'path'; -import * as sinon from 'sinon'; -import { LogOutputChannel } from 'vscode'; -import { parsePipListJson, parseUvTree } from '../../../managers/builtin/pipListUtils'; -import { EXTENSION_TEST_ROOT } from '../../constants'; - -const TEST_DATA_ROOT = path.join(EXTENSION_TEST_ROOT, 'managers', 'builtin'); - -suite('Pip List JSON Parser tests', () => { - let log: LogOutputChannel; - - setup(() => { - log = { - error: sinon.stub(), - warn: sinon.stub(), - info: sinon.stub(), - } as unknown as LogOutputChannel; - }); - - teardown(() => { - sinon.restore(); - }); - - const testNames = ['piplist1', 'piplist2', 'piplist3']; - - testNames.forEach((testName) => { - test(`Test parsing pip list JSON output ${testName}`, async () => { - const expected = JSON.parse( - await fs.readFile(path.join(TEST_DATA_ROOT, `${testName}.expected.json`), 'utf8'), - ); - const pipListOutput = JSON.stringify(expected.packages); - - const actualPackages = parsePipListJson(pipListOutput, log); - - assert.equal(actualPackages.length, expected.packages.length, 'Unexpected number of packages'); - actualPackages.forEach((actualPackage) => { - const expectedPackage = expected.packages.find( - (item: { name: string }) => item.name === actualPackage.name, - ); - assert.ok(expectedPackage, `Package ${actualPackage.name} not found in expected packages`); - assert.equal(actualPackage.version, expectedPackage.version, 'Version mismatch'); - }); - - expected.packages.forEach((expectedPackage: { name: string; version: string }) => { - const actualPackage = actualPackages.find((item) => item.name === expectedPackage.name); - assert.ok(actualPackage, `Package ${expectedPackage.name} not found in actual packages`); - assert.equal(actualPackage.version, expectedPackage.version, 'Version mismatch'); - }); - }); - }); - - test('Returns an empty array for invalid JSON input', () => { - assert.deepStrictEqual(parsePipListJson('not json', log), []); - }); - - test('Logs error when JSON parsing fails', () => { - parsePipListJson('not valid json', log); - assert.ok((log.error as sinon.SinonStub).calledOnce, 'Expected error to be logged'); - }); - - test('Returns empty array without logging when no log is provided', () => { - const result = parsePipListJson('not valid json'); - assert.deepStrictEqual(result, []); - }); - - test('Skips items without a name or version', () => { - const actualPackages = parsePipListJson( - JSON.stringify([{ name: 'pip', version: '24.0' }, { name: 'setuptools' }, { version: '1.0.0' }]), - log, - ); - - assert.deepStrictEqual(actualPackages, [ - { - name: 'pip', - version: '24.0', - displayName: 'pip', - description: '24.0', - }, - ]); - }); - - test('Returns empty array for non-array JSON', () => { - const result = parsePipListJson('{"name": "pip"}', log); - assert.deepStrictEqual(result, []); - }); - - test('Returns empty array for empty array JSON', () => { - const result = parsePipListJson('[]', log); - assert.deepStrictEqual(result, []); - }); -}); - -suite('parseUvTree tests', () => { - test('Parses uv pip tree output with depth 0', () => { - const input = 'requests v2.31.0\nflask v3.0.0\n'; - const result = parseUvTree(input); - assert.deepStrictEqual(result, ['requests', 'flask']); - }); - - test('Handles empty output', () => { - assert.deepStrictEqual(parseUvTree(''), []); - }); - - test('Filters blank lines', () => { - const input = 'requests v2.31.0\n\n\nflask v3.0.0\n'; - const result = parseUvTree(input); - assert.deepStrictEqual(result, ['requests', 'flask']); - }); - - test('Handles single package', () => { - const input = 'pip v24.0\n'; - const result = parseUvTree(input); - assert.deepStrictEqual(result, ['pip']); - }); - - test('Trims leading whitespace from indented lines', () => { - const input = ' requests v2.31.0\n flask v3.0.0\n'; - const result = parseUvTree(input); - assert.deepStrictEqual(result, ['requests', 'flask']); - }); -}); diff --git a/src/test/managers/builtin/pipVersions.unit.test.ts b/src/test/managers/builtin/pipVersions.unit.test.ts deleted file mode 100644 index 5c06c394..00000000 --- a/src/test/managers/builtin/pipVersions.unit.test.ts +++ /dev/null @@ -1,37 +0,0 @@ -import assert from 'assert'; -import { explain } from '@renovatebot/pep440'; -import { parsePipIndexVersionsJson } from '../../../managers/builtin/pipPackageManager'; - -suite('Pip Version Parsing', () => { - suite('parsePipIndexVersionsJson', () => { - test('parses valid JSON with versions array', () => { - const output = JSON.stringify({ name: 'requests', versions: ['2.31.0', '2.30.0', '2.29.0'] }); - const versions = parsePipIndexVersionsJson(output); - assert.deepStrictEqual(versions, ['2.31.0', '2.30.0', '2.29.0'].map((v) => explain(v))); - }); - - test('parses output with a single version', () => { - const output = JSON.stringify({ name: 'my-package', versions: ['1.0.0'] }); - const versions = parsePipIndexVersionsJson(output); - assert.deepStrictEqual(versions, [explain('1.0.0')]); - }); - - test('returns undefined for empty versions array', () => { - const output = JSON.stringify({ name: 'pkg', versions: [] }); - const versions = parsePipIndexVersionsJson(output); - assert.strictEqual(versions, undefined); - }); - - test('returns undefined for invalid JSON', () => { - const versions = parsePipIndexVersionsJson('not json'); - assert.strictEqual(versions, undefined); - }); - - test('returns undefined when versions field is missing', () => { - const output = JSON.stringify({ name: 'pkg' }); - const versions = parsePipIndexVersionsJson(output); - assert.strictEqual(versions, undefined); - }); - }); -}); -