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;
            }));
        }
        private string[] GetMissingModules(HashSet <string> existingDatabase)
        {
            var searchPaths = PythonTypeDatabase.GetCachedDatabaseSearchPaths(DatabasePath);

            if (searchPaths == null)
            {
                // No cached search paths means our database is out of date.
                return(existingDatabase
                       .Except(RequiredBuiltinModules)
                       .OrderBy(name => name, StringComparer.InvariantCultureIgnoreCase)
                       .ToArray());
            }

            return(PythonTypeDatabase.GetDatabaseExpectedModules(_config.Version, searchPaths)
                   .SelectMany()
                   .Select(mp => mp.ModuleName)
                   .Concat(RequiredBuiltinModules)
                   .Where(m => !existingDatabase.Contains(m))
                   .OrderBy(name => name, StringComparer.InvariantCultureIgnoreCase)
                   .ToArray());
        }
        private string[] GetMissingModules(HashSet <string> existingDatabase)
        {
            List <PythonLibraryPath> searchPaths;

            try {
                searchPaths = PythonTypeDatabase.GetCachedDatabaseSearchPaths(DatabasePath);
            } catch (IOException) {
                return(existingDatabase
                       .Except(RequiredBuiltinModules)
                       .OrderBy(name => name, StringComparer.InvariantCultureIgnoreCase)
                       .ToArray());
            }

            return(PythonTypeDatabase.GetDatabaseExpectedModules(_config.Version, searchPaths)
                   .SelectMany()
                   .Select(mp => mp.ModuleName)
                   .Concat(RequiredBuiltinModules)
                   .Where(m => !existingDatabase.Contains(m))
                   .OrderBy(name => name, StringComparer.InvariantCultureIgnoreCase)
                   .ToArray());
        }
Exemplo n.º 4
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 finding      = new HashSet <string>(moduleNames);
            var found        = new HashSet <string>();
            var withPackages = factory.PackageManager;

            if (withPackages != null)
            {
                foreach (var m in finding)
                {
                    if ((await withPackages.GetInstalledPackageAsync(new PackageSpec(m), CancellationToken.None)).IsValid)
                    {
                        found.Add(m);
                    }
                }
                finding.ExceptWith(found);
                if (!finding.Any())
                {
                    // Found all of them, so stop searching
                    return(found);
                }
            }

            var withDb = factory as PythonInterpreterFactoryWithDatabase;

            if (withDb != null && withDb.IsCurrent)
            {
                var db = withDb.GetCurrentDatabase();
                found.UnionWith(finding.Where(m => db.GetModule(m) != null));

                // Always stop searching after this step
                return(found);
            }

            if (withDb != null)
            {
                try {
                    var paths = await PythonTypeDatabase.GetDatabaseSearchPathsAsync(withDb);

                    found.UnionWith(PythonTypeDatabase.GetDatabaseExpectedModules(withDb.Configuration.Version, paths)
                                    .SelectMany()
                                    .Select(g => g.ModuleName)
                                    .Where(m => finding.Contains(m)));
                } catch (InvalidOperationException) {
                }

                finding.ExceptWith(found);
                if (!finding.Any())
                {
                    // Found all of them, so stop searching
                    return(found);
                }
            }

            return(await Task.Run(() => {
                foreach (var mp in ModulePath.GetModulesInLib(factory.Configuration))
                {
                    if (finding.Remove(mp.ModuleName))
                    {
                        found.Add(mp.ModuleName);
                    }

                    if (!finding.Any())
                    {
                        break;
                    }
                }
                return found;
            }));
        }
Exemplo n.º 5
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;
            }));
        }