-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): rewrite requirements parser
- Loading branch information
1 parent
7332eb6
commit 1fdbb8a
Showing
3 changed files
with
56 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,66 @@ | ||
package python | ||
|
||
import ( | ||
"github.com/dlclark/regexp2" | ||
"github.com/murphysecurity/murphysec/utils/must" | ||
"github.com/repeale/fp-go" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const pyName = `(?:[A-Za-z0-9][A-Za-z0-9._-]*[A-Za-z0-9]|[A-Za-z0-9])` | ||
const pyVersion = `(?:[A-Za-z0-9_.!-]+)` | ||
const pyVersionOp = `(?<![=!<>])(?:=|<=|==|>=|===)` | ||
const pyVersionSeg = pyVersionOp + `\s*['""]?(` + pyVersion + `)` | ||
|
||
var pyVersionSegPattern = regexp2.MustCompile(pyVersionSeg, regexp2.Compiled) | ||
var pyNamePrefixPattern = regexp.MustCompile("^" + pyName) | ||
|
||
func parseRequirements(data string) map[string]string { | ||
var pattern = regexp.MustCompile(`^([\w_.-]+)[>=<]+([\w.]+)$`) | ||
var deps = make(map[string]string) | ||
for _, s := range strings.Split(data, "\n") { | ||
s = strings.TrimSpace(s) | ||
m := pattern.FindStringSubmatch(s) | ||
if m == nil { | ||
continue | ||
var lines []string | ||
var lineContinuation = false | ||
for _, s := range fp.Map(func(s string) string { return strings.TrimRight(s, "\r") })(strings.Split(data, "\n")) { | ||
s = strings.TrimRight(s, "\r") | ||
var currentLine = strings.TrimSuffix(s, "\\") | ||
if lineContinuation { | ||
lines[len(lines)-1] = lines[len(lines)-1] + currentLine | ||
} else { | ||
lines = append(lines, currentLine) | ||
} | ||
lineContinuation = strings.HasSuffix(s, "\\") | ||
} | ||
lines = fp.Map(func(t string) string { | ||
var i = strings.IndexRune(t, '#') | ||
if i > -1 { | ||
return t[:i] | ||
} | ||
k := strings.TrimSpace(m[1]) | ||
if k == "" { | ||
return t | ||
})(lines) | ||
lines = fp.Map(func(t string) string { return strings.TrimSpace(t) })(lines) | ||
lines = fp.Filter(func(t string) bool { return t != "" })(lines) | ||
|
||
var m = make(map[string]string) | ||
for _, line := range lines { | ||
i := strings.IndexRune(line, ';') | ||
if i > -1 { | ||
line = line[:i] | ||
} | ||
line = strings.TrimSpace(line) | ||
name := pyNamePrefixPattern.FindString(line) | ||
if name == "" { | ||
continue | ||
} | ||
v := strings.TrimSpace(m[2]) | ||
deps[k] = v | ||
var version string | ||
var versions []string | ||
match := must.A(pyVersionSegPattern.FindStringMatch(line)) | ||
for match != nil { | ||
versions = append(versions, match.GroupByNumber(1).String()) | ||
match = must.A(pyVersionSegPattern.FindNextMatch(match)) | ||
} | ||
if len(versions) == 1 { | ||
version = versions[0] | ||
} | ||
m[name] = version | ||
} | ||
return deps | ||
return m | ||
} |