| | | 1 | | package repometa |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "io/fs" |
| | | 5 | | "path/filepath" |
| | | 6 | | |
| | | 7 | | "github.com/BurntSushi/toml" |
| | | 8 | | ) |
| | | 9 | | |
| | | 10 | | type pythonDetector struct{} |
| | | 11 | | |
| | | 12 | | type pyprojectTOML struct { |
| | | 13 | | Tool struct { |
| | | 14 | | Uv *struct { |
| | | 15 | | Workspace *struct { |
| | | 16 | | Members []string `toml:"members"` |
| | | 17 | | } `toml:"workspace"` |
| | | 18 | | } `toml:"uv"` |
| | | 19 | | Poetry *struct{} `toml:"poetry"` |
| | | 20 | | } `toml:"tool"` |
| | | 21 | | Project *struct { |
| | | 22 | | Name string `toml:"name"` |
| | | 23 | | } `toml:"project"` |
| | | 24 | | } |
| | | 25 | | |
| | 84 | 26 | | func (pythonDetector) detect(dv dirVisit, cfg options) []finding { |
| | 84 | 27 | | // Any of these markers makes the directory a Python package for our |
| | 84 | 28 | | // purposes. pyproject.toml wins the "primary" evidence slot when |
| | 84 | 29 | | // present because it is the modern canonical form. |
| | 84 | 30 | | markers := []string{"pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile"} |
| | 84 | 31 | | primary, ok := firstFile(dv.files, markers...) |
| | 74 | 32 | | if !ok { |
| | 74 | 33 | | return nil |
| | 74 | 34 | | } |
| | | 35 | | |
| | 10 | 36 | | attrs := detectPythonPM(dv.files) |
| | 0 | 37 | | if attrs == nil { |
| | 0 | 38 | | attrs = map[string]string{} |
| | 0 | 39 | | } |
| | 10 | 40 | | evidence := []Evidence{{Path: relJoin(dv.rel, primary), Reason: "Python packaging marker"}} |
| | 10 | 41 | | var workspaces []Workspace |
| | 10 | 42 | | |
| | 10 | 43 | | confidence := 1.0 |
| | 8 | 44 | | if primary == "pyproject.toml" { |
| | 8 | 45 | | data, err := readManifest(filepath.Join(dv.abs, "pyproject.toml"), cfg) |
| | 8 | 46 | | switch { |
| | 0 | 47 | | case err != nil: |
| | 0 | 48 | | confidence = confidenceUnreadable |
| | 0 | 49 | | evidence = append(evidence, evidenceUnreadable(dv.rel, "pyproject.toml", err)) |
| | 8 | 50 | | default: |
| | 8 | 51 | | var pp pyprojectTOML |
| | 1 | 52 | | if uerr := toml.Unmarshal(data, &pp); uerr != nil { |
| | 1 | 53 | | confidence = confidenceUnparsable |
| | 1 | 54 | | evidence = append(evidence, evidenceUnparsable(dv.rel, "pyproject.toml", uerr)) |
| | 1 | 55 | | break |
| | | 56 | | } |
| | 1 | 57 | | if pp.Tool.Uv != nil && pp.Tool.Uv.Workspace != nil { |
| | 1 | 58 | | members := expandMembers(dv.abs, pp.Tool.Uv.Workspace.Members, dv.rel) |
| | 1 | 59 | | workspaces = append(workspaces, Workspace{Kind: WorkspaceUv, Members: members}) |
| | 1 | 60 | | evidence = append(evidence, Evidence{ |
| | 1 | 61 | | Path: relJoin(dv.rel, "pyproject.toml"), |
| | 1 | 62 | | Reason: "pyproject.toml declares [tool.uv.workspace]", |
| | 1 | 63 | | }) |
| | 1 | 64 | | } |
| | 1 | 65 | | if pp.Tool.Poetry != nil { |
| | 1 | 66 | | // Poetry is not a workspace tool in v0 — recorded as |
| | 1 | 67 | | // package-manager attribute only. |
| | 1 | 68 | | attrs["python.pm"] = "poetry" |
| | 1 | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 10 | 73 | | return []finding{{ |
| | 10 | 74 | | Kind: KindPythonPackage, |
| | 10 | 75 | | Confidence: confidence, |
| | 10 | 76 | | Evidence: evidence, |
| | 10 | 77 | | Workspaces: workspaces, |
| | 10 | 78 | | Attributes: attrs, |
| | 10 | 79 | | }} |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // detectPythonPM returns the most specific package-manager attribution |
| | | 83 | | // available from lockfiles / marker files in the directory. Order reflects |
| | | 84 | | // modern preference: uv > poetry > pipenv > pip. The result may be empty |
| | | 85 | | // when no lockfile or requirements.txt is present. |
| | | 86 | | func detectPythonPM(files []fs.DirEntry) map[string]string { |
| | | 87 | | attrs := map[string]string{} |
| | | 88 | | switch { |
| | | 89 | | case hasFile(files, "uv.lock"): |
| | | 90 | | attrs["python.pm"] = "uv" |
| | | 91 | | case hasFile(files, "poetry.lock"): |
| | | 92 | | attrs["python.pm"] = "poetry" |
| | | 93 | | case hasFile(files, "Pipfile.lock") || hasFile(files, "Pipfile"): |
| | | 94 | | attrs["python.pm"] = "pipenv" |
| | | 95 | | case hasFile(files, "requirements.txt"): |
| | | 96 | | attrs["python.pm"] = "pip" |
| | | 97 | | } |
| | | 98 | | return attrs |
| | | 99 | | } |