forked from go-python/gopy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
150 lines (129 loc) · 2.6 KB
/
Copy pathutils.go
File metadata and controls
150 lines (129 loc) · 2.6 KB
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
// Copyright 2015 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bind
import (
"bufio"
"bytes"
"fmt"
"go/types"
"io"
"os/exec"
"regexp"
"sort"
)
func isErrorType(typ types.Type) bool {
return typ == types.Universe.Lookup("error").Type()
}
func isStringer(obj types.Object) bool {
switch obj := obj.(type) {
case *types.Func:
if obj.Name() != "String" {
return false
}
sig, ok := obj.Type().(*types.Signature)
if !ok {
return false
}
if sig.Recv() == nil {
return false
}
if sig.Params().Len() != 0 {
return false
}
res := sig.Results()
if res.Len() != 1 {
return false
}
ret := res.At(0).Type()
if ret != types.Universe.Lookup("string").Type() {
return false
}
return true
default:
return false
}
return false
}
func hasError(sig *types.Signature) bool {
res := sig.Results()
if res == nil || res.Len() <= 0 {
return false
}
nerr := 0
for i := 0; i < res.Len(); i++ {
ret := res.At(i)
if isErrorType(ret.Type()) {
nerr++
}
}
switch {
case nerr == 0:
return false
case nerr == 1:
return true
default:
panic(fmt.Errorf(
"gopy: invalid number of comma-errors (%d)",
nerr,
))
}
return false
}
func isConstructor(sig *types.Signature) bool {
//TODO(sbinet)
return false
}
// getPkgConfig returns the name of the pkg-config python's pc file
func getPkgConfig(vers int) (string, error) {
bin, err := exec.LookPath("pkg-config")
if err != nil {
return "", fmt.Errorf(
"gopy: could not locate 'pkg-config' executable (err: %v)",
err,
)
}
out, err := exec.Command(bin, "--list-all").Output()
if err != nil {
return "", fmt.Errorf(
"gopy: error retrieving the list of packages known to pkg-config (err: %v)",
err,
)
}
pkgs := []string{}
re := regexp.MustCompile(fmt.Sprintf(`^python(\s|-|\.|)%d.*?`, vers))
s := bufio.NewScanner(bytes.NewReader(out))
for s.Scan() {
err = s.Err()
if err != nil {
if err == io.EOF {
err = nil
}
break
}
line := s.Bytes()
if !bytes.HasPrefix(line, []byte("python")) {
continue
}
if !re.Match(line) {
continue
}
pkg := bytes.Split(line, []byte(" "))
pkgs = append(pkgs, string(pkg[0]))
}
if err != nil {
return "", fmt.Errorf(
"gopy: error scanning pkg-config output (err: %v)",
err,
)
}
if len(pkgs) <= 0 {
return "", fmt.Errorf(
"gopy: could not find pkg-config file (no python.pc installed?)",
)
}
sort.Strings(pkgs)
// FIXME(sbinet): make sure we take the latest version?
pkgcfg := pkgs[0]
return pkgcfg, nil
}