@@ -32,11 +32,12 @@ import (
32
32
// It is a PipelineItem.
33
33
type Extractor struct {
34
34
core.NoopMerger
35
- Endpoint string
36
- Context func () (context.Context , context.CancelFunc )
37
- PoolSize int
38
- FailOnErrors bool
39
- ProcessedFiles map [string ]int
35
+ Endpoint string
36
+ Context func () (context.Context , context.CancelFunc )
37
+ PoolSize int
38
+ FailOnErrors bool
39
+ ProcessedFiles map [string ]int
40
+ IgnoredMissingDrivers map [string ]bool
40
41
41
42
clients []* bblfsh.Client
42
43
pool * tunny.Pool
@@ -45,22 +46,36 @@ type Extractor struct {
45
46
const (
46
47
// ConfigUASTEndpoint is the name of the configuration option (Extractor.Configure())
47
48
// which sets the Babelfish server address.
48
- ConfigUASTEndpoint = "ConfigUASTEndpoint "
49
+ ConfigUASTEndpoint = "UAST.Endpoint "
49
50
// ConfigUASTTimeout is the name of the configuration option (Extractor.Configure())
50
51
// which sets the maximum amount of time to wait for a Babelfish server response.
51
- ConfigUASTTimeout = "ConfigUASTTimeout "
52
+ ConfigUASTTimeout = "UAST.Timeout "
52
53
// ConfigUASTPoolSize is the name of the configuration option (Extractor.Configure())
53
54
// which sets the number of goroutines to run for UAST parse queries.
54
- ConfigUASTPoolSize = "ConfigUASTPoolSize "
55
+ ConfigUASTPoolSize = "UAST.PoolSize "
55
56
// ConfigUASTFailOnErrors is the name of the configuration option (Extractor.Configure())
56
57
// which enables early exit in case of any Babelfish UAST parsing errors.
57
- ConfigUASTFailOnErrors = "ConfigUASTFailOnErrors"
58
+ ConfigUASTFailOnErrors = "UAST.FailOnErrors"
59
+ // ConfigUASTIgnoreMissingDrivers is the name of the configuration option (Extractor.Configure())
60
+ // which sets the ignored missing driver names.
61
+ ConfigUASTIgnoreMissingDrivers = "UAST.IgnoreMissingDrivers"
62
+ // DefaultBabelfishEndpoint is the default address of the Babelfish parsing server.
63
+ DefaultBabelfishEndpoint = "0.0.0.0:9432"
64
+ // DefaultBabelfishTimeout is the default value of the RPC timeout in seconds.
65
+ DefaultBabelfishTimeout = 20
58
66
// FeatureUast is the name of the Pipeline feature which activates all the items related to UAST.
59
67
FeatureUast = "uast"
60
68
// DependencyUasts is the name of the dependency provided by Extractor.
61
69
DependencyUasts = "uasts"
62
70
)
63
71
72
+ var (
73
+ // DefaultBabelfishWorkers is the default number of parsing RPC goroutines.
74
+ DefaultBabelfishWorkers = runtime .NumCPU () * 2
75
+ // DefaultIgnoredMissingDrivers is the languages which are ignored if the Babelfish driver is missing.
76
+ DefaultIgnoredMissingDrivers = []string {"markdown" , "text" , "yaml" , "json" }
77
+ )
78
+
64
79
type uastTask struct {
65
80
Lock * sync.RWMutex
66
81
Dest map [plumbing.Hash ]nodes.Node
@@ -117,22 +132,27 @@ func (exr *Extractor) ListConfigurationOptions() []core.ConfigurationOption {
117
132
Description : "How many days there are in a single band." ,
118
133
Flag : "bblfsh" ,
119
134
Type : core .StringConfigurationOption ,
120
- Default : "0.0.0.0:9432" }, {
135
+ Default : DefaultBabelfishEndpoint }, {
121
136
Name : ConfigUASTTimeout ,
122
137
Description : "Babelfish's server timeout in seconds." ,
123
138
Flag : "bblfsh-timeout" ,
124
139
Type : core .IntConfigurationOption ,
125
- Default : 20 }, {
140
+ Default : DefaultBabelfishTimeout }, {
126
141
Name : ConfigUASTPoolSize ,
127
142
Description : "Number of goroutines to extract UASTs." ,
128
143
Flag : "bblfsh-pool-size" ,
129
144
Type : core .IntConfigurationOption ,
130
- Default : runtime . NumCPU () * 2 }, {
145
+ Default : DefaultBabelfishWorkers }, {
131
146
Name : ConfigUASTFailOnErrors ,
132
147
Description : "Panic if there is a UAST extraction error." ,
133
148
Flag : "bblfsh-fail-on-error" ,
134
149
Type : core .BoolConfigurationOption ,
135
- Default : false },
150
+ Default : false }, {
151
+ Name : ConfigUASTIgnoreMissingDrivers ,
152
+ Description : "Do not warn about missing drivers for the specified languages." ,
153
+ Flag : "bblfsh-ignored-drivers" ,
154
+ Type : core .StringsConfigurationOption ,
155
+ Default : DefaultIgnoredMissingDrivers },
136
156
}
137
157
return options [:]
138
158
}
@@ -154,6 +174,12 @@ func (exr *Extractor) Configure(facts map[string]interface{}) error {
154
174
if val , exists := facts [ConfigUASTFailOnErrors ].(bool ); exists {
155
175
exr .FailOnErrors = val
156
176
}
177
+ if val , exists := facts [ConfigUASTIgnoreMissingDrivers ].([]string ); exists {
178
+ exr .IgnoredMissingDrivers = map [string ]bool {}
179
+ for _ , name := range val {
180
+ exr .IgnoredMissingDrivers [name ] = true
181
+ }
182
+ }
157
183
return nil
158
184
}
159
185
@@ -162,9 +188,16 @@ func (exr *Extractor) Configure(facts map[string]interface{}) error {
162
188
func (exr * Extractor ) Initialize (repository * git.Repository ) error {
163
189
if exr .Context == nil {
164
190
exr .Context = func () (context.Context , context.CancelFunc ) {
165
- return context .Background (), nil
191
+ return context .WithTimeout (context .Background (),
192
+ time .Duration (DefaultBabelfishTimeout )* time .Second )
166
193
}
167
194
}
195
+ if exr .Endpoint == "" {
196
+ exr .Endpoint = DefaultBabelfishEndpoint
197
+ }
198
+ if exr .PoolSize == 0 {
199
+ exr .PoolSize = DefaultBabelfishWorkers
200
+ }
168
201
poolSize := exr .PoolSize
169
202
if poolSize == 0 {
170
203
poolSize = runtime .NumCPU ()
@@ -196,6 +229,12 @@ func (exr *Extractor) Initialize(repository *git.Repository) error {
196
229
panic ("UAST goroutine pool was not created" )
197
230
}
198
231
exr .ProcessedFiles = map [string ]int {}
232
+ if exr .IgnoredMissingDrivers == nil {
233
+ exr .IgnoredMissingDrivers = map [string ]bool {}
234
+ for _ , name := range DefaultIgnoredMissingDrivers {
235
+ exr .IgnoredMissingDrivers [name ] = true
236
+ }
237
+ }
199
238
return nil
200
239
}
201
240
@@ -250,7 +289,7 @@ func (exr *Extractor) Consume(deps map[string]interface{}) (map[string]interface
250
289
if exr .FailOnErrors {
251
290
return nil , errors .New (joined )
252
291
}
253
- fmt . Fprintln ( os . Stderr , joined )
292
+ log . Println ( joined )
254
293
}
255
294
return map [string ]interface {}{DependencyUasts : uasts }, nil
256
295
}
@@ -284,6 +323,11 @@ func (exr *Extractor) extractTask(client *bblfsh.Client, data interface{}) inter
284
323
task .Lock .Lock ()
285
324
defer task .Lock .Unlock ()
286
325
if err != nil {
326
+ for lang := range exr .IgnoredMissingDrivers {
327
+ if strings .HasSuffix (err .Error (), "\" " + lang + "\" " ) {
328
+ return nil
329
+ }
330
+ }
287
331
* task .Errors = append (* task .Errors ,
288
332
fmt .Errorf ("\n file %s, blob %s: %v" , task .Name , task .Hash .String (), err ))
289
333
return nil
0 commit comments