@@ -49,6 +49,17 @@ const (
49
49
// naming convention. See python_library_naming_convention for more info on
50
50
// the package name interpolation.
51
51
TestNamingConvention = "python_test_naming_convention"
52
+ // PipRepoNamingConvention represents the directive that controls the
53
+ // mapping between Python modules and the repos. E.g. if the Python package name is `foo` and the
54
+ // pip repository is named `my_pip`, setting this
55
+ // to `$repo_name$_$distribution_name$` would render to `@my_pip_foo`.
56
+ PipRepoNamingConvention = "python_pip_repo_naming_convention"
57
+ // PipPackageNamingConvention represents the directive that controls the
58
+ // mapping between Python modules and the repos. By default it is ``
59
+ PipPackageNamingConvention = "python_pip_package_naming_convention"
60
+ // PipTargetNamingConvention represents the directive that controls the
61
+ // mapping between Python modules and the repos. By default it is `pkg`
62
+ PipTargetNamingConvention = "python_pip_target_naming_convention"
52
63
)
53
64
54
65
// GenerationModeType represents one of the generation modes for the Python
@@ -67,7 +78,9 @@ const (
67
78
)
68
79
69
80
const (
70
- packageNameNamingConventionSubstitution = "$package_name$"
81
+ repoNamePipRepoNamingConventionSubstitution = "$repo_name$"
82
+ distributionNamePipRepoNamingConventionSubstitution = "$distribution_name$"
83
+ packageNameNamingConventionSubstitution = "$package_name$"
71
84
)
72
85
73
86
// defaultIgnoreFiles is the list of default values used in the
@@ -99,14 +112,17 @@ type Config struct {
99
112
pythonProjectRoot string
100
113
gazelleManifest * manifest.Manifest
101
114
102
- excludedPatterns * singlylinkedlist.List
103
- ignoreFiles map [string ]struct {}
104
- ignoreDependencies map [string ]struct {}
105
- validateImportStatements bool
106
- coarseGrainedGeneration bool
107
- libraryNamingConvention string
108
- binaryNamingConvention string
109
- testNamingConvention string
115
+ excludedPatterns * singlylinkedlist.List
116
+ ignoreFiles map [string ]struct {}
117
+ ignoreDependencies map [string ]struct {}
118
+ validateImportStatements bool
119
+ coarseGrainedGeneration bool
120
+ libraryNamingConvention string
121
+ binaryNamingConvention string
122
+ testNamingConvention string
123
+ pipRepoNamingConvention string
124
+ pipPackageNamingConvention string
125
+ pipTargetNamingConvention string
110
126
}
111
127
112
128
// New creates a new Config.
@@ -115,17 +131,20 @@ func New(
115
131
pythonProjectRoot string ,
116
132
) * Config {
117
133
return & Config {
118
- extensionEnabled : true ,
119
- repoRoot : repoRoot ,
120
- pythonProjectRoot : pythonProjectRoot ,
121
- excludedPatterns : singlylinkedlist .New (),
122
- ignoreFiles : make (map [string ]struct {}),
123
- ignoreDependencies : make (map [string ]struct {}),
124
- validateImportStatements : true ,
125
- coarseGrainedGeneration : false ,
126
- libraryNamingConvention : packageNameNamingConventionSubstitution ,
127
- binaryNamingConvention : fmt .Sprintf ("%s_bin" , packageNameNamingConventionSubstitution ),
128
- testNamingConvention : fmt .Sprintf ("%s_test" , packageNameNamingConventionSubstitution ),
134
+ extensionEnabled : true ,
135
+ repoRoot : repoRoot ,
136
+ pythonProjectRoot : pythonProjectRoot ,
137
+ excludedPatterns : singlylinkedlist .New (),
138
+ ignoreFiles : make (map [string ]struct {}),
139
+ ignoreDependencies : make (map [string ]struct {}),
140
+ validateImportStatements : true ,
141
+ coarseGrainedGeneration : false ,
142
+ libraryNamingConvention : packageNameNamingConventionSubstitution ,
143
+ binaryNamingConvention : fmt .Sprintf ("%s_bin" , packageNameNamingConventionSubstitution ),
144
+ testNamingConvention : fmt .Sprintf ("%s_test" , packageNameNamingConventionSubstitution ),
145
+ pipRepoNamingConvention : fmt .Sprintf ("%s_%s" , repoNamePipRepoNamingConventionSubstitution , distributionNamePipRepoNamingConventionSubstitution ),
146
+ pipPackageNamingConvention : "" ,
147
+ pipTargetNamingConvention : "pkg" ,
129
148
}
130
149
}
131
150
@@ -150,6 +169,9 @@ func (c *Config) NewChild() *Config {
150
169
libraryNamingConvention : c .libraryNamingConvention ,
151
170
binaryNamingConvention : c .binaryNamingConvention ,
152
171
testNamingConvention : c .testNamingConvention ,
172
+ pipRepoNamingConvention : c .pipRepoNamingConvention ,
173
+ pipPackageNamingConvention : c .pipPackageNamingConvention ,
174
+ pipTargetNamingConvention : c .pipTargetNamingConvention ,
153
175
}
154
176
}
155
177
@@ -195,24 +217,32 @@ func (c *Config) SetGazelleManifest(gazelleManifest *manifest.Manifest) {
195
217
// name.
196
218
func (c * Config ) FindThirdPartyDependency (modName string ) (string , bool ) {
197
219
for currentCfg := c ; currentCfg != nil ; currentCfg = currentCfg .parent {
198
- if currentCfg .gazelleManifest != nil {
199
- gazelleManifest := currentCfg .gazelleManifest
200
- if distributionName , ok := gazelleManifest .ModulesMapping [modName ]; ok {
201
- var distributionRepositoryName string
202
- if gazelleManifest .PipDepsRepositoryName != "" {
203
- distributionRepositoryName = gazelleManifest .PipDepsRepositoryName
204
- } else if gazelleManifest .PipRepository != nil {
205
- distributionRepositoryName = gazelleManifest .PipRepository .Name
206
- }
207
- sanitizedDistribution := strings .ToLower (distributionName )
208
- sanitizedDistribution = strings .ReplaceAll (sanitizedDistribution , "-" , "_" )
209
- var lbl label.Label
210
- // @<repository_name>_<distribution_name>//:pkg
211
- distributionRepositoryName = distributionRepositoryName + "_" + sanitizedDistribution
212
- lbl = label .New (distributionRepositoryName , "" , "pkg" )
213
- return lbl .String (), true
214
- }
220
+
221
+ if currentCfg .gazelleManifest == nil {
222
+ continue
223
+ }
224
+
225
+ gazelleManifest := currentCfg .gazelleManifest
226
+ distributionName , ok := gazelleManifest .ModulesMapping [modName ]
227
+ if ! ok {
228
+ continue
215
229
}
230
+
231
+ var distributionRepositoryName string
232
+ if gazelleManifest .PipDepsRepositoryName != "" {
233
+ distributionRepositoryName = gazelleManifest .PipDepsRepositoryName
234
+ } else if gazelleManifest .PipRepository != nil {
235
+ distributionRepositoryName = gazelleManifest .PipRepository .Name
236
+ }
237
+ sanitizedDistribution := strings .ToLower (distributionName )
238
+ sanitizedDistribution = strings .ReplaceAll (sanitizedDistribution , "-" , "_" )
239
+
240
+ lbl := label .New (
241
+ currentCfg .renderPipRepository (distributionRepositoryName , sanitizedDistribution ),
242
+ currentCfg .renderPipPackage (sanitizedDistribution ),
243
+ currentCfg .renderPipTarget (sanitizedDistribution ),
244
+ )
245
+ return lbl .String (), true
216
246
}
217
247
return "" , false
218
248
}
@@ -327,6 +357,37 @@ func (c *Config) SetTestNamingConvention(testNamingConvention string) {
327
357
c .testNamingConvention = testNamingConvention
328
358
}
329
359
360
+ // SetPipRepoNamingConvention sets the dependency naming convention.
361
+ func (c * Config ) SetPipRepoNamingConvention (pipRepoNamingConvention string ) {
362
+ c .pipRepoNamingConvention = pipRepoNamingConvention
363
+ }
364
+
365
+ // Accepts sanitized input.
366
+ func (c * Config ) renderPipRepository (distributionRepoName , distributionName string ) string {
367
+ rendered := strings .ReplaceAll (c .pipRepoNamingConvention , repoNamePipRepoNamingConventionSubstitution , distributionRepoName )
368
+ return strings .ReplaceAll (rendered , distributionNamePipRepoNamingConventionSubstitution , distributionName )
369
+ }
370
+
371
+ // SetPipPackageNamingConvention sets the dependency naming convention.
372
+ func (c * Config ) SetPipPackageNamingConvention (pipPackageNamingConvention string ) {
373
+ c .pipPackageNamingConvention = pipPackageNamingConvention
374
+ }
375
+
376
+ // Accepts sanitized input.
377
+ func (c * Config ) renderPipPackage (distributionName string ) string {
378
+ return strings .ReplaceAll (c .pipPackageNamingConvention , distributionNamePipRepoNamingConventionSubstitution , distributionName )
379
+ }
380
+
381
+ // SetPipTargetNamingConvention sets the dependency naming convention.
382
+ func (c * Config ) SetPipTargetNamingConvention (pipTargetNamingConvention string ) {
383
+ c .pipTargetNamingConvention = pipTargetNamingConvention
384
+ }
385
+
386
+ // Accepts sanitized input.
387
+ func (c * Config ) renderPipTarget (distributionName string ) string {
388
+ return strings .ReplaceAll (c .pipTargetNamingConvention , distributionNamePipRepoNamingConventionSubstitution , distributionName )
389
+ }
390
+
330
391
// RenderTestName returns the py_test target name by performing all
331
392
// substitutions.
332
393
func (c * Config ) RenderTestName (packageName string ) string {
0 commit comments