Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): add license support for requirement.txt files #6782

Merged
merged 24 commits into from
May 30, 2024

Conversation

DmitriyLewen
Copy link
Contributor

@DmitriyLewen DmitriyLewen commented May 27, 2024

Description

Add license support for requirement.txt files.

➜  trivy -q fs --scanners license ./pkg/fanal/analyzer/language/python/pip/testdata/happy 

requirements.txt (license)

Total: 1 (UNKNOWN: 1, LOW: 0, MEDIUM: 0, HIGH: 0, CRITICAL: 0)

┌─────────┬─────────────┬────────────────┬──────────┐
│ Package │   License   │ Classification │ Severity │
├─────────┼─────────────┼────────────────┼──────────┤
│ Flask   │ BSD License │ Non Standard   │ UNKNOWN  │
└─────────┴─────────────┴────────────────┴──────────┘

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@DmitriyLewen DmitriyLewen self-assigned this May 27, 2024
@DmitriyLewen DmitriyLewen changed the title feat(python): add lincense support for requirement.txt files feat(python): add license support for requirement.txt files May 27, 2024
@DmitriyLewen DmitriyLewen marked this pull request as ready for review May 27, 2024 11:11
@DmitriyLewen DmitriyLewen requested a review from knqyf263 as a code owner May 27, 2024 11:11
@DmitriyLewen DmitriyLewen marked this pull request as draft May 27, 2024 11:20
@DmitriyLewen DmitriyLewen marked this pull request as ready for review May 27, 2024 12:07
Comment on lines 219 to 244
sort.Slice(pythonDirs, func(i, j int) bool {
majorVer1, minorVer1, found1 := strings.Cut(pythonDirs[i], ".")
majorVer2, minorVer2, found2 := strings.Cut(pythonDirs[j], ".")

// if one of dir doesn't contain minor version => sort as strings
if !found1 || !found2 {
return pythonDirs[i] < pythonDirs[j]
}

// Sort major versions as strings
// e.g. `python2.7` and `python3.10`
if majorVer1 != majorVer2 {
return majorVer1 < majorVer2
}

// Convert minor versions
ver1, err1 := strconv.Atoi(minorVer1)
ver2, err2 := strconv.Atoi(minorVer2)

// If we can't convert minor versions => sort as strings
if err1 != nil || err2 != nil {
return pythonDirs[i] < pythonDirs[j]
}

return ver1 < ver2
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe go-version helps.
https://go.dev/play/p/Ytgevsntqr7

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm... i didn't think about this 👍
Updated in 226b3da

Comment on lines 212 to 217
var pythonDirs []string
for _, entry := range entries {
if entry.IsDir() && strings.HasPrefix(entry.Name(), "python") {
pythonDirs = append(pythonDirs, entry.Name())
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var pythonDirs []string
for _, entry := range entries {
if entry.IsDir() && strings.HasPrefix(entry.Name(), "python") {
pythonDirs = append(pythonDirs, entry.Name())
}
}
var pythonVers []version.Version
for _, entry := range entries {
if entry.IsDir() && strings.HasPrefix(entry.Name(), "python") {
ver := strings.TrimPrefix(entry.Name(), "python")
v, err := version.Parse(ver)
if err != nil {
log.Debug("...", log.Err(err))
continue
}
pythonVers = append(pythonVers, v)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knqyf263 knqyf263 enabled auto-merge May 30, 2024 07:41
@knqyf263 knqyf263 added this pull request to the merge queue May 30, 2024
github-merge-queue bot pushed a commit that referenced this pull request May 30, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 30, 2024
@knqyf263 knqyf263 added this pull request to the merge queue May 30, 2024
Merged via the queue into aquasecurity:main with commit 29615be May 30, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(pip): license support
2 participants