8000 Allow configuring the pip dependency naming convention · bazel-contrib/rules_python@4fd35ad · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4fd35ad

Browse files
committed
Allow configuring the pip dependency naming convention
1 parent 0a5f79f commit 4fd35ad

File tree

4 files changed

+113
-38
lines changed

4 files changed

+113
-38
lines changed

examples/bzlmod_build_file_generation/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ gazelle(
5656
gazelle = "@rules_python//gazelle:gazelle_python_binary",
5757
)
5858

59+
# Using bzlmod we also need to set a different pip repository naming convention:
60+
#
61+
# gazelle:python_pip_repo_naming_convention $repo_name$
62+
# gazelle:python_pip_target_naming_convention $distribution_name$_pkg
63+
5964
# This rule is auto-generated and managed by Gazelle,
6065
# because it found the __init__.py file in this folder.
6166
# See: https://bazel.build/reference/be/python#py_library

gazelle/configure.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ func (py *Configurer) KnownDirectives() []string {
4848
pythonconfig.LibraryNamingConvention,
4949
pythonconfig.BinaryNamingConvention,
5050
pythonconfig.TestNamingConvention,
51+
pythonconfig.PipRepoNamingConvention,
52+
pythonconfig.PipPackageNamingConvention,
53+
pythonconfig.PipTargetNamingConvention,
5154
}
5255
}
5356

@@ -136,6 +139,12 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
136139
config.SetBinaryNamingConvention(strings.TrimSpace(d.Value))
137140
case pythonconfig.TestNamingConvention:
138141
config.SetTestNamingConvention(strings.TrimSpace(d.Value))
142+
case pythonconfig.PipRepoNamingConvention:
143+
config.SetPipRepoNamingConvention(strings.TrimSpace(d.Value))
144+
case pythonconfig.PipPackageNamingConvention:
145+
config.SetPipPackageNamingConvention(strings.TrimSpace(d.Value))
146+
case pythonconfig.PipTargetNamingConvention:
147+
config.SetPipTargetNamingConvention(strings.TrimSpace(d.Value))
139148
}
140149
}
141150

gazelle/pythonconfig/pythonconfig.go

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ const (
4949
// naming convention. See python_library_naming_convention for more info on
5050
// the package name interpolation.
5151
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"
5263
)
5364

5465
// GenerationModeType represents one of the generation modes for the Python
@@ -67,7 +78,9 @@ const (
6778
)
6879

6980
const (
70-
packageNameNamingConventionSubstitution = "$package_name$"
81+
repoNamePipRepoNamingConventionSubstitution = "$repo_name$"
82+
distributionNamePipRepoNamingConventionSubstitution = "$distribution_name$"
83+
packageNameNamingConventionSubstitution = "$package_name$"
7184
)
7285

7386
// defaultIgnoreFiles is the list of default values used in the
@@ -99,14 +112,17 @@ type Config struct {
99112
pythonProjectRoot string
100113
gazelleManifest *manifest.Manifest
101114

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
110126
}
111127

112128
// New creates a new Config.
@@ -115,17 +131,20 @@ func New(
115131
pythonProjectRoot string,
116132
) *Config {
117133
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",
129148
}
130149
}
131150

@@ -150,6 +169,9 @@ func (c *Config) NewChild() *Config {
150169
libraryNamingConvention: c.libraryNamingConvention,
151170
binaryNamingConvention: c.binaryNamingConvention,
152171
testNamingConvention: c.testNamingConvention,
172+
pipRepoNamingConvention: c.pipRepoNamingConvention,
173+
pipPackageNamingConvention: c.pipPackageNamingConvention,
174+
pipTargetNamingConvention: c.pipTargetNamingConvention,
153175
}
154176
}
155177

@@ -195,24 +217,32 @@ func (c *Config) SetGazelleManifest(gazelleManifest *manifest.Manifest) {
195217
// name.
196218
func (c *Config) FindThirdPartyDependency(modName string) (string, bool) {
197219
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
215229
}
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
216246
}
217247
return "", false
218248
}
@@ -327,6 +357,37 @@ func (c *Config) SetTestNamingConvention(testNamingConvention string) {
327357
c.testNamingConvention = testNamingConvention
328358
}
329359

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+
330391
// RenderTestName returns the py_test target name by performing all
331392
// substitutions.
332393
func (c *Config) RenderTestName(packageName string) string {

gazelle/pythonconfig/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,4 @@ func (sml *StringMapList) Set(s string) error {
100100
func (sml *StringMapList) Get(key string) (string, bool) {
101101
val, exists := sml.mapping[key]
102102
return val, exists
103-
}
103+
}

0 commit comments

Comments
 (0)
0