/
utils.go
319 lines (284 loc) · 12.5 KB
/
utils.go
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package pythonutils
import (
"errors"
"fmt"
"net/url"
"path/filepath"
"regexp"
"strings"
"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/build-info-go/utils"
"github.com/jfrog/gofrog/io"
gofrogcmd "github.com/jfrog/gofrog/io"
)
const (
Pip PythonTool = "pip"
Pipenv PythonTool = "pipenv"
Poetry PythonTool = "poetry"
startDownloadingPattern = `^\s*Downloading\s`
downloadingCaptureGroup = `[^\s]*`
startUsingCachedPattern = `^\s*Using\scached\s`
usingCacheCaptureGroup = `[\S]+`
endPattern = `\s\(`
)
type PythonTool string
// Parse pythonDependencyPackage list to dependencies map. (mapping dependency to his child deps)
// Also returns a list of project's root dependencies
func parseDependenciesToGraph(packages []pythonDependencyPackage) (map[string][]string, []string, error) {
// Create packages map.
packagesMap := map[string][]string{}
allSubPackages := map[string]bool{}
for _, pkg := range packages {
var subPackages []string
for _, subPkg := range pkg.Dependencies {
subPkgFullName := subPkg.Key + ":" + subPkg.InstalledVersion
subPackages = append(subPackages, subPkgFullName)
allSubPackages[subPkgFullName] = true
}
packagesMap[pkg.Package.Key+":"+pkg.Package.InstalledVersion] = subPackages
}
var topLevelPackagesList []string
for pkgName := range packagesMap {
if !allSubPackages[pkgName] {
topLevelPackagesList = append(topLevelPackagesList, pkgName)
}
}
return packagesMap, topLevelPackagesList, nil
}
// Structs for parsing the pip-dependency-map result.
type pythonDependencyPackage struct {
Package packageType `json:"package,omitempty"`
Dependencies []packageType `json:"dependencies,omitempty"`
}
type packageType struct {
Key string `json:"key,omitempty"`
PackageName string `json:"package_name,omitempty"`
InstalledVersion string `json:"installed_version,omitempty"`
}
func GetPythonDependenciesFiles(tool PythonTool, args []string, buildName, buildNumber string, log utils.Log, srcPath string) (map[string]entities.Dependency, error) {
switch tool {
case Pip, Pipenv:
return InstallWithLogParsing(tool, args, log, srcPath)
case Poetry:
if buildName != "" && buildNumber != "" {
log.Warn("Poetry commands are not supporting collecting dependencies files")
}
return make(map[string]entities.Dependency), nil
default:
return nil, errors.New(string(tool) + " commands are not supported.")
}
}
func GetPythonDependencies(tool PythonTool, srcPath, localDependenciesPath string) (dependenciesGraph map[string][]string, topLevelDependencies []string, err error) {
switch tool {
case Pip:
return getPipDependencies(srcPath, localDependenciesPath)
case Pipenv:
return getPipenvDependencies(srcPath)
case Poetry:
return getPoetryDependencies(srcPath)
default:
return nil, nil, errors.New(string(tool) + " commands are not supported.")
}
}
func GetPackageName(tool PythonTool, srcPath string) (packageName string, err error) {
switch tool {
case Pip, Pipenv:
return getPackageNameFromSetuppy(srcPath)
case Poetry:
packageName, _, err = getPackageNameFromPyproject(srcPath)
return
default:
return "", errors.New(string(tool) + " commands are not supported.")
}
}
// Before running this function, dependency IDs may be the file names of the resolved python packages.
// Update build info dependency IDs and the requestedBy field.
// allDependencies - Dependency name to Dependency map
// dependenciesGraph - Dependency graph as built by 'pipdeptree' or 'pipenv graph'
// topLevelPackagesList - The direct dependencies
// packageName - The resolved package name of the Python project, may be empty if we couldn't resolve it
// moduleName - The input module name from the user, or the packageName
func UpdateDepsIdsAndRequestedBy(dependenciesMap map[string]entities.Dependency, dependenciesGraph map[string][]string, topLevelPackagesList []string, packageName, moduleName string) {
if packageName == "" {
// Projects without setup.py
dependenciesGraph[moduleName] = topLevelPackagesList
} else if packageName != moduleName {
// Projects with setup.py
dependenciesGraph[moduleName] = dependenciesGraph[packageName]
}
rootModule := entities.Dependency{Id: moduleName, RequestedBy: [][]string{{}}}
updateDepsIdsAndRequestedBy(rootModule, dependenciesMap, dependenciesGraph)
}
func updateDepsIdsAndRequestedBy(parentDependency entities.Dependency, dependenciesMap map[string]entities.Dependency, dependenciesGraph map[string][]string) {
for _, childId := range dependenciesGraph[parentDependency.Id] {
childName := childId[0:strings.Index(childId, ":")]
if childDep, ok := dependenciesMap[childName]; ok {
if childDep.NodeHasLoop() || len(childDep.RequestedBy) >= entities.RequestedByMaxLength {
continue
}
// Update RequestedBy field from parent's RequestedBy.
childDep.UpdateRequestedBy(parentDependency.Id, parentDependency.RequestedBy)
// Set dependency type
if childDep.Type == "" {
fileType := ""
if i := strings.LastIndex(childDep.Id, ".tar."); i != -1 {
fileType = childDep.Id[i+1:]
} else if i := strings.LastIndex(childDep.Id, "."); i != -1 {
fileType = childDep.Id[i+1:]
}
childDep.Type = fileType
}
// Convert Id field from filename to dependency id
childDep.Id = childId
// Reassign map entry with new entry copy
dependenciesMap[childName] = childDep
// Run recursive call on child dependencies
updateDepsIdsAndRequestedBy(childDep, dependenciesMap, dependenciesGraph)
}
}
}
func getFilePath(srcPath, fileName string) (string, error) {
filePath := filepath.Join(srcPath, fileName)
// Check if fileName exists.
validPath, err := utils.IsFileExists(filePath, false)
if err != nil || !validPath {
return "", err
}
return filePath, nil
}
// Create the CmdOutputPattern objects that can capture group content that may span multiple lines for logs that have line size limitations.
// Since the log parser parse line by line, we need to create a parser that can capture group content that may span multiple lines.
func getMultilineSplitCaptureOutputPattern(startCollectingPattern, captureGroup, endCollectingPattern string, handler func(pattern *gofrogcmd.CmdOutputPattern) (string, error)) (parsers []*gofrogcmd.CmdOutputPattern) {
// Prepare regex patterns.
oneLineRegex := regexp.MustCompile(startCollectingPattern + `(` + captureGroup + `)` + endCollectingPattern)
startCollectionRegexp := regexp.MustCompile(startCollectingPattern)
endCollectionRegexp := regexp.MustCompile(endCollectingPattern)
// Create a parser for single line pattern matches.
parsers = append(parsers, &gofrogcmd.CmdOutputPattern{RegExp: oneLineRegex, ExecFunc: handler})
// Create a parser for multi line pattern matches.
lineBuffer := ""
collectingMultiLineValue := false
parsers = append(parsers, &gofrogcmd.CmdOutputPattern{RegExp: regexp.MustCompile(".*"), ExecFunc: func(pattern *gofrogcmd.CmdOutputPattern) (string, error) {
// Check if the line matches the startCollectingPattern.
if !collectingMultiLineValue && startCollectionRegexp.MatchString(pattern.Line) {
// Start collecting lines.
collectingMultiLineValue = true
lineBuffer = pattern.Line
// We assume that the content is multiline so no need to check end at this point.
// Single line will be handled and matched by the other parser.
return pattern.Line, nil
}
if !collectingMultiLineValue {
return pattern.Line, nil
}
// Add the line content to the buffer.
lineBuffer += pattern.Line
// Check if the line matches the endCollectingPattern.
if endCollectionRegexp.MatchString(pattern.Line) {
collectingMultiLineValue = false
// Simulate a one line content check to make sure we have regex match.
if oneLineRegex.MatchString(lineBuffer) {
return handler(&gofrogcmd.CmdOutputPattern{Line: pattern.Line, MatchedResults: oneLineRegex.FindStringSubmatch(lineBuffer)})
}
}
return pattern.Line, nil
}})
return
}
func InstallWithLogParsing(tool PythonTool, commandArgs []string, log utils.Log, srcPath string) (map[string]entities.Dependency, error) {
if tool == Pipenv {
// Add verbosity flag to pipenv commands to collect necessary data
commandArgs = append(commandArgs, "-v")
}
installCmd := io.NewCommand(string(tool), "install", commandArgs)
installCmd.Dir = srcPath
dependenciesMap := map[string]entities.Dependency{}
parsers := []*gofrogcmd.CmdOutputPattern{}
var packageName string
expectingPackageFilePath := false
// Extract downloaded package name.
parsers = append(parsers, &gofrogcmd.CmdOutputPattern{
RegExp: regexp.MustCompile(`^Collecting\s(\w[\w-.]+)`),
ExecFunc: func(pattern *gofrogcmd.CmdOutputPattern) (string, error) {
// If this pattern matched a second time before downloaded-file-name was found, prompt a message.
if expectingPackageFilePath {
// This may occur when a package-installation file is saved in pip-cache-dir, thus not being downloaded during the installation.
// Re-running pip-install with 'no-cache-dir' fixes this issue.
log.Debug(fmt.Sprintf("Could not resolve download path for package: %s, continuing...", packageName))
// Save package with empty file path.
dependenciesMap[strings.ToLower(packageName)] = entities.Dependency{Id: ""}
}
// Check for out of bound results.
if len(pattern.MatchedResults)-1 <= 0 {
log.Debug(fmt.Sprintf("Failed extracting package name from line: %s", pattern.Line))
return pattern.Line, nil
}
// Save dependency information.
expectingPackageFilePath = true
packageName = pattern.MatchedResults[1]
return pattern.Line, nil
},
})
saveCaptureGroupAsDependencyInfo := func(pattern *gofrogcmd.CmdOutputPattern) (string, error) {
fileName := extractFileNameFromRegexCaptureGroup(pattern)
if fileName == "" {
log.Debug(fmt.Sprintf("Failed extracting download path from line: %s", pattern.Line))
return pattern.Line, nil
}
// If this pattern matched before package-name was found, do not collect this path.
if !expectingPackageFilePath {
log.Debug(fmt.Sprintf("Could not resolve package name for download path: %s , continuing...", packageName))
return pattern.Line, nil
}
// Save dependency information.
dependenciesMap[strings.ToLower(packageName)] = entities.Dependency{Id: fileName}
expectingPackageFilePath = false
log.Debug(fmt.Sprintf("Found package: %s installed with: %s", packageName, fileName))
return pattern.Line, nil
}
// Extract downloaded file, stored in Artifactory. (value at log may be split into multiple lines)
parsers = append(parsers, getMultilineSplitCaptureOutputPattern(startDownloadingPattern, downloadingCaptureGroup, endPattern, saveCaptureGroupAsDependencyInfo)...)
// Extract cached file, stored in Artifactory. (value at log may be split into multiple lines)
parsers = append(parsers, getMultilineSplitCaptureOutputPattern(startUsingCachedPattern, usingCacheCaptureGroup, endPattern, saveCaptureGroupAsDependencyInfo)...)
// Extract already installed packages names.
parsers = append(parsers, &gofrogcmd.CmdOutputPattern{
RegExp: regexp.MustCompile(`^Requirement\salready\ssatisfied:\s(\w[\w-.]+)`),
ExecFunc: func(pattern *gofrogcmd.CmdOutputPattern) (string, error) {
// Check for out of bound results.
if len(pattern.MatchedResults)-1 < 0 {
log.Debug(fmt.Sprintf("Failed extracting package name from line: %s", pattern.Line))
return pattern.Line, nil
}
// Save dependency with empty file name.
dependenciesMap[strings.ToLower(pattern.MatchedResults[1])] = entities.Dependency{Id: ""}
log.Debug(fmt.Sprintf("Found package: %s already installed", pattern.MatchedResults[1]))
return pattern.Line, nil
},
})
// Execute command.
_, errorOut, _, err := gofrogcmd.RunCmdWithOutputParser(installCmd, true, parsers...)
if err != nil {
return nil, fmt.Errorf("failed running %s command with error: '%s - %s'", string(tool), err.Error(), errorOut)
}
return dependenciesMap, nil
}
func extractFileNameFromRegexCaptureGroup(pattern *gofrogcmd.CmdOutputPattern) (fileName string) {
// Check for out of bound results (no captures).
if len(pattern.MatchedResults) <= 1 {
return ""
}
// Extract file information from capture group.
filePath := pattern.MatchedResults[1]
lastSlashIndex := strings.LastIndex(filePath, "/")
if lastSlashIndex == -1 {
return filePath
}
lastComponent := filePath[lastSlashIndex+1:]
// Unescape the last component, for example 'PyYAML-5.1.2%2Bsp1.tar.gz' -> 'PyYAML-5.1.2+sp1.tar.gz'.
unescapedComponent, _ := url.QueryUnescape(lastComponent)
if unescapedComponent == "" {
// Couldn't escape, will use the raw string
return lastComponent
}
return unescapedComponent
}