Example #1
0
        public override async Task CheckAsync()
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            if (IsCreated || IsGloballySuppressed)
            {
                return;
            }

            EnvironmentYmlPath = Project.GetEnvironmentYmlPath();
            Caption            = Project.Caption;
            Context            = InfoBarContexts.Project;
            MissingEnvName     = null;

            if (Project.GetProjectProperty(PythonConstants.SuppressEnvironmentCreationPrompt).IsTrue())
            {
                return;
            }

            // Skip if active is already conda
            var active = Project.ActiveInterpreter;

            if (IsCondaEnvOrAnaconda(active) && active.IsRunnable())
            {
                return;
            }

            var all                = Project.InterpreterFactories.ToArray();
            var allConda           = all.Where(IsCondaEnvOrAnaconda).ToArray();
            var foundConda         = allConda.Where(f => f.IsRunnable()).ToArray();
            var condaNotFoundNames = Project.InvalidInterpreterIds
                                     .Where(id => CondaEnvironmentFactoryProvider.IsCondaEnv(id))
                                     .Select(id => CondaEnvironmentFactoryProvider.NameFromId(id))
                                     .Where(name => name != null)
                                     .ToArray();

            if (condaNotFoundNames.Any() && !foundConda.Any())
            {
                // Propose to recreate one of the conda references, since they are all missing
                MissingEnvName = condaNotFoundNames.First();
            }
            else if (!foundConda.Any() && !string.IsNullOrEmpty(EnvironmentYmlPath))
            {
                // Propose to create a new one, since there's a yaml file and no conda references
                MissingEnvName = null;
            }
            else
            {
                // Nothing to do
                return;
            }

            ShowInfoBar();
        }
Example #2
0
        private bool IsCondaEnvOrAnaconda(IPythonInterpreterFactory fact)
        {
            if (CondaEnvironmentFactoryProvider.IsCondaEnv(fact))
            {
                return(true);
            }

            // If it's not a conda env, but has conda package manager, then do not prompt
            // (this may be the root anaconda installation)
            var pm = Project.InterpreterOptions.GetPackageManagers(fact)?.FirstOrDefault(p => p.UniqueKey == "conda");

            return(pm != null);
        }
 private static void TestTriggerDiscovery(string userProfileFolder, Action triggerDiscovery)
 {
     using (var evt = new AutoResetEvent(false))
         using (var globalProvider = new CPythonInterpreterFactoryProvider(null, false))
             using (var condaProvider = new CondaEnvironmentFactoryProvider(globalProvider, null, new JoinableTaskFactory(new JoinableTaskContext()), true, userProfileFolder)) {
                 // This initializes the provider, discovers the initial set
                 // of factories and starts watching the filesystem.
                 var configs = condaProvider.GetInterpreterConfigurations();
                 condaProvider.DiscoveryStarted += (sender, e) => {
                     evt.Set();
                 };
                 triggerDiscovery();
                 Assert.IsTrue(evt.WaitOne(5000), "Failed to trigger discovery.");
             }
 }
        private static string GetUnusedEnvironmentName(CondaEnvironmentManager mgr)
        {
            // Avoid names already used by any of the existing environments.
            var    info = CondaEnvironmentFactoryProvider.ExecuteCondaInfo(mgr.CondaPath);
            var    used = info.EnvironmentFolders.Select(absPath => PathUtils.GetFileOrDirectoryName(absPath));
            string name;

            do
            {
                // Pick a random name (instead of incrementing a numerical suffix)
                // so this works better if we ever run tests in parallel.
                name = Path.GetRandomFileName();
            } while (used.Contains(name, StringComparer.OrdinalIgnoreCase));

            return(name);
        }
        public AddCondaEnvironmentOperation(
            IServiceProvider site,
            ICondaEnvironmentManager condaMgr,
            PythonProjectNode project,
            IPythonWorkspaceContext workspace,
            string envNameOrPath,
            string envFilePath,
            List <PackageSpec> packages,
            bool setAsCurrent,
            bool setAsDefault,
            bool viewInEnvWindow
            )
        {
            _site            = site ?? throw new ArgumentNullException(nameof(site));
            _condaMgr        = condaMgr ?? throw new ArgumentNullException(nameof(condaMgr));
            _project         = project;
            _workspace       = workspace;
            _envNameOrPath   = envNameOrPath ?? throw new ArgumentNullException(nameof(envNameOrPath));
            _envFilePath     = envFilePath;
            _packages        = packages ?? throw new ArgumentNullException(nameof(packages));
            _setAsCurrent    = setAsCurrent;
            _setAsDefault    = setAsDefault;
            _viewInEnvWindow = viewInEnvWindow;

            // If passed a path, the actual name reported by conda will the last part
            _actualName = PathUtils.GetFileOrDirectoryName(_envNameOrPath);
            if (_actualName.Length == 0)
            {
                _actualName = _envNameOrPath;
            }

            _outputWindow = OutputWindowRedirector.GetGeneral(_site);
            _statusBar    = _site.GetService(typeof(SVsStatusbar)) as IVsStatusbar;
            _showAndActiveOutputWindow = _site.GetPythonToolsService().GeneralOptions.ShowOutputWindowForVirtualEnvCreate;
            _statusCenter    = _site.GetService(typeof(SVsTaskStatusCenterService)) as IVsTaskStatusCenterService;
            _registry        = _site.GetComponentModel().GetService <IInterpreterRegistryService>();
            _options         = _site.GetComponentModel().GetService <IInterpreterOptionsService>();
            _logger          = _site.GetService(typeof(IPythonToolsLogger)) as IPythonToolsLogger;
            _factoryProvider = _site.GetComponentModel().GetService <CondaEnvironmentFactoryProvider>();
        }
        private static async Task <string> GetEnvironmentPathAsync(CondaEnvironmentManager mgr, string envName)
        {
            var info = await CondaEnvironmentFactoryProvider.ExecuteCondaInfoAsync(mgr.CondaPath);

            return(info.EnvironmentFolders.SingleOrDefault(absPath => string.Compare(PathUtils.GetFileOrDirectoryName(absPath), envName, StringComparison.OrdinalIgnoreCase) == 0));
        }
Example #7
0
        public override async Task CheckAsync()
        {
            if (IsCreated)
            {
                return;
            }

            if (!Project.Site.GetPythonToolsService().GeneralOptions.PromptForEnvCreate)
            {
                return;
            }

            var suppressProp = Project.GetProjectProperty(PythonConstants.SuppressEnvironmentCreationPrompt);

            if (suppressProp.IsTrue())
            {
                return;
            }

            // Skip if active is already conda
            var active = Project.ActiveInterpreter;

            if (IsCondaEnvOrAnaconda(active) && active.IsRunnable())
            {
                return;
            }

            var yamlPath = Project.GetEnvironmentYmlPath();

            var all                = Project.InterpreterFactories.ToArray();
            var allConda           = all.Where(IsCondaEnvOrAnaconda).ToArray();
            var foundConda         = allConda.Where(f => f.IsRunnable()).ToArray();
            var condaNotFoundNames = Project.InvalidInterpreterIds
                                     .Where(id => CondaEnvironmentFactoryProvider.IsCondaEnv(id))
                                     .Select(id => CondaEnvironmentFactoryProvider.NameFromId(id))
                                     .Where(name => name != null)
                                     .ToArray();

            string existingName;

            if (condaNotFoundNames.Any() && !foundConda.Any())
            {
                // Propose to recreate one of the conda references, since they are all missing
                existingName = condaNotFoundNames.First();
            }
            else if (!foundConda.Any() && !string.IsNullOrEmpty(yamlPath))
            {
                // Propose to create a new one, since there's a yaml file and no conda references
                existingName = null;
            }
            else
            {
                // Nothing to do
                return;
            }

            Action create = () => {
                Logger?.LogEvent(
                    PythonLogEvent.CondaEnvCreateInfoBar,
                    new CondaEnvCreateInfoBarInfo()
                {
                    Action = CondaEnvCreateInfoBarActions.Create,
                }
                    );
                Project.ShowAddCondaEnvironment(existingName, yamlPath);
                Close();
            };

            Action projectIgnore = () => {
                Logger?.LogEvent(
                    PythonLogEvent.CondaEnvCreateInfoBar,
                    new CondaEnvCreateInfoBarInfo()
                {
                    Action = CondaEnvCreateInfoBarActions.Ignore,
                }
                    );
                Project.SetProjectProperty(PythonConstants.SuppressEnvironmentCreationPrompt, true.ToString());
                Close();
            };

            var messages = new List <IVsInfoBarTextSpan>();
            var actions  = new List <InfoBarActionItem>();

            var msg = existingName != null
                ? Strings.CondaInfoBarRecreateMessage.FormatUI(Project.Caption, existingName)
                : Strings.CondaInfoBarCreateNewMessage.FormatUI(Project.Caption);

            messages.Add(new InfoBarTextSpan(msg));
            actions.Add(new InfoBarButton(Strings.CondaInfoBarCreateAction, create));
            actions.Add(new InfoBarButton(Strings.CondaInfoBarProjectIgnoreAction, projectIgnore));

            Logger?.LogEvent(
                PythonLogEvent.CondaEnvCreateInfoBar,
                new CondaEnvCreateInfoBarInfo()
            {
                Action = CondaEnvCreateInfoBarActions.Prompt,
                Reason = existingName != null ? CondaEnvCreateInfoBarReasons.MissingEnv : CondaEnvCreateInfoBarReasons.NoEnv,
            }
                );

            Create(new InfoBarModel(messages, actions, KnownMonikers.StatusInformation, isCloseButtonVisible: true));
        }