public async Task <List <string> > EnumerateAllModules(bool refresh = false) { AbortOnInvalidConfiguration(); if (_modules == null || refresh) { var stdLibPaths = new HashSet <string>(StringComparer.OrdinalIgnoreCase); stdLibPaths.Add(Path.Combine(_factory.Configuration.PrefixPath, "DLLs")); stdLibPaths.Add(Path.GetDirectoryName(_factory.Configuration.InterpreterPath)); var results = await Task.Run(() => { var paths = PythonTypeDatabase.GetCachedDatabaseSearchPaths(_factory.DatabasePath); if (paths == null) { paths = PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync( _factory.Configuration.InterpreterPath ).WaitAndUnwrapExceptions(); try { PythonTypeDatabase.WriteDatabaseSearchPaths(_factory.DatabasePath, paths); } catch (Exception ex) { if (ex.IsCriticalException()) { throw; } } } var groups = PythonTypeDatabase.GetDatabaseExpectedModules( _factory.Configuration.Version, paths ).ToList(); var stdLibModules = groups[0].Select(mp => mp.ModuleName).ToList(); var modules = groups.SelectMany().Select(mp => mp.ModuleName).ToList(); stdLibModules.Sort(); modules.Sort(); for (int i = stdLibModules.Count - 1; i > 0; --i) { if (stdLibModules[i - 1] == stdLibModules[i]) { stdLibModules.RemoveAt(i); } } for (int i = modules.Count - 1; i > 0; --i) { if (modules[i - 1] == modules[i]) { modules.RemoveAt(i); } } return(Tuple.Create(modules, stdLibModules)); }); _modules = results.Item1; _stdLibModules = results.Item2; } return(_modules); }
private IEnumerable <PythonLibraryPath> GetCurrentSearchPaths() { if (!File.Exists(Configuration?.InterpreterPath)) { return(null); } try { return(PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync(Configuration.InterpreterPath).WaitAndUnwrapExceptions()); } catch (InvalidOperationException) { return(null); } }
public async Task GetExpectedDatabaseModules() { Python.AssertInstalled(); var db = PythonTypeDatabase.GetDatabaseExpectedModules( Python.Version.ToVersion(), await PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync(Python.InterpreterPath) ).ToList(); var stdlib = db[0]; AssertUtil.ContainsAtLeast(stdlib.Select(mp => mp.FullName), "os", "ctypes.__init__", "encodings.utf_8", "ntpath" ); }
public async Task GetSearchPaths() { Python.AssertInstalled(); var paths = await PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync(Python.InterpreterPath); Console.WriteLine("Paths for {0}", Python.InterpreterPath); foreach (var path in paths) { Console.WriteLine("{0} {1}", path.Path, path.IsStandardLibrary ? "(stdlib)" : ""); } // Python.PrefixPath and LibraryPath should be included. // We can't assume anything else AssertUtil.ContainsAtLeast(paths.Select(p => p.Path.ToLowerInvariant().TrimEnd('\\')), Python.PrefixPath.ToLowerInvariant().TrimEnd('\\'), Python.LibPath.ToLowerInvariant().TrimEnd('\\') ); // All paths should exist AssertUtil.ArrayEquals(paths.Where(p => !Directory.Exists(p.Path)).ToList(), new PythonLibraryPath[0]); // Ensure we can round-trip the entries via ToString/Parse var asStrings = paths.Select(p => p.ToString()).ToList(); var asPaths = asStrings.Select(PythonLibraryPath.Parse).ToList(); var asStrings2 = asPaths.Select(p => p.ToString()).ToList(); AssertUtil.ArrayEquals(asStrings, asStrings2); AssertUtil.ArrayEquals(paths, asPaths, (o1, o2) => { PythonLibraryPath p1 = (PythonLibraryPath)o1, p2 = (PythonLibraryPath)o2; return(p1.Path == p2.Path && p1.IsStandardLibrary == p2.IsStandardLibrary); }); var dbPath = TestData.GetTempPath(randomSubPath: true); Assert.IsNull(PythonTypeDatabase.GetCachedDatabaseSearchPaths(dbPath), "Should not have found cached paths in an empty directory"); PythonTypeDatabase.WriteDatabaseSearchPaths(dbPath, paths); Assert.IsTrue(File.Exists(Path.Combine(dbPath, "database.path"))); var paths2 = PythonTypeDatabase.GetCachedDatabaseSearchPaths(dbPath); AssertUtil.ArrayEquals(paths, paths2, (o1, o2) => { PythonLibraryPath p1 = (PythonLibraryPath)o1, p2 = (PythonLibraryPath)o2; return(p1.Path == p2.Path && p1.IsStandardLibrary == p2.IsStandardLibrary); }); }
public async Task GetVirtualEnvDatabasePaths() { var version = PythonPaths.Python27 ?? PythonPaths.Python27_x64; version.AssertInstalled(); var env = Path.Combine(TestData.GetTempPath(randomSubPath: true), "env"); using (var p = ProcessOutput.RunHiddenAndCapture(version.InterpreterPath, "-m", "virtualenv", env)) { if ((await p) != 0) { Assert.Fail("Could not create virtualenv{0}{1}{0}{2}", Environment.NewLine, string.Join(Environment.NewLine, p.StandardOutputLines), string.Join(Environment.NewLine, p.StandardErrorLines) ); return; } } var interpreter = Path.Combine(env, "Scripts", "python.exe"); var paths = await PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync(interpreter); AssertUtil.ContainsAtLeast( paths.Where(p => p.IsStandardLibrary).Select(p => p.Path), new[] { Path.Combine(env, "Scripts").ToLowerInvariant(), PathUtils.TrimEndSeparator(env.ToLowerInvariant()), PathUtils.TrimEndSeparator(version.PrefixPath.ToLowerInvariant()), Path.Combine(version.PrefixPath, "Lib").ToLowerInvariant() } ); AssertUtil.ContainsAtLeast( paths.Where(p => !p.IsStandardLibrary).Select(p => p.Path), new[] { Path.Combine(env, "Lib", "site-packages").ToLowerInvariant() } ); AssertUtil.DoesntContain( paths.Select(p => p.Path), Path.Combine(version.PrefixPath, "Lib", "site-packages").ToLowerInvariant() ); }
public async Task GetDatabasePaths() { foreach (var version in PythonPaths.Versions) { var paths = await PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync(version.InterpreterPath); AssertUtil.ContainsAtLeast( paths.Where(p => p.IsStandardLibrary).Select(p => p.Path), new[] { PathUtils.TrimEndSeparator(version.PrefixPath.ToLowerInvariant()), Path.Combine(version.PrefixPath, "Lib").ToLowerInvariant() } ); AssertUtil.ContainsAtLeast( paths.Where(p => !p.IsStandardLibrary).Select(p => p.Path), new[] { Path.Combine(version.PrefixPath, "Lib", "site-packages").ToLowerInvariant() } ); } }
protected virtual IEnumerable <string> GetCurrentSearchPaths() { if (Configuration.SearchPaths.Any()) { return(Configuration.SearchPaths); } if (!File.Exists(Configuration?.InterpreterPath)) { return(Enumerable.Empty <string>()); } try { return(PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync(Configuration.InterpreterPath) .WaitAndUnwrapExceptions() .MaybeEnumerate() .Select(p => p.Path)); } catch (InvalidOperationException) { return(Enumerable.Empty <string>()); } }