-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyprojecthandler.go
More file actions
111 lines (86 loc) · 2.51 KB
/
pyprojecthandler.go
File metadata and controls
111 lines (86 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-FileCopyrightText: © 2026 Idiap Research Institute <contact@idiap.ch>
// SPDX-FileContributor: Samuel Gaist <samuel.gaist@idiap.ch>
//
// SPDX-License-Identifier: Apache-2.0
package pythonpackagers
import (
"fmt"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
pip "github.com/paketo-buildpacks/python-packagers/pkg/packagers/pip"
poetry "github.com/paketo-buildpacks/python-packagers/pkg/packagers/poetry"
uv "github.com/paketo-buildpacks/python-packagers/pkg/packagers/uv"
)
const (
Hatchling = "hatchling.build"
Setuptools = "setuptools.build_meta"
Flit = "flit_core.buildapi"
PDG = "pdm.backend"
UvBuild = "uv_build"
Poetry = "poetry.core.masonry.api"
ProjectFile = "pyproject.toml"
)
type BuildSystem struct {
Requires []string `toml:"requires"`
BuildBackend string `toml:"build-backend"`
}
type Project struct {
BuildSystem BuildSystem `toml:"build-system"`
}
type PyProjectHandler struct {
}
var InstallerMap = map[string]string{
Setuptools: pip.Pip,
Poetry: poetry.Poetry,
UvBuild: uv.UvPlanEntry,
"": poetry.Poetry, // To keep compatibility with original implementation and poetry v1
}
func NewPyProjectHandler() PyProjectHandler {
return PyProjectHandler{}
}
func (p *PyProjectHandler) GetBuildBackend(projectPath string) (string, error) {
var project Project
_, err := toml.DecodeFile(filepath.Join(projectPath, ProjectFile), &project)
if err != nil {
return "", err
}
return project.BuildSystem.BuildBackend, nil
}
func (p *PyProjectHandler) GetInstaller(projectPath string) (string, error) {
backend, err := p.GetBuildBackend(projectPath)
if err != nil {
return "", err
}
if backend == "" {
found, err := fs.Exists(filepath.Join(projectPath, uv.LockfileName))
if err != nil {
return "", err
}
if found {
backend = UvBuild
}
}
installer, ok := InstallerMap[backend]
if !ok {
return "", fmt.Errorf("unsupported backend: %s", backend)
}
return installer, nil
}
func (p *PyProjectHandler) Detect(installer string, context packit.DetectContext) (packit.DetectResult, error) {
var result packit.DetectResult
var err error
switch installer {
case pip.Pip:
result, err = pip.Detect()(context)
case poetry.Poetry:
result, err = poetry.Detect()(context)
case uv.UvPlanEntry:
result, err = uv.Detect()(context)
default:
result = packit.DetectResult{}
err = fmt.Errorf("unsupported installer: %s", installer)
}
return result, err
}