Beispiel #1
0
        private string GetData()
        {
            StringBuilder res = new StringBuilder();

            if (PythonToolsPackage.IsIpyToolsInstalled())
            {
                res.AppendLine("WARNING: IpyTools is installed on this machine.  Having both IpyTools and Python Tools for Visual Studio installed will break Python editing.");
            }

            var pythonPathIsMasked = _serviceProvider.GetPythonToolsService().GeneralOptions.ClearGlobalPythonPath ? " (masked)" : "";

            var dte = (EnvDTE.DTE)_serviceProvider.GetService(typeof(EnvDTE.DTE));

            res.AppendLine("Projects: ");

            var projects = dte.Solution.Projects;

            foreach (EnvDTE.Project project in projects)
            {
                string name;
                try {
                    // Some projects will throw rather than give us a unique
                    // name. They are not ours, so we will ignore them.
                    name = project.UniqueName;
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    bool isPythonProject = false;

                    try {
                        isPythonProject = Utilities.GuidEquals(PythonConstants.ProjectFactoryGuid, project.Kind);
                    } catch (Exception ex2) when(!ex2.IsCriticalException())
                    {
                    }

                    if (isPythonProject)
                    {
                        // Actually, it was one of our projects, so we do care
                        // about the exception. We'll add it to the output,
                        // rather than crashing.
                        res.AppendLine("    Project: " + ex.Message);
                        res.AppendLine("        Kind: Python");
                    }
                    continue;
                }
                res.AppendLine("    Project: " + name);

                if (Utilities.GuidEquals(PythonConstants.ProjectFactoryGuid, project.Kind))
                {
                    res.AppendLine("        Kind: Python");

                    foreach (var prop in InterestingDteProperties)
                    {
                        res.AppendLine("        " + prop + ": " + GetProjectProperty(project, prop));
                    }

                    var pyProj = project.GetPythonProject();
                    if (pyProj != null)
                    {
                        _serviceProvider.GetUIThread().Invoke(() => {
                            foreach (var prop in InterestingProjectProperties)
                            {
                                var propValue = pyProj.GetProjectProperty(prop);
                                if (propValue != null)
                                {
                                    res.AppendLine("        " + prop + ": " + propValue);
                                }
                            }
                        });

                        foreach (var factory in pyProj.InterpreterFactories)
                        {
                            res.AppendLine();
                            res.AppendLine("        Interpreter: " + factory.Configuration.Description);
                            res.AppendLine("            Id: " + factory.Configuration.Id);
                            res.AppendLine("            Version: " + factory.Configuration.Version);
                            res.AppendLine("            Arch: " + factory.Configuration.Architecture);
                            res.AppendLine("            Prefix Path: " + factory.Configuration.PrefixPath ?? "(null)");
                            res.AppendLine("            Path: " + factory.Configuration.InterpreterPath ?? "(null)");
                            res.AppendLine("            Windows Path: " + factory.Configuration.WindowsInterpreterPath ?? "(null)");
                            res.AppendLine("            Lib Path: " + factory.Configuration.LibraryPath ?? "(null)");
                            res.AppendLine(string.Format("            Path Env: {0}={1}{2}",
                                                         factory.Configuration.PathEnvironmentVariable ?? "(null)",
                                                         Environment.GetEnvironmentVariable(factory.Configuration.PathEnvironmentVariable ?? ""),
                                                         pythonPathIsMasked
                                                         ));
                        }
                    }
                }
                else
                {
                    res.AppendLine("        Kind: " + project.Kind);
                }

                res.AppendLine();
            }

            res.AppendLine("Environments: ");
            var knownProviders = _serviceProvider.GetComponentModel().GetExtensions <IPythonInterpreterFactoryProvider>();

            foreach (var provider in knownProviders)
            {
                res.AppendLine("    " + provider.GetType().FullName);
                foreach (var config in provider.GetInterpreterConfigurations())
                {
                    res.AppendLine("        Id: " + config.Id);
                    res.AppendLine("        Factory: " + config.Description);
                    res.AppendLine("        Version: " + config.Version);
                    res.AppendLine("        Arch: " + config.Architecture);
                    res.AppendLine("        Prefix Path: " + config.PrefixPath ?? "(null)");
                    res.AppendLine("        Path: " + config.InterpreterPath ?? "(null)");
                    res.AppendLine("        Windows Path: " + config.WindowsInterpreterPath ?? "(null)");
                    res.AppendLine("        Lib Path: " + config.LibraryPath ?? "(null)");
                    res.AppendLine("        Path Env: " + config.PathEnvironmentVariable ?? "(null)");
                    res.AppendLine();
                }
            }

            res.AppendLine("Launchers:");
            var launchProviders = _serviceProvider.GetComponentModel().GetExtensions <IPythonLauncherProvider>();

            foreach (var launcher in launchProviders)
            {
                res.AppendLine("    Launcher: " + launcher.GetType().FullName);
                res.AppendLine("        " + launcher.Description);
                res.AppendLine("        " + launcher.Name);
                res.AppendLine();
            }

            try {
                res.AppendLine("Logged events/stats:");
                var inMemLogger = _serviceProvider.GetComponentModel().GetService <InMemoryLogger>();
                res.AppendLine(inMemLogger.ToString());
                res.AppendLine();
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                res.AppendLine("  Failed to access event log.");
                res.AppendLine(ex.ToString());
                res.AppendLine();
            }

            try {
                res.AppendLine("System events:");

                var application = new EventLog("Application");
                var lastWeek    = DateTime.Now.Subtract(TimeSpan.FromDays(7));
                foreach (var entry in application.Entries.Cast <EventLogEntry>()
                         .Where(e => e.InstanceId == 1026L) // .NET Runtime
                         .Where(e => e.TimeGenerated >= lastWeek)
                         .Where(e => InterestingApplicationLogEntries.IsMatch(e.Message))
                         .OrderByDescending(e => e.TimeGenerated)
                         )
                {
                    res.AppendLine(string.Format("Time: {0:s}", entry.TimeGenerated));
                    using (var reader = new StringReader(entry.Message.TrimEnd())) {
                        for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
                        {
                            res.AppendLine(line);
                        }
                    }
                    res.AppendLine();
                }
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                res.AppendLine("  Failed to access event log.");
                res.AppendLine(ex.ToString());
                res.AppendLine();
            }

            res.AppendLine("Loaded assemblies:");
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().OrderBy(assem => assem.FullName))
            {
                AssemblyFileVersionAttribute assemFileVersion;
                var error = "(null)";
                try {
                    assemFileVersion = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                                       .OfType <AssemblyFileVersionAttribute>()
                                       .FirstOrDefault();
                } catch (Exception e) when(!e.IsCriticalException())
                {
                    assemFileVersion = null;
                    error            = string.Format("{0}: {1}", e.GetType().Name, e.Message);
                }

                res.AppendLine(string.Format("  {0}, FileVersion={1}",
                                             assembly.FullName,
                                             assemFileVersion?.Version ?? error
                                             ));
            }
            res.AppendLine();

            string globalAnalysisLog = PythonTypeDatabase.GlobalLogFilename;

            if (File.Exists(globalAnalysisLog))
            {
                res.AppendLine("Global Analysis:");
                try {
                    res.AppendLine(File.ReadAllText(globalAnalysisLog));
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    res.AppendLine("Error reading the global analysis log.");
                    res.AppendLine("Please wait for analysis to complete and try again.");
                    res.AppendLine(ex.ToString());
                }
            }
            res.AppendLine();

            res.AppendLine("Environment Analysis Logs: ");
            foreach (var provider in knownProviders)
            {
                foreach (var factory in provider.GetInterpreterFactories().OfType <IPythonInterpreterFactoryWithDatabase>())
                {
                    res.AppendLine(factory.Configuration.Description);
                    string analysisLog = factory.GetAnalysisLogContent(CultureInfo.InvariantCulture);
                    if (!string.IsNullOrEmpty(analysisLog))
                    {
                        res.AppendLine(analysisLog);
                    }
                    res.AppendLine();
                }
            }
            return(res.ToString());
        }