Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily load python package mappings #51

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions internal/backends/python/gen_pypi_map/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ func main() {
// moduleToPypiPackage holds a map of all known modules to their corresponding
// best matching package. This helps us guess which packages should be installed
// for the given imports.
var moduleToPypiPackage = map[string]string{
var moduleToPypiPackageCached = map[string]string{}

func moduleToPypiPackage() map[string]string {
if len(moduleToPypiPackageCached) == 0 {
moduleToPypiPackageCached = map[string]string{
`)

addMap := func(mod, pkg, comment string) {
Expand Down Expand Up @@ -368,7 +372,7 @@ nextpkg:
}
}

fmt.Fprintf(outgo, "}\n")
fmt.Fprintf(outgo, "}\n}\nreturn moduleToPypiPackageCached }\n")

fmt.Fprintf(outgo, `
// pypiPackageToModules holds a map of every known python package to the modules
Expand All @@ -378,8 +382,11 @@ nextpkg:
//
// The module names are comma separated because go's compiler seems to vomit
// when you create too many slices.
var pypiPackageToModulesCached = map[string]string{}

var pypiPackageToModules = map[string]string{
func pypiPackageToModules() map[string]string {
if len(pypiPackageToModulesCached) == 0 {
pypiPackageToModulesCached = map[string]string{
`)

for _, pkg := range pkgs {
Expand All @@ -403,7 +410,7 @@ var pypiPackageToModules = map[string]string{
fmt.Fprintf(outgo, "\n")
}

fmt.Fprintf(outgo, "}\n")
fmt.Fprintf(outgo, "}\n}\nreturn pypiPackageToModulesCached\n}")

err = outgo.Close()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/backends/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func guess(python string) (map[api.PkgName]bool, bool) {

if knownPkgs, err := listSpecfile(); err == nil {
for pkgName := range knownPkgs {
mods, ok := pypiPackageToModules[string(pkgName)]
mods, ok := pypiPackageToModules()[string(pkgName)]
if ok {
for _, mod := range strings.Split(mods, ",") {
availMods[mod] = true
Expand All @@ -409,13 +409,13 @@ func guess(python string) (map[api.PkgName]bool, bool) {
}

// If this module has a package pragma, use that
if pragmas.Package != ""{
if pragmas.Package != "" {
name := api.PkgName(pragmas.Package)
pkgs[normalizePackageName(name)] = true

} else {
// Otherwise, try and look it up in Pypi
pkg, ok := moduleToPypiPackage[modname]
pkg, ok := moduleToPypiPackage()[modname]
if ok {
name := api.PkgName(pkg)
pkgs[normalizePackageName(name)] = true
Expand Down