Skip to content

Commit

Permalink
Improves detection of PyPI package names in environment dependencies (#…
Browse files Browse the repository at this point in the history
…1699)

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

## Tests
Added unit tests
  • Loading branch information
andrewnester authored Aug 21, 2024
1 parent a4c1ba3 commit c775d25
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
22 changes: 18 additions & 4 deletions bundle/libraries/local_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libraries
import (
"net/url"
"path"
"regexp"
"strings"
)

Expand Down Expand Up @@ -65,14 +66,27 @@ func IsLibraryLocal(dep string) bool {
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 {
// If the dependency has ==, it's a package with version
if strings.Contains(name, "==") {
if packageRegex.MatchString(name) {
return true
}

// If the dependency has no extension, it's a PyPi package name
return path.Ext(name) == ""
return isUrlBasedLookup(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 {
Expand Down
9 changes: 9 additions & 0 deletions bundle/libraries/local_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ func TestIsLibraryLocal(t *testing.T) {
{path: "-r /Workspace/my_project/requirements.txt", expected: false},
{path: "s3://mybucket/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[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 {
Expand Down

0 comments on commit c775d25

Please sign in to comment.