/// <summary> /// Installs virtualenv. If pip is not installed, the returned task will /// succeed but error text will be passed to the redirector. /// </summary> public static Task <bool> Install(IServiceProvider provider, IPythonInterpreterFactory factory) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } if (factory == null) { throw new ArgumentNullException(nameof(factory)); } var ui = new VsPackageManagerUI(provider); var interpreterOpts = provider.GetComponentModel().GetService <IInterpreterOptionsService>(); var pm = interpreterOpts?.GetPackageManagers(factory).FirstOrDefault(p => p.UniqueKey == "pip"); if (factory.Configuration.Version < new Version(2, 5)) { ui.OnErrorTextReceived(null, "Python versions earlier than 2.5 are not supported by PTVS.\n"); throw new OperationCanceledException(); } else if (pm == null) { ui.OnErrorTextReceived(null, Strings.PackageManagementNotSupported_Package.FormatUI("virtualenv")); throw new OperationCanceledException(); } else if (factory.Configuration.Version == new Version(2, 5)) { return(pm.InstallAsync(PackageSpec.FromArguments("https://go.microsoft.com/fwlink/?LinkID=317970"), ui, CancellationToken.None)); } else { return(pm.InstallAsync(PackageSpec.FromArguments("https://go.microsoft.com/fwlink/?LinkID=317969"), ui, CancellationToken.None)); } }
public static Task <bool> PipUninstallAsync(this IPythonInterpreterFactory factory, string arguments) { return(factory.PackageManager.UninstallAsync( PackageSpec.FromArguments(arguments), new TestPackageManagerUI(), CancellationToken.None )); }
public async Task Run() { var service = _project.Site.GetComponentModel().GetService <IInterpreterRegistryService>(); IPythonInterpreterFactory factory; try { var baseInterp = service.FindInterpreter(_baseInterpreter); factory = await _project.CreateOrAddVirtualEnvironment( service, _create, _virtualEnvPath, baseInterp, _useVEnv ); } catch (Exception ex) when(!ex.IsCriticalException()) { WriteError(ex.Message); factory = null; } if (factory == null) { return; } var txt = PathUtils.GetAbsoluteFilePath(_project.ProjectHome, "requirements.txt"); if (!_installRequirements || !File.Exists(txt)) { return; } if (factory.PackageManager == null) { WriteError( Strings.PackageManagementNotSupported_Package.FormatUI(PathUtils.GetFileOrDirectoryName(txt)) ); return; } WriteOutput(Strings.RequirementsTxtInstalling.FormatUI(txt)); if (await factory.PackageManager.InstallAsync( PackageSpec.FromArguments("-r " + ProcessOutput.QuoteSingleArgument(txt)), new VsPackageManagerUI(_project.Site), CancellationToken.None )) { WriteOutput(Strings.PackageInstallSucceeded.FormatUI(Path.GetFileName(txt))); } else { WriteOutput(Strings.PackageInstallFailed.FormatUI(Path.GetFileName(txt))); } }
private async Task <bool> PromptInstallModuleAsync( IPythonFormatter formatter, IPythonInterpreterFactory factory, IPackageManager pm ) { await _joinableTaskFactory.SwitchToMainThreadAsync(); var message = Strings.InstallFormatterPrompt.FormatUI(formatter.Package, factory.Configuration.Description); if (ShowYesNoPrompt(message)) { try { return(await pm.InstallAsync(PackageSpec.FromArguments(formatter.Package), new VsPackageManagerUI(_site), CancellationToken.None)); } catch (Exception ex) when(!ex.IsCriticalException()) { ShowErrorMessage(Strings.ErrorUnableToInstallFormatter.FormatUI(ex.Message)); } } return(false); }
internal async Task CreateEnvironmentAsync(CondaEnvironmentView view) { var mgr = CondaEnvironmentManager.Create(_registry); if (mgr == null) { // TODO: Instead of this message box, hide the input/create // controls and show a message there instead. MessageBox.Show(Resources.CondaExtensionNotAvailable, Resources.ProductTitle); return; } _isWorking = true; try { // Use single equal sign to install the selected version or any of its revisions var packages = new[] { PackageSpec.FromArguments($"python={view.VersionName}") }; await mgr.CreateAsync(view.EnvironmentName, packages, this, CancellationToken.None); } finally { _isWorking = false; } }
/// <summary> /// Installs virtualenv. If pip is not installed, the returned task will /// succeed but error text will be passed to the redirector. /// </summary> public static Task <bool> Install(IServiceProvider provider, IPythonInterpreterFactory factory) { var ui = new VsPackageManagerUI(provider); if (factory.Configuration.Version < new Version(2, 5)) { ui.OnErrorTextReceived(null, "Python versions earlier than 2.5 are not supported by PTVS.\n"); throw new OperationCanceledException(); } else if (factory.PackageManager == null) { ui.OnErrorTextReceived(null, Strings.PackageManagementNotSupported_Package.FormatUI("virtualenv")); throw new OperationCanceledException(); } else if (factory.Configuration.Version == new Version(2, 5)) { return(factory.PackageManager.InstallAsync(PackageSpec.FromArguments("https://go.microsoft.com/fwlink/?LinkID=317970"), ui, CancellationToken.None)); } else { return(factory.PackageManager.InstallAsync(PackageSpec.FromArguments("https://go.microsoft.com/fwlink/?LinkID=317969"), ui, CancellationToken.None)); } }
public async Task InstallPackagesAsync() { WriteOutput(Strings.RequirementsTxtInstalling.FormatUI(_reqsPath)); bool success = false; try { var ui = new VsPackageManagerUI(_site); if (!_pm.IsReady) { await _pm.PrepareAsync(ui, CancellationToken.None); } success = await _pm.InstallAsync( PackageSpec.FromArguments("-r " + ProcessOutput.QuoteSingleArgument(_reqsPath)), ui, CancellationToken.None ); } catch (InvalidOperationException ex) { WriteOutput(ex.Message); throw; } catch (Exception ex) when(!ex.IsCriticalException()) { WriteOutput(ex.Message); Debug.Fail(ex.ToUnhandledExceptionMessage(GetType())); throw; } finally { if (success) { WriteOutput(Strings.PackageInstallSucceeded.FormatUI(Path.GetFileName(_reqsPath))); } else { var msg = Strings.PackageInstallFailed.FormatUI(Path.GetFileName(_reqsPath)); WriteOutput(msg); throw new ApplicationException(msg); } } }
private async Task ExecuteWorker(PythonProjectNode project) { _errorListProvider.Tasks.Clear(); var interpFactory = project.GetInterpreterFactoryOrThrow(); var startInfo = GetStartInfo(project); var packagesToInstall = new List <string>(); var pm = interpFactory.PackageManager; if (pm != null) { foreach (var pkg in startInfo.RequiredPackages) { if (!(await pm.GetInstalledPackageAsync(new PackageSpec(pkg), CancellationToken.None)).IsValid) { packagesToInstall.Add(pkg); } } } if (packagesToInstall.Any()) { var installMissingButton = new TaskDialogButton( Strings.CustomCommandPrerequisitesInstallMissing, Strings.CustomCommandPrerequisitesInstallMissingSubtext + "\r\n\r\n" + string.Join("\r\n", packagesToInstall)); var runAnywayButton = new TaskDialogButton(Strings.CustomCommandPrerequisitesRunAnyway); var doNotRunButton = new TaskDialogButton(Strings.CustomCommandPrerequisitesDoNotRun); var taskDialog = new TaskDialog(project.Site) { Title = Strings.ProductTitle, MainInstruction = Strings.CustomCommandPrerequisitesInstruction, Content = Strings.CustomCommandPrerequisitesContent.FormatUI(DisplayLabelWithoutAccessKeys), AllowCancellation = true, Buttons = { installMissingButton, runAnywayButton, doNotRunButton, TaskDialogButton.Cancel } }; var selectedButton = taskDialog.ShowModal(); if (selectedButton == installMissingButton) { var ui = new VsPackageManagerUI(project.Site); if (!pm.IsReady) { await pm.PrepareAsync(ui, CancellationToken.None); } await pm.InstallAsync(PackageSpec.FromArguments(string.Join(" ", packagesToInstall)), ui, CancellationToken.None); } else if (selectedButton == runAnywayButton) { } else { throw new TaskCanceledException(); } } if (startInfo.TargetType == PythonCommandTask.TargetTypePip) { if (startInfo.ExecuteInOutput && pm != null) { var ui = new VsPackageManagerUI(project.Site); if (!pm.IsReady) { await pm.PrepareAsync(ui, CancellationToken.None); } await pm.InstallAsync( PackageSpec.FromArguments(string.IsNullOrEmpty(startInfo.Arguments) ? startInfo.Filename : "{0} {1}".FormatUI(startInfo.Filename, startInfo.Arguments)), ui, CancellationToken.None ); return; } // Rewrite start info to execute startInfo.TargetType = PythonCommandTask.TargetTypeModule; startInfo.AddArgumentAtStart(startInfo.Filename); startInfo.Filename = "pip"; } if (startInfo.ExecuteInRepl) { if (await RunInRepl(project, startInfo)) { return; } } startInfo.AdjustArgumentsForProcessStartInfo(GetInterpreterPath(project, false)); if (startInfo.ExecuteInOutput) { RunInOutput(project, startInfo); } else { RunInConsole(project, startInfo); } }
public async Task Run() { var service = _project.Site.GetComponentModel().GetService <IInterpreterRegistryService>(); IPythonInterpreterFactory factory; try { var baseInterp = service.FindInterpreter(_baseInterpreter); factory = await _project.CreateOrAddVirtualEnvironment( service, _create, _virtualEnvPath, baseInterp, _useVEnv ); } catch (Exception ex) when(!ex.IsCriticalException()) { WriteError(ex.Message); factory = null; } if (factory == null) { return; } var txt = _requirementsPath; if (!_installRequirements || !File.Exists(txt)) { return; } var interpreterOpts = _project.Site.GetComponentModel().GetService <IInterpreterOptionsService>(); var pm = interpreterOpts?.GetPackageManagers(factory).FirstOrDefault(p => p.UniqueKey == "pip"); if (pm == null) { WriteError( Strings.PackageManagementNotSupported_Package.FormatUI(PathUtils.GetFileOrDirectoryName(txt)) ); return; } WriteOutput(Strings.RequirementsTxtInstalling.FormatUI(txt)); bool success = false; try { var ui = new VsPackageManagerUI(_project.Site); if (!pm.IsReady) { await pm.PrepareAsync(ui, CancellationToken.None); } success = await pm.InstallAsync( PackageSpec.FromArguments("-r " + ProcessOutput.QuoteSingleArgument(txt)), ui, CancellationToken.None ); } catch (InvalidOperationException ex) { WriteOutput(ex.Message); } catch (Exception ex) when(!ex.IsCriticalException()) { WriteOutput(ex.Message); Debug.Fail(ex.ToUnhandledExceptionMessage(GetType())); } if (success) { WriteOutput(Strings.PackageInstallSucceeded.FormatUI(Path.GetFileName(txt))); } else { WriteOutput(Strings.PackageInstallFailed.FormatUI(Path.GetFileName(txt))); } }