-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathpy_library.go
More file actions
171 lines (145 loc) · 4.58 KB
/
py_library.go
File metadata and controls
171 lines (145 loc) · 4.58 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package rules_python
import (
"path/filepath"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/resolve"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/stackb/rules_proto/pkg/protoc"
)
var pyLibraryKindInfo = rule.KindInfo{
MergeableAttrs: map[string]bool{
"srcs": true,
"deps": true,
"visibility": true,
"imports": true,
},
NonEmptyAttrs: map[string]bool{
"srcs": true,
},
ResolveAttrs: map[string]bool{
"deps": true,
},
}
// PyLibrary implements RuleProvider for 'py_library'-derived rules.
type PyLibrary struct {
KindName string
RuleNameSuffix string
Outputs []string
Config *protoc.ProtocConfiguration
RuleConfig *protoc.LanguageRuleConfig
Resolver protoc.DepsResolver
}
// Kind implements part of the ruleProvider interface.
func (s *PyLibrary) Kind() string {
return s.KindName
}
// Name implements part of the ruleProvider interface.
func (s *PyLibrary) Name() string {
return s.Config.Library.BaseName() + s.RuleNameSuffix
}
// Srcs computes the srcs list for the rule.
func (s *PyLibrary) Srcs() []string {
srcs := make([]string, 0)
for _, output := range s.Outputs {
if strings.HasSuffix(output, ".py") {
srcs = append(srcs, protoc.StripRel(s.Config.Rel, output))
}
}
return srcs
}
// Deps computes the deps list for the rule.
func (s *PyLibrary) Deps() []string {
return s.RuleConfig.GetDeps()
}
// Visibility provides visibility labels.
func (s *PyLibrary) Visibility() []string {
return s.RuleConfig.GetVisibility()
}
// ImportsAttr provides the py_library.imports attribute values.
func (s *PyLibrary) ImportsAttr() (imps []string) {
// if we have a strip_import_prefix on the proto_library, the python search
// path should include the directory N parents above the current package,
// where N is the number of segments needed to ascend to the prefix from
// the dir for the current rule.
if s.Config.Library.StripImportPrefix() == "" {
return
}
prefix := s.Config.Library.StripImportPrefix()
if !strings.HasPrefix(prefix, "/") {
return // deal with relative-imports at another time
}
prefix = strings.TrimPrefix(prefix, "/")
rel, err := filepath.Rel(prefix, s.Config.Rel)
if err != nil {
return // the prefix doesn't prefix the current path, shouldn't happen
}
parts := strings.Split(rel, "/")
for i := 0; i < len(parts); i++ {
parts[i] = ".."
}
imp := strings.Join(parts, "/")
imps = append(imps, imp)
return
}
// Rule implements part of the ruleProvider interface.
func (s *PyLibrary) Rule(otherGen ...*rule.Rule) *rule.Rule {
newRule := rule.NewRule(s.Kind(), s.Name())
newRule.SetAttr("srcs", s.Srcs())
deps := s.Deps()
if len(deps) > 0 {
newRule.SetAttr("deps", deps)
}
imports := s.ImportsAttr()
if len(imports) > 0 {
newRule.SetAttr("imports", imports)
}
visibility := s.Visibility()
if len(visibility) > 0 {
newRule.SetAttr("visibility", visibility)
}
return newRule
}
func pyFilenameToImport(s , stripImportPrefix string) string {
if stripImportPrefix != "" {
prefix := strings.TrimPrefix(stripImportPrefix, "/")
s = strings.TrimPrefix(s, prefix)
s = strings.TrimPrefix(s, "/") // should never be absolute
}
if strings.HasSuffix(s, ".py") {
return strings.ReplaceAll(s[:len(s)-3], "/", ".")
}
return s
}
// Imports implements part of the RuleProvider interface.
func (s *PyLibrary) Imports(c *config.Config, r *rule.Rule, file *rule.File) []resolve.ImportSpec {
if lib, ok := r.PrivateAttr(protoc.ProtoLibraryKey).(protoc.ProtoLibrary); ok {
specs := protoc.ProtoLibraryImportSpecsForKind(r.Kind(), lib)
specs = maybeStripImportPrefix(specs, lib.StripImportPrefix())
from := label.New("", file.Pkg, r.Name())
for _, o := range s.Outputs {
pyImp := pyFilenameToImport(o, lib.StripImportPrefix())
protoc.GlobalResolver().Provide("py", "py", pyImp, from)
specs = append(specs, resolve.ImportSpec{Lang: "py", Imp: pyImp})
}
return specs
}
return nil
}
// Resolve implements part of the RuleProvider interface.
func (s *PyLibrary) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
s.Resolver(c, ix, r, imports, from)
}
func maybeStripImportPrefix(specs []resolve.ImportSpec, stripImportPrefix string) []resolve.ImportSpec {
if stripImportPrefix == "" {
return specs
}
prefix := strings.TrimPrefix(stripImportPrefix, "/")
for i, spec := range specs {
spec.Imp = strings.TrimPrefix(spec.Imp, prefix)
spec.Imp = strings.TrimPrefix(spec.Imp, "/") // should never be absolute
specs[i] = spec
}
return specs
}