Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
            });
        }