From 11a51fb1796546b0a49de6e700784b187ea2a905 Mon Sep 17 00:00:00 2001 From: mcoops Date: Mon, 4 Oct 2021 02:33:54 +1000 Subject: [PATCH] python: improve name and version parsing (#47) --- deplist_test.go | 6 ++++++ internal/scan/python.go | 14 ++++++++++++-- test/testRepo/requirements.txt | 3 +++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/deplist_test.go b/deplist_test.go index 2b8f8a0c..ad28982c 100644 --- a/deplist_test.go +++ b/deplist_test.go @@ -234,6 +234,9 @@ func BuildWant() []Dependency { "cotyledon", "Flask", "kuryr-lib", + "docutils", + "python-dateutil", + "unittest2", "cryptography", } @@ -274,6 +277,9 @@ func BuildWant() []Dependency { deps = append(deps, d) } + end = len(deps) - 2 // get the cryptography ver + deps[end].Version = "0.5.1" + end = len(deps) - 1 // get the cryptography ver deps[end].Version = "2.3.0" diff --git a/internal/scan/python.go b/internal/scan/python.go index 433a779e..0cf73180 100755 --- a/internal/scan/python.go +++ b/internal/scan/python.go @@ -31,14 +31,24 @@ func GetPythonDeps(path string) (map[string]string, error) { line := scanner.Text() // skip comments - if strings.HasPrefix(line, "#") || line == "" { + // and editable? https://github.com/pypa/pip/issues/4812 + if strings.HasPrefix(line, "#") || strings.HasPrefix(line, "-e") || line == "" { continue } // easy case, elasticsearch-curator==5.8.1 // record name and version, only for == - idx := strings.LastIndex(line, "==") + idx := strings.Index(line, "==") if idx > 0 { + // test if there's a ';', i.e. unittest2==0.5.1; python_version == '2.6' + colIdx := strings.Index(line, ";") + if colIdx > 0 { + if idx+2 >= colIdx { + continue + } + // truncate line + line = line[:colIdx] + } gathered[line[:idx]] = line[idx+2:] continue } diff --git a/test/testRepo/requirements.txt b/test/testRepo/requirements.txt index d116f28f..fc2d6d78 100644 --- a/test/testRepo/requirements.txt +++ b/test/testRepo/requirements.txt @@ -5,4 +5,7 @@ cotyledon>=1.5.0 # Apache-2.0 Flask!=0.11,>=0.12.3 # BSD kuryr-lib>=0.5.0 # Apache-2.0 +unittest2==0.5.1; python_version == '2.6' +python-dateutil>=2.1,<3.0.0; python_version>="2.7" +docutils>=0.10 cryptography==2.3.0