-
Notifications
You must be signed in to change notification settings - Fork 584
/
pip.go
139 lines (124 loc) · 3.92 KB
/
pip.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package versioning
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/pkg/errors"
)
const (
// NameRegex is used to match the pip descriptor artifact name
NameRegex = "(?s)(.*)name=['\"](.*?)['\"](.*)"
// VersionRegex is used to match the pip descriptor artifact version
VersionRegex = "(?s)(.*)version=['\"](.*?)['\"](.*)"
)
// Pip utility to interact with Python specific versioning
type Pip struct {
path string
readFile func(string) ([]byte, error)
writeFile func(string, []byte, os.FileMode) error
fileExists func(string) (bool, error)
buildDescriptorContent string
}
func (p *Pip) init() error {
if p.readFile == nil {
p.readFile = os.ReadFile
}
if p.writeFile == nil {
p.writeFile = os.WriteFile
}
if len(p.buildDescriptorContent) == 0 {
content, err := p.readFile(p.path)
if err != nil {
return errors.Wrapf(err, "failed to read file '%v'", p.path)
}
p.buildDescriptorContent = string(content)
}
return nil
}
// GetVersion returns the Pip descriptor version property
func (p *Pip) GetVersion() (string, error) {
buildDescriptorFilePath := p.path
var err error
if strings.Contains(p.path, "setup.py") {
buildDescriptorFilePath, err = searchDescriptor([]string{"version.txt", "VERSION"}, p.fileExists)
if err != nil {
initErr := p.init()
if initErr != nil {
return "", errors.Wrapf(initErr, "failed to read file '%v'", p.path)
}
if evaluateResult(p.buildDescriptorContent, VersionRegex) {
compile := regexp.MustCompile(VersionRegex)
values := compile.FindStringSubmatch(p.buildDescriptorContent)
return values[2], nil
}
return "", errors.Wrap(err, "failed to retrieve version")
}
}
artifact := &Versionfile{
path: buildDescriptorFilePath,
versioningScheme: p.VersioningScheme(),
readFile: p.readFile,
}
return artifact.GetVersion()
}
// SetVersion sets the Pip descriptor version property
func (p *Pip) SetVersion(v string) error {
buildDescriptorFilePath := p.path
var err error
if strings.Contains(p.path, "setup.py") {
buildDescriptorFilePath, err = searchDescriptor([]string{"version.txt", "VERSION"}, p.fileExists)
if err != nil {
initErr := p.init()
if initErr != nil {
return errors.Wrapf(initErr, "failed to read file '%v'", p.path)
}
if evaluateResult(p.buildDescriptorContent, VersionRegex) {
compile := regexp.MustCompile(VersionRegex)
values := compile.FindStringSubmatch(p.buildDescriptorContent)
p.buildDescriptorContent = strings.ReplaceAll(p.buildDescriptorContent, fmt.Sprintf("version='%v'", values[2]), fmt.Sprintf("version='%v'", v))
p.buildDescriptorContent = strings.ReplaceAll(p.buildDescriptorContent, fmt.Sprintf("version=\"%v\"", values[2]), fmt.Sprintf("version=\"%v\"", v))
p.writeFile(p.path, []byte(p.buildDescriptorContent), 0600)
} else {
return errors.Wrap(err, "failed to retrieve version")
}
}
}
artifact := &Versionfile{
path: buildDescriptorFilePath,
versioningScheme: p.VersioningScheme(),
writeFile: p.writeFile,
}
return artifact.SetVersion(v)
}
// VersioningScheme returns the relevant versioning scheme
func (p *Pip) VersioningScheme() string {
return "pep440"
}
// GetCoordinates returns the pip build descriptor coordinates
func (p *Pip) GetCoordinates() (Coordinates, error) {
result := Coordinates{}
err := p.init()
if err != nil {
return result, err
}
if evaluateResult(p.buildDescriptorContent, NameRegex) {
compile := regexp.MustCompile(NameRegex)
values := compile.FindStringSubmatch(p.buildDescriptorContent)
result.ArtifactID = values[2]
} else {
result.ArtifactID = ""
}
result.Version, err = p.GetVersion()
if err != nil {
return result, errors.Wrap(err, "failed to retrieve coordinates")
}
return result, nil
}
func evaluateResult(value, regex string) bool {
if len(value) > 0 {
match, _ := regexp.MatchString(regex, value)
return match
}
return true
}