internal/deps/python.go

110 lines · 3226 bytes

  1package deps
  2
  3import (
  4	"strings"
  5
  6	"github.com/BurntSushi/toml"
  7)
  8
  9// parsePython reads requirements.txt and pyproject.toml. Only requirements
 10// that name one release are reported: a bare `requests` or a `>=2,<3` range
 11// says the project accepts whatever is current, so there is nothing to tell
 12// its maintainer.
 13func parsePython(read ReadFile) []Dep { return pythonDeps(read, pin) }
 14
 15// pythonDeps reads both files, reducing each requirement with pinned.
 16func pythonDeps(read ReadFile, pinned func(string) string) []Dep {
 17	seen := map[string]bool{}
 18	var out []Dep
 19	add := func(name, version string) {
 20		name = normalizePyPI(name)
 21		if name == "" || name == "python" || seen[name] {
 22			return
 23		}
 24		if v := pinned(version); v != "" {
 25			seen[name] = true
 26			out = append(out, Dep{Ecosystem: EcoPyPI, Name: name, Current: v})
 27		}
 28	}
 29	if raw, err := read("requirements.txt"); err == nil {
 30		for _, line := range strings.Split(string(raw), "\n") {
 31			if i := strings.Index(line, "#"); i >= 0 {
 32				line = line[:i]
 33			}
 34			line = strings.TrimSpace(line)
 35			if line == "" || strings.HasPrefix(line, "-") {
 36				continue
 37			}
 38			name, version := splitPEP508(line)
 39			add(name, version)
 40		}
 41	}
 42	if raw, err := read("pyproject.toml"); err == nil {
 43		pyprojectDeps(raw, add)
 44	}
 45	return out
 46}
 47
 48// pyprojectDeps adds the dependencies a pyproject.toml lists under
 49// [project] and [tool.poetry].
 50func pyprojectDeps(raw []byte, add func(name, version string)) {
 51	var doc struct {
 52		Project struct {
 53			Dependencies []string `toml:"dependencies"`
 54		} `toml:"project"`
 55		Tool struct {
 56			Poetry struct {
 57				Dependencies    map[string]any `toml:"dependencies"`
 58				DevDependencies map[string]any `toml:"dev-dependencies"`
 59			} `toml:"poetry"`
 60		} `toml:"tool"`
 61	}
 62	if toml.Unmarshal(raw, &doc) != nil {
 63		return
 64	}
 65	for _, spec := range doc.Project.Dependencies {
 66		name, version := splitPEP508(spec)
 67		add(name, version)
 68	}
 69	poetry := doc.Tool.Poetry
 70	for _, set := range []map[string]any{poetry.Dependencies, poetry.DevDependencies} {
 71		for name, spec := range set {
 72			// Poetry shares Cargo's two forms: a bare string, or a
 73			// table that may point somewhere other than PyPI.
 74			if req, ok := cargoRequirement(spec); ok {
 75				add(name, req)
 76			}
 77		}
 78	}
 79}
 80
 81// splitPEP508 separates the distribution name from its version specifier,
 82// dropping extras and environment markers: `django[bcrypt]==5.0 ; sys_platform
 83// != "win32"` becomes ("django", "==5.0").
 84func splitPEP508(spec string) (name, version string) {
 85	if i := strings.Index(spec, ";"); i >= 0 {
 86		spec = spec[:i]
 87	}
 88	spec = strings.TrimSpace(spec)
 89	i := strings.IndexAny(spec, "[<>=!~ ")
 90	if i < 0 {
 91		return spec, ""
 92	}
 93	name, version = spec[:i], spec[i:]
 94	if j := strings.Index(version, "]"); j >= 0 {
 95		version = version[j+1:]
 96	}
 97	return name, strings.TrimSpace(version)
 98}
 99
100// normalizePyPI applies PEP 503 name normalization, so `Flask_SQLAlchemy`
101// and `flask-sqlalchemy` are one dependency.
102func normalizePyPI(name string) string {
103	name = strings.TrimSpace(strings.ToLower(name))
104	name = strings.ReplaceAll(name, "_", "-")
105	name = strings.ReplaceAll(name, ".", "-")
106	for strings.Contains(name, "--") {
107		name = strings.ReplaceAll(name, "--", "-")
108	}
109	return name
110}