-
Notifications
You must be signed in to change notification settings - Fork 168
/
python.go
192 lines (178 loc) · 4.18 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 python
import (
"context"
"github.com/murphysecurity/murphysec/infra/logctx"
"github.com/murphysecurity/murphysec/infra/pathignore"
"github.com/murphysecurity/murphysec/model"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
)
type Inspector struct{}
func (i Inspector) SupportFeature(feature model.InspectorFeature) bool {
return false
}
func (i Inspector) String() string {
return "Python"
}
func (i Inspector) CheckDir(dir string) bool {
r, e := os.ReadDir(dir)
if e == nil {
for _, it := range r {
if it.IsDir() {
continue
}
name := it.Name()
if name == "conanfile.py" {
continue
}
if isRequirementsFile(name) {
return true
}
if filepath.Ext(name) == ".py" {
return true
}
}
}
return false
}
func (i Inspector) InspectProject(ctx context.Context) error {
logger := logctx.Use(ctx).Sugar()
dir := model.UseInspectorTask(ctx).ScanDir
info, e := collectDepsInfo(ctx, dir)
if e != nil {
return e
}
if len(info) == 0 {
logger.Infof("found no deps, omit module")
return nil
}
m := model.Module{
Name: filepath.Base(dir),
PackageManager: model.PMPip,
Language: model.Python,
Dependencies: make([]model.Dependency, 0),
RelativePath: dir,
}
if m.Name == "." {
m.Name = "Python"
}
for _, it := range info {
k, v := it[0], it[1]
var di model.Dependency
di.Name = k
di.Version = v
m.Dependencies = append(m.Dependencies, di)
}
model.UseInspectorTask(ctx).AddModule(m)
return nil
}
func isRequirementsFile(filename string) bool {
return strings.Contains(strings.ToLower(filename), "requirement")
}
func isDockerfile(filename string) bool {
return strings.Contains(strings.ToLower(filename), "dockerfile")
}
func dirIgnore(name string) bool {
return name == "" || name[0] == '.' || pathignore.DirName(name)
}
func collectDepsInfo(ctx context.Context, dir string) ([][2]string, error) {
var logger = logctx.Use(ctx).Sugar()
if !filepath.IsAbs(dir) {
panic("dir must be absolute")
}
var noVersionComps = make(map[string]struct{})
var versionedComps = make(map[string]string)
var unknownVersionComps = make(map[string]struct{})
e := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil || d == nil {
return err
}
filename := d.Name()
if d.IsDir() {
if dirIgnore(filename) {
return filepath.SkipDir
}
return nil
}
// ignore conan.py
if filename == "" || filename[0] == '.' || filename == "conan.py" {
return nil
}
if isDockerfile(filename) {
data, e := readFile(path, 64*1024) // Dockerfile max 64K
if e != nil {
logger.Warnf("read dockerfile: %s %v", path, e)
return nil
}
for _, s := range parseDockerfilePipInstall(string(data)) {
noVersionComps[s] = struct{}{}
}
return nil
}
if isRequirementsFile(filename) {
data, e := readFile(path, 64*1024)
if e != nil {
logger.Warnf("read requirement: %s %v", path, e)
return nil
}
for k, v := range parseRequirements(string(data)) {
if v == "" {
unknownVersionComps[k] = struct{}{}
} else {
versionedComps[k] = v
}
}
return nil
}
if filepath.Ext(filename) == ".py" {
data, e := readFile(path, 256*1024)
if e != nil {
logger.Warnf("read py: %s %v", path, e)
return nil
}
for _, s := range parsePyImport(string(data)) {
if pyPkgBlackList[s] {
continue
}
unknownVersionComps[s] = struct{}{}
}
}
return nil
})
if e != nil {
logger.Warnf("walk error: %v", e)
}
// merge unknown version components
for s := range unknownVersionComps {
if versionedComps[s] != "" {
delete(unknownVersionComps, s)
}
}
if len(unknownVersionComps) != 0 {
// try to resolve version from pip list
m, e := getEnvPipListMap(ctx)
if e != nil {
m = make(map[string]string)
}
for s := range unknownVersionComps {
versionedComps[s] = m[s]
}
}
for s := range noVersionComps {
if versionedComps[s] != "" {
continue
}
versionedComps[s] = ""
}
var result [][2]string
for k, v := range versionedComps {
result = append(result, [2]string{k, v})
}
sort.Slice(result, func(i, j int) bool {
return result[i][0] < result[j][0] // sort by name is enough
})
return result, nil
}