/// <summary> /// Creates a new analyzer that is ready for use. /// </summary> public static async Task <PythonAnalyzer> CreateAsync(IPythonInterpreterFactory factory, CancellationToken token = default) { var analyzer = new PythonAnalyzer(factory); try { await analyzer.ReloadModulesAsync(token).ConfigureAwait(false); } catch (Exception) { analyzer.Dispose(); throw; } return(analyzer); }
// Test helper method internal static PythonAnalyzer CreateSynchronously( IPythonInterpreterFactory factory, IPythonInterpreter interpreter = null ) { var res = new PythonAnalyzer(factory, interpreter); try { res.ReloadModulesAsync(CancellationToken.None).WaitAndUnwrapExceptions(); var r = res; res = null; return(r); } finally { if (res != null) { res.Dispose(); } } }
/// <summary> /// Creates a new analyzer that is ready for use. /// </summary> public static async Task <PythonAnalyzer> CreateAsync( IPythonInterpreterFactory factory, IPythonInterpreter interpreter = null ) { var res = new PythonAnalyzer(factory, interpreter, null); try { await res.ReloadModulesAsync().ConfigureAwait(false); var r = res; res = null; return(r); } finally { if (res != null) { res.Dispose(); } } }
internal VsProjectAnalyzer( IServiceProvider serviceProvider, IPythonInterpreter interpreter, IPythonInterpreterFactory factory, IPythonInterpreterFactory[] allFactories, bool implicitProject = true ) { _errorProvider = (ErrorTaskProvider)serviceProvider.GetService(typeof(ErrorTaskProvider)); _commentTaskProvider = (CommentTaskProvider)serviceProvider.GetService(typeof(CommentTaskProvider)); _unresolvedSquiggles = new UnresolvedImportSquiggleProvider(serviceProvider, _errorProvider); _queue = new ParseQueue(this); _analysisQueue = new AnalysisQueue(this); _analysisQueue.AnalysisStarted += AnalysisQueue_AnalysisStarted; _allFactories = allFactories; _interpreterFactory = factory; _implicitProject = implicitProject; if (interpreter != null) { _pyAnalyzer = PythonAnalyzer.Create(factory, interpreter); ReloadTask = _pyAnalyzer.ReloadModulesAsync().HandleAllExceptions(SR.ProductName, GetType()); ReloadTask.ContinueWith(_ => ReloadTask = null); interpreter.ModuleNamesChanged += OnModulesChanged; } _projectFiles = new ConcurrentDictionary<string, IProjectEntry>(StringComparer.OrdinalIgnoreCase); _pyService = serviceProvider.GetPythonToolsService(); _serviceProvider = serviceProvider; if (_pyAnalyzer != null) { _pyAnalyzer.Limits.CrossModule = _pyService.GeneralOptions.CrossModuleAnalysisLimit; // TODO: Load other limits from options } _userCount = 1; }
private Response Initialize(AP.InitializeRequest request) { List<AssemblyCatalog> catalogs = new List<AssemblyCatalog>(); HashSet<string> assemblies = new HashSet<string>(request.mefExtensions); assemblies.Add(typeof(IInterpreterRegistryService).Assembly.Location); assemblies.Add(GetType().Assembly.Location); List<string> failures = new List<string>(); string error = null; foreach (var asm in assemblies) { try { var asmCatalog = new AssemblyCatalog(asm); _catalog.Catalogs.Add(asmCatalog); } catch (Exception e) { failures.Add(String.Format("Failed to load {0}: {1}", asm, e)); } } if (request.projectFile != null) { var projectContextProvider = _container.GetExportedValue<OutOfProcProjectContextProvider>(); projectContextProvider.AddContext( new InMemoryProject( request.projectFile, new Dictionary<string, object>() { { "InterpreterId", request.interpreterId }, { "ProjectHome", request.projectHome }, { "Interpreters", request.derivedInterpreters.Select( interp => new Dictionary<string, string>() { { "EvaluatedInclude", interp.name }, { MSBuildConstants.IdKey, interp.id }, { MSBuildConstants.VersionKey, interp.version }, { MSBuildConstants.DescriptionKey, interp.description }, { MSBuildConstants.BaseInterpreterKey, interp.baseInterpreter }, { MSBuildConstants.InterpreterPathKey, interp.path }, { MSBuildConstants.WindowsPathKey, interp.windowsPath }, { MSBuildConstants.LibraryPathKey, interp.libPath }, { MSBuildConstants.PathEnvVarKey, interp.pathEnvVar }, { MSBuildConstants.ArchitectureKey, interp.arch } } ).ToArray() } } ) ); } var registry = _container.GetExportedValue<IInterpreterRegistryService>(); IPythonInterpreterFactory factory = null; Version analysisVersion; if (request.interpreterId.StartsWith("AnalysisOnly;")) { int versionStart = request.interpreterId.IndexOf(';') + 1; int versionEnd = request.interpreterId.IndexOf(';', versionStart); if (Version.TryParse(request.interpreterId.Substring(versionStart, versionEnd - versionStart), out analysisVersion)) { string[] dbDirs; if (versionEnd + 1 != request.interpreterId.Length) { dbDirs = request.interpreterId.Substring(versionEnd + 1).Split(';'); } else { dbDirs = null; } factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(analysisVersion, null, dbDirs); } } else { factory = registry.FindInterpreter(request.interpreterId); } if (factory == null) { error = String.Format("No active interpreter found for interpreter ID: {0}", request.interpreterId); return new AP.InitializeResponse() { failedLoads = failures.ToArray(), error = error }; } _interpreterFactory = factory; _allConfigs = registry.Configurations.ToArray(); var interpreter = factory.CreateInterpreter(); if (interpreter != null) { _pyAnalyzer = PythonAnalyzer.Create(factory, interpreter); ReloadTask = _pyAnalyzer.ReloadModulesAsync()/*.HandleAllExceptions(_serviceProvider, GetType())*/; ReloadTask.ContinueWith(_ => ReloadTask = null); interpreter.ModuleNamesChanged += OnModulesChanged; } return new AP.InitializeResponse() { failedLoads = failures.ToArray(), error = error }; }
internal PythonAnalyzer AnalyzeDir(string dir, PythonLanguageVersion version = PythonLanguageVersion.V27, IEnumerable<string> excludeDirectories = null, CancellationToken? cancel = null) { List<string> files = new List<string>(); try { ISet<string> excluded = null; if (excludeDirectories != null) { excluded = new HashSet<string>(excludeDirectories, StringComparer.InvariantCultureIgnoreCase); } CollectFiles(dir, files, excluded); } catch (DirectoryNotFoundException) { return null; } List<FileStreamReader> sourceUnits = new List<FileStreamReader>(); foreach (string file in files) { sourceUnits.Add( new FileStreamReader(file) ); } Stopwatch sw = new Stopwatch(); sw.Start(); long start0 = sw.ElapsedMilliseconds; // Explicitly specify the builtins name because we are using a 2.7 // interpreter for all versions. var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion()); var projectState = new PythonAnalyzer(fact, fact.CreateInterpreter(), "__builtin__"); projectState.ReloadModulesAsync().WaitAndUnwrapExceptions(); projectState.Limits = AnalysisLimits.GetStandardLibraryLimits(); var modules = new List<IPythonProjectEntry>(); foreach (var sourceUnit in sourceUnits) { try { modules.Add(projectState.AddModule( ModulePath.FromFullPath(sourceUnit.Path).ModuleName, sourceUnit.Path, null )); } catch (ArgumentException) { // Invalid module name, so skip the module } } long start1 = sw.ElapsedMilliseconds; Trace.TraceInformation("AddSourceUnit: {0} ms", start1 - start0); var nodes = new List<Microsoft.PythonTools.Parsing.Ast.PythonAst>(); for (int i = 0; i < modules.Count; i++) { PythonAst ast = null; try { var sourceUnit = sourceUnits[i]; ast = Parser.CreateParser(sourceUnit, version).ParseFile(); } catch (Exception) { } nodes.Add(ast); } long start2 = sw.ElapsedMilliseconds; Trace.TraceInformation("Parse: {0} ms", start2 - start1); for (int i = 0; i < modules.Count; i++) { var ast = nodes[i]; if (ast != null) { modules[i].UpdateTree(ast, null); } } long start3 = sw.ElapsedMilliseconds; for (int i = 0; i < modules.Count; i++) { Trace.TraceInformation("Analyzing {1}: {0} ms", sw.ElapsedMilliseconds - start3, sourceUnits[i].Path); var ast = nodes[i]; if (ast != null) { modules[i].Analyze(cancel ?? CancellationToken.None, true); } } if (modules.Count > 0) { Trace.TraceInformation("Analyzing queue"); modules[0].AnalysisGroup.AnalyzeQueuedEntries(cancel ?? CancellationToken.None); } long start4 = sw.ElapsedMilliseconds; Trace.TraceInformation("Analyze: {0} ms", start4 - start3); return projectState; }