-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.go
192 lines (172 loc) · 5.33 KB
/
python.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package internal
import (
"log"
"path"
"regexp"
"strings"
"github.com/CircleCI-Public/circleci-config/labeling/codebase"
"github.com/CircleCI-Public/circleci-config/labeling/labels"
"github.com/pelletier/go-toml"
)
var pipenvFiles = []string{
"Pipfile",
"Pipfile.lock",
}
var poetryFiles = []string{
"poetry.lock",
}
// All the possible files that could be used to determine if it's a Python codebase
var possiblePythonFiles = append(
append(
[]string{
"requirements.txt",
"pyproject.toml",
"manage.py",
"setup.py",
},
pipenvFiles...,
),
poetryFiles...,
)
var PythonRules = []labels.Rule{
func(c codebase.Codebase, ls labels.LabelSet) (labels.Label, error) {
label := labels.Label{
Key: labels.DepsPython,
}
filePath, _ := c.FindFile(possiblePythonFiles...)
label.Valid = filePath != ""
label.BasePath = path.Dir(filePath)
pythonVersion := getPythonVersion(c)
if pythonVersion != "" {
label.Dependencies = map[string]string{
"python": pythonVersion,
}
}
return label, nil
},
func(c codebase.Codebase, ls labels.LabelSet) (labels.Label, error) {
label := labels.Label{
Key: labels.PackageManagerPipenv,
}
pipfile, _ := c.FindFile(pipenvFiles...)
label.Valid = pipfile != ""
label.BasePath = path.Dir(pipfile)
pyprojectPath, _ := c.FindFile("pyproject.toml")
if pyprojectPath != "" && fileContainsString(c, pyprojectPath, "pipenv") {
label.Valid = true
label.BasePath = path.Dir(pyprojectPath)
}
return label, nil
},
func(c codebase.Codebase, ls labels.LabelSet) (labels.Label, error) {
label := labels.Label{
Key: labels.PackageManagerPoetry,
}
poetryLock, _ := c.FindFile(poetryFiles...)
label.Valid = poetryLock != ""
label.BasePath = path.Dir(poetryLock)
pyprojectPath, _ := c.FindFile("pyproject.toml")
if pyprojectPath != "" && fileContainsString(c, pyprojectPath, "poetry") {
label.Valid = true
label.BasePath = path.Dir(pyprojectPath)
}
return label, nil
},
func(c codebase.Codebase, ls labels.LabelSet) (labels.Label, error) {
label := labels.Label{
Key: labels.FileManagePy,
}
managePyPath, _ := c.FindFile("manage.py")
label.Valid = managePyPath != ""
label.BasePath = path.Dir(managePyPath)
return label, nil
},
func(c codebase.Codebase, ls labels.LabelSet) (labels.Label, error) {
label := labels.Label{
Key: labels.FileSetupPy,
}
setupPath, _ := c.FindFile("setup.py")
label.Valid = setupPath != ""
label.BasePath = path.Dir(setupPath)
return label, nil
},
func(c codebase.Codebase, ls labels.LabelSet) (labels.Label, error) {
label := labels.Label{Key: labels.TestTox}
toxPath, _ := c.FindFile("tox.ini")
label.Valid = toxPath != ""
label.BasePath = path.Dir(toxPath)
return label, nil
},
}
func fileContainsString(c codebase.Codebase, filePath string, str string) bool {
file, err := c.ReadFile(filePath)
if err != nil {
return false
}
fileStr := string(file)
if fileStr == "" {
return false
}
return strings.Contains(fileStr, str)
}
func getPythonVersion(c codebase.Codebase) string {
versionFilePath, _ := c.FindFile(".python-version")
if versionFilePath != "" {
file, err := c.ReadFile(versionFilePath)
if err != nil {
log.Println("Unable to read file .python-version. Attempting to determine python version another way... ", err)
} else {
versionRegex := regexp.MustCompile(`[0-9.]+`)
pythonVersion := versionRegex.FindString(string(file))
if pythonVersion != "" {
log.Println("Unable to parse file .python-version. Attempting to determine python version another way...")
}
log.Println("Found Python version in .python-version file: ", pythonVersion)
return pythonVersion
}
}
pyprojectFilePath, _ := c.FindFile("pyproject.toml")
if pyprojectFilePath != "" {
file, err := c.ReadFile(pyprojectFilePath)
if err != nil {
log.Println("Unable to read file pyproject.toml. Attempting to determine python version another way... ", err)
} else {
tree, err := toml.LoadBytes(file)
if err != nil {
log.Println("Unable to read file pyproject.toml. Attempting to determine python version another way... ", err)
} else {
pythonVersion := tree.Get("tool.poetry.dependencies.python")
if pythonVersion == nil || pythonVersion.(string) == "" {
log.Println("Error parsing pyproject.toml, python version is nil")
} else {
pythonVersion = strings.TrimPrefix(pythonVersion.(string), "^")
log.Println("Found Python version in pyproject.toml file: ", pythonVersion.(string))
return pythonVersion.(string)
}
}
}
}
pipfileFilePath, _ := c.FindFile("Pipfile")
if pipfileFilePath != "" {
file, err := c.ReadFile(pipfileFilePath)
if err != nil {
log.Println("Unable to read file Pipfile. Attempting to determine python version another way... ", err)
} else {
tree, err := toml.LoadBytes(file)
if err != nil {
log.Println("Unable to read file Pipfile. Attempting to determine python version another way... ", err)
} else {
pythonVersion := tree.Get("requires.python_version")
if pythonVersion == nil || pythonVersion.(string) == "" {
log.Println("Error parsing Pipfile, returning nil")
return ""
} else {
pythonVersion = strings.TrimPrefix(pythonVersion.(string), "^")
log.Println("Found Python version in Pipfile: ", pythonVersion.(string))
return pythonVersion.(string)
}
}
}
}
return ""
}