Exemplo n.º 1
0
        /// <summary>
        /// Determines whether the interpreter factory contains the specified
        /// modules.
        /// </summary>
        /// <returns>The names of the modules that were found.</returns>
        public static async Task <HashSet <string> > FindModulesAsync(this IPythonInterpreterFactory factory, params string[] moduleNames)
        {
            var withDb = factory as PythonInterpreterFactoryWithDatabase;

            if (withDb != null && withDb.IsCurrent)
            {
                var db  = withDb.GetCurrentDatabase();
                var set = new HashSet <string>(moduleNames.Where(m => db.GetModule(m) != null));
                return(set);
            }

            var expected = new HashSet <string>(moduleNames);

            if (withDb != null)
            {
                var paths = PythonTypeDatabase.GetCachedDatabaseSearchPaths(withDb.DatabasePath) ??
                            await PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync(withDb.Configuration.InterpreterPath).ConfigureAwait(false);

                var db = PythonTypeDatabase.GetDatabaseExpectedModules(withDb.Configuration.Version, paths)
                         .SelectMany()
                         .Select(g => g.ModuleName);
                expected.IntersectWith(db);
                return(expected);
            }

            return(await Task.Run(() => {
                var result = new HashSet <string>();
                foreach (var mp in ModulePath.GetModulesInLib(factory))
                {
                    if (expected.Count == 0)
                    {
                        break;
                    }

                    if (expected.Remove(mp.ModuleName))
                    {
                        result.Add(mp.ModuleName);
                    }
                }
                return result;
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines whether the interpreter factory contains the specified
        /// modules.
        /// </summary>
        /// <returns>The names of the modules that were found.</returns>
        public static async Task <HashSet <string> > FindModulesAsync(this IPythonInterpreterFactory factory, params string[] moduleNames)
        {
            var withPackages = factory as IPackageManager;

            if (withPackages != null)
            {
                var res = new HashSet <string>();
                foreach (var m in moduleNames)
                {
                    if ((await withPackages.GetInstalledPackageAsync(new PackageSpec(m), CancellationToken.None)).IsValid)
                    {
                        res.Add(m);
                    }
                }
                if (res.SetEquals(moduleNames))
                {
                    return(res);
                }
            }

            var withDb = factory as PythonInterpreterFactoryWithDatabase;

            if (withDb != null && withDb.IsCurrent)
            {
                var db  = withDb.GetCurrentDatabase();
                var set = new HashSet <string>(moduleNames.Where(m => db.GetModule(m) != null));
                return(set);
            }

            var expected = new HashSet <string>(moduleNames);

            if (withDb != null)
            {
                try {
                    var paths = PythonTypeDatabase.GetCachedDatabaseSearchPaths(withDb.DatabasePath) ??
                                await PythonTypeDatabase.GetUncachedDatabaseSearchPathsAsync(withDb.Configuration.InterpreterPath).ConfigureAwait(false);

                    var db = PythonTypeDatabase.GetDatabaseExpectedModules(withDb.Configuration.Version, paths)
                             .SelectMany()
                             .Select(g => g.ModuleName);
                    expected.IntersectWith(db);
                    return(expected);
                } catch (InvalidOperationException) {
                }
            }

            return(await Task.Run(() => {
                var result = new HashSet <string>();
                foreach (var mp in ModulePath.GetModulesInLib(factory.Configuration))
                {
                    if (expected.Count == 0)
                    {
                        break;
                    }

                    if (expected.Remove(mp.ModuleName))
                    {
                        result.Add(mp.ModuleName);
                    }
                }
                return result;
            }));
        }