Files
aquasecurity-trivy/pkg/dependency/parser/python/python.go
2025-08-01 08:04:31 +00:00

23 lines
749 B
Go

package python
import (
"regexp"
"strings"
)
var normalizePkgNameRegexp = regexp.MustCompile(`[-_.]+`)
// NormalizePkgName normalizes the package name based on pep-0503 (with the option to disable conversion to lowercase).
// cf. https://peps.python.org/pep-0503/#normalized-names:
// The name should be lowercased with all runs of the characters ., -, or _ replaced with a single - character.
func NormalizePkgName(name string, inLowerCase bool) string {
name = normalizePkgNameRegexp.ReplaceAllString(name, "-")
// pep-0503 requires that all packages names MUST be lowercase.
// But there are cases where the original case should be preserved (e.g. dist-info dir names).
if inLowerCase {
name = strings.ToLower(name)
}
return name
}