forked from updatecli/updatecli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependencies.go
More file actions
220 lines (184 loc) · 6.6 KB
/
dependencies.go
File metadata and controls
220 lines (184 loc) · 6.6 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package pyproject
import (
"bytes"
"fmt"
"path"
"path/filepath"
"sort"
"text/template"
"github.com/BurntSushi/toml"
"github.com/sirupsen/logrus"
"github.com/updatecli/updatecli/pkg/plugins/utils/version"
)
// pyprojectTOML mirrors the subset of pyproject.toml we care about.
type pyprojectTOML struct {
Project struct {
Name string `toml:"name"`
Dependencies []string `toml:"dependencies"`
OptionalDependencies map[string][]string `toml:"optional-dependencies"`
} `toml:"project"`
}
// loadPyprojectData reads and unmarshals a pyproject.toml file.
func loadPyprojectData(filePath string) (pyprojectTOML, error) {
var data pyprojectTOML
if _, err := toml.DecodeFile(filePath, &data); err != nil {
return data, fmt.Errorf("parsing %q: %w", filePath, err)
}
return data, nil
}
// discoverDependencyManifests is the main entry point called by DiscoverManifests.
func (p Pyproject) discoverDependencyManifests() ([][]byte, error) {
var manifests [][]byte
searchFromDir := p.rootDir
// spec.RootDir relative paths are joined onto rootDir; absolute ones were resolved in New().
if p.spec.RootDir != "" && !path.IsAbs(p.spec.RootDir) {
searchFromDir = filepath.Join(p.rootDir, p.spec.RootDir)
}
foundFiles, err := findPyprojectFiles(searchFromDir)
if err != nil {
return nil, err
}
for _, foundFile := range foundFiles {
logrus.Debugf("parsing file %q", foundFile)
dir := filepath.Dir(foundFile)
lockSupport, skip := detectLockFileSupport(dir, p.uvAvailable)
if skip {
continue
}
data, err := loadPyprojectData(foundFile)
if err != nil {
logrus.Debugln(err)
continue
}
relativeFile, err := filepath.Rel(p.rootDir, foundFile)
if err != nil {
logrus.Debugln(err)
continue
}
workdir, err := filepath.Rel(p.rootDir, dir)
if err != nil {
logrus.Debugln(err)
continue
}
projectName := data.Project.Name
// Process [project.dependencies] — the main dependency group.
mainDeps := make([]string, len(data.Project.Dependencies))
copy(mainDeps, data.Project.Dependencies)
sort.Strings(mainDeps)
manifests = append(manifests, p.processDependencies(mainDeps, "", relativeFile, lockSupport, projectName, workdir)...)
// Process each [project.optional-dependencies] group in deterministic order.
groups := make([]string, 0, len(data.Project.OptionalDependencies))
for g := range data.Project.OptionalDependencies {
groups = append(groups, g)
}
sort.Strings(groups)
for _, group := range groups {
groupDeps := make([]string, len(data.Project.OptionalDependencies[group]))
copy(groupDeps, data.Project.OptionalDependencies[group])
sort.Strings(groupDeps)
manifests = append(manifests, p.processDependencies(groupDeps, group, relativeFile, lockSupport, projectName, workdir)...)
}
}
logrus.Printf("%v manifests identified", len(manifests))
return manifests, nil
}
// processDependencies generates manifests for a list of PEP 508 dependency strings.
// group is the optional-dependency group name, or "" for main dependencies.
func (p Pyproject) processDependencies(
deps []string,
group string,
relativeFile string,
lockSupport lockFileSupport,
projectName string,
workdir string,
) [][]byte {
var manifests [][]byte
tmpl, err := template.New("manifest").Parse(manifestTemplate)
if err != nil {
logrus.Errorln(err)
return manifests
}
for _, depStr := range deps {
dep, err := parsePEP508(depStr)
if err != nil {
logrus.Warningf("skipping dependency %q from %q: %s", depStr, relativeFile, err)
continue
}
if len(p.spec.Ignore) > 0 && p.spec.Ignore.isMatchingRules(p.rootDir, relativeFile, dep.Name, dep.Version) {
logrus.Debugf("ignoring %q from %q as matching ignore rule(s)", dep.Name, relativeFile)
continue
}
if len(p.spec.Only) > 0 && !p.spec.Only.isMatchingRules(p.rootDir, relativeFile, dep.Name, dep.Version) {
logrus.Debugf("ignoring %q from %q as not matching only rule(s)", dep.Name, relativeFile)
continue
}
params := p.buildTemplateParams(dep, group, lockSupport, projectName, workdir)
var buf bytes.Buffer
if err := tmpl.Execute(&buf, params); err != nil {
logrus.Debugln(err)
continue
}
manifests = append(manifests, buf.Bytes())
}
return manifests
}
// buildTemplateParams constructs the manifestTemplateParams for a single dependency.
func (p Pyproject) buildTemplateParams(
dep pythonDependency,
group string,
lockSupport lockFileSupport,
projectName string,
workdir string,
) manifestTemplateParams {
// Build a human-readable manifest name that includes the optional group when set.
var manifestName string
if group == "" {
manifestName = fmt.Sprintf("deps(pypi): bump %q for %q project", dep.Name, projectName)
} else {
manifestName = fmt.Sprintf("deps(pypi): bump %q [%s] for %q project", dep.Name, group, projectName)
}
// Determine version filter.
//
// Priority:
// 1. User-specified VersionFilter from spec.
// 2. Constraint derived from the dependency itself (e.g. ">=2.28").
// 3. Wildcard "*" when no version information is present.
sourceVersionFilterKind := p.versionFilter.Kind
sourceVersionFilterPattern := p.versionFilter.Pattern
sourceVersionFilterRegex := p.versionFilter.Regex
if !p.spec.VersionFilter.IsZero() && dep.Version != "" {
var err error
sourceVersionFilterPattern, err = p.versionFilter.GreaterThanPattern(dep.Version)
if err != nil {
logrus.Debugf("building version filter pattern for %q: %s", dep.Name, err)
sourceVersionFilterPattern = p.versionFilter.Pattern
}
}
if p.spec.VersionFilter.IsZero() {
if dep.Constraint != "" {
sourceVersionFilterKind = version.PEP440VERSIONKIND
sourceVersionFilterPattern = dep.Constraint
} else {
sourceVersionFilterKind = version.PEP440VERSIONKIND
sourceVersionFilterPattern = "*"
}
}
relLockFile := filepath.Join(workdir, "uv.lock")
return manifestTemplateParams{
ManifestName: manifestName,
ActionID: p.actionID,
SourceID: dep.Name,
SourceName: fmt.Sprintf("Get latest %q package version", dep.Name),
SourceVersionFilterKind: sourceVersionFilterKind,
SourceVersionFilterPattern: sourceVersionFilterPattern,
SourceVersionFilterRegex: sourceVersionFilterRegex,
DependencyName: dep.Name,
IndexURL: p.spec.IndexURL,
TargetID: dep.Name,
TargetName: fmt.Sprintf("deps(pypi): bump %q to {{ source %q }}", dep.Name, dep.Name),
ScmID: p.scmID,
UvEnabled: lockSupport.uv,
LockFile: relLockFile,
Workdir: workdir,
}
}