Пример #1
0
        public static async Task <IPythonInterpreterFactory> CreateAndAddFactory(
            IServiceProvider site,
            IInterpreterRegistryService registry,
            IInterpreterOptionsService options,
            PythonProjectNode project,
            IWorkspace workspace,
            string path,
            IPythonInterpreterFactory baseInterp,
            bool registerAsCustomEnv,
            string customEnvName,
            bool preferVEnv = false
            )
        {
            if (site == null)
            {
                throw new ArgumentNullException(nameof(site));
            }

            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (baseInterp == null)
            {
                throw new ArgumentNullException(nameof(baseInterp));
            }

            if (preferVEnv)
            {
                await CreateWithVEnv(site, baseInterp, path);
            }
            else
            {
                await CreateWithVirtualEnv(site, baseInterp, path);
            }

            if (registerAsCustomEnv)
            {
                GetVirtualEnvConfig(path, baseInterp, out string interpExe, out string winterpExe, out string pathVar);

                var factory = await CustomEnv.CreateCustomEnv(
                    registry,
                    options,
                    path,
                    interpExe,
                    winterpExe,
                    pathVar,
                    baseInterp.Configuration.Architecture,
                    baseInterp.Configuration.Version,
                    customEnvName
                    );

                if (factory != null)
                {
                    if (project != null)
                    {
                        project.AddInterpreter(factory.Configuration.Id);
                    }
                    else if (workspace != null)
                    {
                        await workspace.SetInterpreterFactoryAsync(factory);
                    }
                }

                return(factory);
            }
            else
            {
                if (project != null)
                {
                    return(project.AddVirtualEnvironment(registry, path, baseInterp));
                }
                else if (workspace != null)
                {
                    // In workspaces, always store the path to the virtual env's python.exe
                    GetVirtualEnvConfig(path, baseInterp, out string interpExe, out string winterpExe, out string pathVar);

                    var workspaceFactoryProvider = site.GetComponentModel().GetService <WorkspaceInterpreterFactoryProvider>();
                    using (workspaceFactoryProvider?.SuppressDiscoverFactories(forceDiscoveryOnDispose: true)) {
                        var relativeInterpExe = PathUtils.GetRelativeFilePath(workspace.Location, interpExe);
                        await workspace.SetInterpreterAsync(relativeInterpExe);
                    }

                    var factory = workspaceFactoryProvider?
                                  .GetInterpreterFactories()
                                  .FirstOrDefault(f => PathUtils.IsSamePath(f.Configuration.InterpreterPath, interpExe));
                    return(factory);
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #2
0
        private async Task CreateVirtualEnvironmentAsync(ITaskHandler taskHandler)
        {
            IPythonInterpreterFactory factory = null;

            bool failed     = true;
            var  baseInterp = _registry.FindInterpreter(_baseInterpreter);

            try {
                factory = await VirtualEnv.CreateAndAddFactory(
                    _site,
                    _registry,
                    _options,
                    _project,
                    _workspace,
                    _virtualEnvPath,
                    baseInterp,
                    _registerAsCustomEnv,
                    _customEnvName,
                    _useVEnv
                    );

                if (factory != null)
                {
                    if (_installReqs && File.Exists(_reqsPath))
                    {
                        await InstallPackagesAsync(taskHandler, factory);
                    }

                    await _site.GetUIThread().InvokeTask(async() => {
                        // Note that for a workspace, VirtualEnv.CreateAndAddFactory
                        // takes care of updating PythonSettings.json, as that is
                        // required in order to obtain the factory. So no need to do
                        // anything here for workspace.
                        if (_project != null)
                        {
                            _project.AddInterpreter(factory.Configuration.Id);
                            if (_setAsCurrent)
                            {
                                _project.SetInterpreterFactory(factory);
                            }
                        }

                        if (_setAsDefault && _options != null)
                        {
                            _options.DefaultInterpreter = factory;
                        }

                        if (_viewInEnvWindow)
                        {
                            await InterpreterListToolWindow.OpenAtAsync(_site, factory);
                        }
                    });
                }

                failed = false;
            } finally {
                _logger?.LogEvent(PythonLogEvent.CreateVirtualEnv, new CreateVirtualEnvInfo()
                {
                    Failed = failed,
                    InstallRequirements = _installReqs,
                    UseVEnv             = _useVEnv,
                    Global                 = _registerAsCustomEnv,
                    LanguageVersion        = baseInterp?.Configuration?.Version.ToString() ?? "",
                    Architecture           = baseInterp?.Configuration?.ArchitectureString ?? "",
                    SetAsDefault           = _setAsDefault,
                    SetAsCurrent           = _setAsCurrent,
                    OpenEnvironmentsWindow = _viewInEnvWindow,
                });
            }

            taskHandler?.Progress.Report(new TaskProgressData()
            {
                CanBeCanceled   = false,
                ProgressText    = Strings.VirtualEnvStatusCenterCreateProgressCompleted,
                PercentComplete = 100,
            });
        }
Пример #3
0
        public static async Task <IPythonInterpreterFactory> CreateAndAddFactory(
            IServiceProvider site,
            IInterpreterRegistryService registry,
            IInterpreterOptionsService options,
            PythonProjectNode project,
            string path,
            IPythonInterpreterFactory baseInterp,
            bool registerAsCustomEnv,
            string customEnvName,
            bool preferVEnv = false
            )
        {
            if (site == null)
            {
                throw new ArgumentNullException(nameof(site));
            }

            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (baseInterp == null)
            {
                throw new ArgumentNullException(nameof(baseInterp));
            }

            if (preferVEnv)
            {
                await CreateWithVEnv(site, baseInterp, path);
            }
            else
            {
                await CreateWithVirtualEnv(site, baseInterp, path);
            }

            if (registerAsCustomEnv)
            {
                GetVirtualEnvConfig(path, baseInterp, out string interpExe, out string winterpExe, out string pathVar);

                var factory = await CustomEnv.CreateCustomEnv(
                    registry,
                    options,
                    path,
                    interpExe,
                    winterpExe,
                    pathVar,
                    baseInterp.Configuration.Architecture,
                    baseInterp.Configuration.Version,
                    customEnvName
                    );

                if (project != null && factory != null)
                {
                    project.AddInterpreter(factory.Configuration.Id);
                }

                return(factory);
            }
            else
            {
                if (project != null)
                {
                    return(project.AddVirtualEnvironment(registry, path, baseInterp));
                }
                else
                {
                    return(null);
                }
            }
        }