private async Task <PythonAnalyzer> CreateAnalyzer(PythonInitializationOptions.Interpreter interpreter) { IPythonInterpreterFactory factory = null; if (!string.IsNullOrEmpty(interpreter.assembly) && !string.IsNullOrEmpty(interpreter.typeName)) { try { var assembly = File.Exists(interpreter.assembly) ? AssemblyName.GetAssemblyName(interpreter.assembly) : new AssemblyName(interpreter.assembly); var type = Assembly.Load(assembly).GetType(interpreter.typeName, true); factory = (IPythonInterpreterFactory)Activator.CreateInstance( type, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new object[] { interpreter.properties }, CultureInfo.CurrentCulture ); } catch (Exception ex) { LogMessage(MessageType.Warning, ex.ToString()); } } else { factory = new AstPythonInterpreterFactory(interpreter.properties); } if (factory == null) { Version v; if (!Version.TryParse(interpreter.version ?? "0.0", out v)) { v = new Version(); } factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(v); } var interp = factory.CreateInterpreter(); if (interp == null) { throw new InvalidOperationException("Failed to create interpreter"); } LogMessage(MessageType.Info, $"Created {interp.GetType().FullName} instance from {factory.GetType().FullName}"); return(await PythonAnalyzer.CreateAsync(factory, interp)); }
private DjangoAnalyzer AnalyzerTest(string path) { var version = new Version(2, 7); var testFact = new AstPythonInterpreterFactory( new InterpreterConfiguration($"AnalysisOnly|{version}", $"Analysis Only {version}", version: version, uiMode: InterpreterUIMode.Normal), new InterpreterFactoryCreationOptions { WatchFileSystem = false } ); var serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider(); PythonAnalyzer analyzer = PythonAnalyzer.CreateAsync(testFact).WaitAndUnwrapExceptions(); DjangoAnalyzer djangoAnalyzer = new DjangoAnalyzer(); djangoAnalyzer.Register(analyzer); analyzer.SetSearchPaths(new[] { path }); List <IPythonProjectEntry> entries = new List <IPythonProjectEntry>(); foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories)) { var entry = analyzer.AddModule(ModulePath.FromFullPath(file).ModuleName, file); var parser = Parser.CreateParser( new FileStream(file, FileMode.Open, FileAccess.Read), PythonLanguageVersion.V27 ); using (var p = entry.BeginParse()) { p.Tree = parser.ParseFile(); p.Complete(); } entries.Add(entry); } foreach (var entry in entries) { entry.Analyze(CancellationToken.None, false); } return(djangoAnalyzer); }
private async Task FullStdLibTest(PythonVersion v, params string[] skipModules) { v.AssertInstalled(); var factory = new AstPythonInterpreterFactory(v.Configuration, new InterpreterFactoryCreationOptions { DatabasePath = TestData.GetTempPath(), UseExistingCache = false }); var modules = ModulePath.GetModulesInLib(v.PrefixPath).ToList(); var skip = new HashSet <string>(skipModules); skip.UnionWith(new[] { "matplotlib.backends._backend_gdk", "matplotlib.backends._backend_gtkagg", "matplotlib.backends._gtkagg", }); bool anySuccess = false; bool anyExtensionSuccess = false, anyExtensionSeen = false; bool anyParseError = false; using (var analyzer = new PythonAnalysis(factory)) { try { var tasks = new List <Task <Tuple <ModulePath, IPythonModule> > >(); var interp = (AstPythonInterpreter)analyzer.Analyzer.Interpreter; foreach (var m in skip) { interp.AddUnimportableModule(m); } foreach (var r in modules .Where(m => !skip.Contains(m.ModuleName)) .GroupBy(m => { int i = m.FullName.IndexOf('.'); return(i <= 0 ? m.FullName : m.FullName.Remove(i)); }) .AsParallel() .SelectMany(g => g.Select(m => Tuple.Create(m, interp.ImportModule(m.ModuleName)))) ) { var modName = r.Item1; var mod = r.Item2; anyExtensionSeen |= modName.IsNativeExtension; if (mod == null) { Trace.TraceWarning("failed to import {0} from {1}", modName.ModuleName, modName.SourceFile); } else if (mod is AstScrapedPythonModule smod) { if (smod.ParseErrors?.Any() ?? false) { anyParseError = true; Trace.TraceError("Parse errors in {0}", modName.SourceFile); foreach (var e in smod.ParseErrors) { Trace.TraceError(e); } } else { anySuccess = true; anyExtensionSuccess |= modName.IsNativeExtension; mod.GetMemberNames(analyzer.ModuleContext).ToList(); } } else if (mod is AstPythonModule) { // pass } else { Trace.TraceError("imported {0} as type {1}", modName.ModuleName, mod.GetType().FullName); } } } finally { _analysisLog = analyzer.GetLogContent(CultureInfo.InvariantCulture); } } Assert.IsTrue(anySuccess, "failed to import any modules at all"); Assert.IsTrue(anyExtensionSuccess || !anyExtensionSeen, "failed to import all extension modules"); Assert.IsFalse(anyParseError, "parse errors occurred"); }