-
Notifications
You must be signed in to change notification settings - Fork 170
/
python.go
167 lines (139 loc) · 3.88 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
package appdetect
import (
"bufio"
"context"
"io/fs"
"os"
"path/filepath"
"strings"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
type pythonDetector struct {
}
func (pd *pythonDetector) Language() Language {
return Python
}
func (pd *pythonDetector) DetectProject(ctx context.Context, path string, entries []fs.DirEntry) (*Project, error) {
for _, entry := range entries {
// entry.Name() == "pyproject.toml" when azd supports pyproject files
if strings.ToLower(entry.Name()) == "requirements.txt" {
project := &Project{
Language: Python,
Path: path,
DetectionRule: "Inferred by presence of: " + entry.Name(),
}
file, err := os.Open(filepath.Join(path, entry.Name()))
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
databaseDepMap := map[DatabaseDep]struct{}{}
for scanner.Scan() {
split := strings.Split(scanner.Text(), "==")
if len(split) < 1 {
continue
}
// pip is case insensitive: PEP 426
// https://peps.python.org/pep-0426/#name
module := strings.ToLower(strings.TrimSpace(split[0]))
switch module {
case "fastapi":
project.Dependencies = append(project.Dependencies, PyFastApi)
case "flask":
project.Dependencies = append(project.Dependencies, PyFlask)
case "django":
project.Dependencies = append(project.Dependencies, PyDjango)
}
switch module {
case "flask_mysqldb",
"mysqlclient",
"aiomysql",
"asyncmy":
databaseDepMap[DbMySql] = struct{}{}
case "psycopg2",
"psycopg2-binary",
"psycopg",
"psycopgbinary",
"asyncpg",
"aiopg":
databaseDepMap[DbPostgres] = struct{}{}
case "pymongo",
"beanie",
"motor":
databaseDepMap[DbMongo] = struct{}{}
case "redis", "redis-om":
databaseDepMap[DbRedis] = struct{}{}
}
}
if err := file.Close(); err != nil {
return nil, err
}
if len(databaseDepMap) > 0 {
project.DatabaseDeps = maps.Keys(databaseDepMap)
slices.SortFunc(project.DatabaseDeps, func(a, b DatabaseDep) bool {
return string(a) < string(b)
})
}
slices.SortFunc(project.Dependencies, func(a, b Dependency) bool {
return string(a) < string(b)
})
return project, nil
}
}
return nil, nil
}
// PyFastApiLaunch returns the launch argument for a python FastAPI project to be served by a python web server.
// An empty string is returned if the project is not a FastAPI project.
// An error is returned only if the project path cannot be walked.
func PyFastApiLaunch(projectPath string) (string, error) {
maxDepth := 2
// A launch path that looks like: dir1.dir2.main:app
launchPath := ""
err := filepath.WalkDir(projectPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(projectPath, path)
if err != nil {
return err
}
if d.IsDir() && strings.Count(rel, string(os.PathSeparator)) > maxDepth-1 {
return filepath.SkipDir
}
if strings.HasSuffix(path, "main.py") || strings.HasSuffix(path, "app.py") {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// match on something that looks like:
// app = FastAPI()
// app = fastapi.FastAPI()
// app = FastAPI(
if strings.Contains(scanner.Text(), "FastAPI(") {
decl := strings.Split(scanner.Text(), " ")
if len(decl) >= 3 && decl[1] == "=" {
mainObjName := decl[0]
mainFilePath := rel
// dir1/dir2/main.py -> dir1.dir2.main
mainPath := strings.ReplaceAll(
strings.TrimSuffix(mainFilePath, ".py"),
string(os.PathSeparator),
".")
launchPath = mainPath + ":" + mainObjName
return filepath.SkipAll
}
}
}
return scanner.Err()
}
return nil
})
if err != nil {
return "", err
}
return launchPath, nil
}