Пример #1
0
        private async Task CheckPackagesAsync() {
            var uiThread = ProjectMgr.Site.GetUIThread();
            uiThread.MustBeCalledFromUIThreadOrThrow();

            bool prevChecked = _checkedItems;
            // Use _checkingItems to prevent the expanded state from
            // disappearing too quickly.
            _checkingItems = true;
            _checkedItems = true;
            if (!Directory.Exists(_factory.Configuration.LibraryPath)) {
                _checkingItems = false;
                ProjectMgr.OnPropertyChanged(this, (int)__VSHPROPID.VSHPROPID_Expandable, 0);
                return;
            }

            HashSet<string> lines;
            bool anyChanges = false;
            try {
                lines = await Pip.List(_factory).ConfigureAwait(true);
            } catch (MissingInterpreterException) {
                return;
            } catch (NoInterpretersException) {
                return;
            } catch (FileNotFoundException) {
                return;
            }

            // Ensure we are back on the UI thread
            uiThread.MustBeCalledFromUIThread();

            if (ProjectMgr == null || ProjectMgr.IsClosed) {
                return;
            }

            var existing = AllChildren.ToDictionary(c => c.Url);

            // remove the nodes which were uninstalled.
            foreach (var keyValue in existing) {
                if (!lines.Contains(keyValue.Key)) {
                    RemoveChild(keyValue.Value);
                    anyChanges = true;
                }
            }

            // remove already existing nodes so we don't add them a 2nd time
            lines.ExceptWith(existing.Keys);

            // add the new nodes
            foreach (var line in lines) {
                AddChild(new InterpretersPackageNode(ProjectMgr, line));
                anyChanges = true;

                var packageInfo = PythonProjectNode.FindRequirementRegex.Match(line.ToLower());
                if (packageInfo.Groups["name"].Success) {
                    //Log the details of the Installation
                    var packageDetails = new Logging.PackageInstallDetails(
                        packageInfo.Groups["name"].Value,
                        packageInfo.Groups["ver"].Success ? packageInfo.Groups["ver"].Value : String.Empty,
                        _factory.GetType().Name,
                        _factory.Configuration.Version.ToString(),
                        _factory.Configuration.Architecture.ToString(),
                        "Existing", //Installer if we tracked it
                        false, //Installer was not run elevated
                        0); //The installation already existed
                    ProjectMgr.Site.GetPythonToolsService().Logger.LogEvent(Logging.PythonLogEvent.PackageInstalled, packageDetails);
                }
            }
            _checkingItems = false;

            ProjectMgr.OnInvalidateItems(this);
            if (!prevChecked) {
                if (anyChanges) {
                    ProjectMgr.OnPropertyChanged(this, (int)__VSHPROPID.VSHPROPID_Expandable, 0);
                }
                if (ProjectMgr.ParentHierarchy != null) {
                    ExpandItem(EXPANDFLAGS.EXPF_CollapseFolder);
                }
            }

            if (prevChecked && anyChanges) {
                var withDb = _factory as IPythonInterpreterFactoryWithDatabase;
                if (withDb != null) {
                    withDb.GenerateDatabase(GenerateDatabaseOptions.SkipUnchanged);
                }
            }
        }
Пример #2
0
        private async Task ExecuteWorker(PythonProjectNode project)
        {
            _errorListProvider.Tasks.Clear();

            var interpFactory = project.GetInterpreterFactoryOrThrow();
            var startInfo     = GetStartInfo(project);

            var packagesToInstall = new List <string>();

            foreach (var pkg in startInfo.RequiredPackages)
            {
                if (!await Pip.IsInstalled(interpFactory, pkg))
                {
                    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)
                {
                    await Pip.Install(
                        project.Site,
                        interpFactory,
                        string.Join(" ", packagesToInstall),
                        false,
                        OutputWindowRedirector.GetGeneral(project.Site));
                }
                else if (selectedButton == runAnywayButton)
                {
                }
                else
                {
                    throw new TaskCanceledException();
                }
            }

            if (startInfo.TargetType == CreatePythonCommandItem.TargetTypePip)
            {
                if (startInfo.ExecuteInOutput)
                {
                    await Pip.Install(
                        _project.Site,
                        interpFactory,
                        string.IsNullOrEmpty(startInfo.Arguments)?
                        startInfo.Filename :
                        string.Format("{0} {1}", startInfo.Filename, startInfo.Arguments),
                        project.Site,
                        false,
                        OutputWindowRedirector.GetGeneral(project.Site)
                        );

                    return;
                }

                // Rewrite start info to execute
                startInfo.TargetType = CreatePythonCommandItem.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);
            }
        }