-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiputil.go
71 lines (61 loc) · 1.55 KB
/
piputil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"bufio"
"os"
"strings"
)
type PipRequirements struct {
path string
libraryVersions []LibraryVersion
}
func (p PipRequirements) OpenRequirementsFile() (*bufio.Reader, *os.File) {
f, err := os.Open(p.path)
if err != nil {
panic(err)
}
reader := bufio.NewReader(f)
return reader, f
}
func (p PipRequirements) ReadCurrentVersion(reader *bufio.Reader) Requirements {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
var tokens []string
line := scanner.Text()
if tokens = tokenize(line); tokens != nil {
p.libraryVersions = append(p.libraryVersions, LibraryVersion{tokens[0], tokens[1]})
}
}
return p
}
func (p PipRequirements) GetLibraryVersions() []LibraryVersion {
return p.libraryVersions
}
func (p PipRequirements) GetPath() string {
return p.path
}
func tokenize(line string) []string {
line = strings.Trim(line, " ")
var tokens []string
if line == "" {
tokens = nil
} else if strings.HasPrefix(line, "#") {
tokens = nil
} else if strings.HasPrefix(line, "--") {
tokens = nil
} else if strings.Contains(line, "==") {
tokens = strings.Split(line, "==")
} else if strings.Contains(line, ">=") {
tokens = strings.Split(line, ">=")
} else if strings.Contains(line, "<=") {
tokens = strings.Split(line, "<=")
} else if strings.Contains(line, "<") {
tokens = strings.Split(line, "<")
} else if strings.Contains(line, ">") {
tokens = strings.Split(line, ">")
} else {
tokens = []string{line, "?"}
}
return tokens
}
func (p PipRequirements) UpdateVersion(toUpdate map[string]string){
}