Improves detection of PyPI package names in environment dependencies (#1699)

## Changes
Improves detection of PyPi package names in environment dependencies

## Tests
Added unit tests
This commit is contained in:
Andrew Nester 2024-08-21 10:22:35 +02:00 committed by GitHub
parent a4c1ba3e28
commit c775d251ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package libraries
import ( import (
"net/url" "net/url"
"path" "path"
"regexp"
"strings" "strings"
) )
@ -65,14 +66,27 @@ func IsLibraryLocal(dep string) bool {
return IsLocalPath(dep) return IsLocalPath(dep)
} }
// ^[a-zA-Z0-9\-_]+: Matches the package name, allowing alphanumeric characters, dashes (-), and underscores (_).
// \[.*\])?: Optionally matches any extras specified in square brackets, e.g., [security].
// ((==|!=|<=|>=|~=|>|<)\d+(\.\d+){0,2}(\.\*)?)?: Optionally matches version specifiers, supporting various operators (==, !=, etc.) followed by a version number (e.g., 2.25.1).
// Spec for package name and version specifier: https://pip.pypa.io/en/stable/reference/requirement-specifiers/
var packageRegex = regexp.MustCompile(`^[a-zA-Z0-9\-_]+\s?(\[.*\])?\s?((==|!=|<=|>=|~=|==|>|<)\s?\d+(\.\d+){0,2}(\.\*)?)?$`)
func isPackage(name string) bool { func isPackage(name string) bool {
// If the dependency has ==, it's a package with version if packageRegex.MatchString(name) {
if strings.Contains(name, "==") {
return true return true
} }
// If the dependency has no extension, it's a PyPi package name return isUrlBasedLookup(name)
return path.Ext(name) == "" }
func isUrlBasedLookup(name string) bool {
parts := strings.Split(name, " @ ")
if len(parts) != 2 {
return false
}
return packageRegex.MatchString(parts[0]) && isRemoteStorageScheme(parts[1])
} }
func isRemoteStorageScheme(path string) bool { func isRemoteStorageScheme(path string) bool {

View File

@ -54,7 +54,16 @@ func TestIsLibraryLocal(t *testing.T) {
{path: "-r /Workspace/my_project/requirements.txt", expected: false}, {path: "-r /Workspace/my_project/requirements.txt", expected: false},
{path: "s3://mybucket/path/to/package", expected: false}, {path: "s3://mybucket/path/to/package", expected: false},
{path: "dbfs:/mnt/path/to/package", expected: false}, {path: "dbfs:/mnt/path/to/package", expected: false},
{path: "beautifulsoup4", expected: false},
{path: "beautifulsoup4==4.12.3", expected: false}, {path: "beautifulsoup4==4.12.3", expected: false},
{path: "beautifulsoup4 >= 4.12.3", expected: false},
{path: "beautifulsoup4 < 4.12.3", expected: false},
{path: "beautifulsoup4 ~= 4.12.3", expected: false},
{path: "beautifulsoup4[security, tests]", expected: false},
{path: "beautifulsoup4[security, tests] ~= 4.12.3", expected: false},
{path: "https://github.com/pypa/pip/archive/22.0.2.zip", expected: false},
{path: "pip @ https://github.com/pypa/pip/archive/22.0.2.zip", expected: false},
{path: "requests [security] @ https://github.com/psf/requests/archive/refs/heads/main.zip", expected: false},
} }
for i, tc := range testCases { for i, tc := range testCases {