Skip to content

Commit 88f4701

Browse files
committed
fix: use NewSpec and python sysPath
1 parent 89377b5 commit 88f4701

3 files changed

Lines changed: 28 additions & 30 deletions

File tree

lang/collect/collect.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ type functionInfo struct {
8686
func switchSpec(l uniast.Language) LanguageSpec {
8787
switch l {
8888
case uniast.Rust:
89-
return &rust.RustSpec{}
89+
return rust.NewRustSpec()
9090
case uniast.Cxx:
91-
return &cxx.CxxSpec{}
91+
return cxx.NewCxxSpec()
9292
case uniast.Python:
93-
return &python.PythonSpec{}
93+
return python.NewPythonSpec()
9494
default:
9595
panic(fmt.Sprintf("unsupported language %s", l))
9696
}

lang/python/lib.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ import (
2424
const MaxWaitDuration = 5 * time.Second
2525

2626
func GetDefaultLSP() (lang uniast.Language, name string) {
27-
// Use custom PyLSP.
2827
return uniast.Python, "pylsp"
2928
}
3029

3130
func CheckRepo(repo string) (string, time.Duration) {
3231
openfile := ""
33-
// TODO: check if the project compiles.
3432

35-
// NOTICE: wait for Rust projects based on code files
33+
// Give the LSP sometime to initialize
3634
_, size := utils.CountFiles(repo, ".py", "SKIPDIR")
3735
wait := 2*time.Second + time.Second*time.Duration(size/1024)
3836
if wait > MaxWaitDuration {

lang/python/spec.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ package python
1717
import (
1818
"fmt"
1919
"os"
20+
"os/exec"
2021
"path/filepath"
2122
"regexp"
23+
"sort"
2224
"strings"
2325

26+
"github.com/cloudwego/abcoder/lang/log"
2427
lsp "github.com/cloudwego/abcoder/lang/lsp"
2528
"github.com/cloudwego/abcoder/lang/uniast"
2629
)
@@ -29,10 +32,23 @@ type PythonSpec struct {
2932
repo string
3033
topModuleName string
3134
topModulePath string
35+
sysPaths []string
3236
}
3337

3438
func NewPythonSpec() *PythonSpec {
35-
return &PythonSpec{}
39+
cmd := exec.Command("python", "-c", "import sys ; print('\\n'.join(sys.path))")
40+
output, err := cmd.Output()
41+
if err != nil {
42+
log.Error("Failed to get sys.path: %v\n", err)
43+
return nil
44+
}
45+
sysPaths := strings.Split(string(output), "\n")
46+
// Match more specific paths first
47+
sort.Slice(sysPaths, func(i, j int) bool {
48+
return len(sysPaths[i]) > len(sysPaths[j])
49+
})
50+
log.Info("PythonSpec: using sysPaths %+v\n", sysPaths)
51+
return &PythonSpec{sysPaths: sysPaths}
3652
}
3753

3854
func (c *PythonSpec) WorkSpace(root string) (map[string]string, error) {
@@ -83,18 +99,11 @@ func (c *PythonSpec) NameSpace(path string) (string, string, error) {
8399
return modName, pkgPath, nil
84100
}
85101

86-
// XXX: hardcode
87-
if strings.HasSuffix(path, "stdlib/3/builtins.pyi") {
88-
// builtin module
89-
return "builtins", "builtins", nil
90-
}
91-
92-
// XXX: hardcoded python path
93-
condaPrefix := "/home/zhenyang/anaconda3/envs/abcoder/lib/python3.11"
94-
if strings.HasPrefix(path, condaPrefix) {
95-
if strings.HasPrefix(path, condaPrefix+"/site-packages") {
96-
// external module
97-
relPath, err := filepath.Rel(condaPrefix+"/site-packages", path)
102+
for _, sysPath := range c.sysPaths {
103+
log.Error("PythonSpec: path %s sysPath %s\n", path, sysPath)
104+
if strings.HasPrefix(path, sysPath) {
105+
relPath, err := filepath.Rel(sysPath, path)
106+
log.Error("PythonSpec: matched relPath %s, sysPath %s\n", relPath, sysPath)
98107
if err != nil {
99108
return "", "", err
100109
}
@@ -107,18 +116,9 @@ func (c *PythonSpec) NameSpace(path string) (string, string, error) {
107116
}
108117
panic(fmt.Sprintf("Malformed Namespace %s, pkgPath %s", path, pkgPath))
109118
}
110-
// builtin module
111-
modName := "builtins"
112-
relPath, err := filepath.Rel(condaPrefix, path)
113-
if err != nil {
114-
return "", "", err
115-
}
116-
relPath = strings.TrimSuffix(relPath, ".py")
117-
pkgPath := strings.ReplaceAll(relPath, string(os.PathSeparator), ".")
118-
return modName, pkgPath, nil
119119
}
120-
121-
panic(fmt.Sprintf("Unhandled Namespace %s", path))
120+
log.Error("Namespace not found for path: %s\n", path)
121+
return "", "", fmt.Errorf("namespace not found for path: %s", path)
122122
}
123123

124124
func (c *PythonSpec) ShouldSkip(path string) bool {

0 commit comments

Comments
 (0)