From 7efa45177482e1d3ee3fc41ca910b3948aaef8f7 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 2 Jan 2024 12:58:05 -0800 Subject: [PATCH] Filter pip freeze based on normalized form pip freeze output is actually pip's internal canonicalized form, which can be different from the normalized package name format. Example: `discord.py` gets normalized into `discord-py`, but `pip show discord-py` still outputs `Name: discord.py`. --- internal/backends/python/python.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/backends/python/python.go b/internal/backends/python/python.go index 7839300d..e23ba3aa 100644 --- a/internal/backends/python/python.go +++ b/internal/backends/python/python.go @@ -388,15 +388,24 @@ func makePythonPipBackend(python string) api.LanguageBackend { util.Die("failed to run freeze: %s", err.Error()) } + // As we walk through the output of pip freeze, + // compare the package metadata name to the normalized + // pkgs that we are trying to install, to see which we + // want to track in `requirements.txt`. + normalizedPkgs := make(map[api.PkgName]bool) + for name := range pkgs { + normalizedPkgs[normalizePackageName(name)] = true + } + var toAppend []string - for _, line := range strings.Split(string(outputB), "\n") { + for _, canonicalSpec := range strings.Split(string(outputB), "\n") { var name api.PkgName - matches := matchPackageAndSpec.FindSubmatch(([]byte)(line)) + matches := matchPackageAndSpec.FindSubmatch(([]byte)(canonicalSpec)) if len(matches) > 0 { name = normalizePackageName(api.PkgName(string(matches[1]))) } - if _, exists := pkgs[name]; exists { - toAppend = append(toAppend, line) + if normalizedPkgs[name] { + toAppend = append(toAppend, canonicalSpec) } }