Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gazelle/python/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (py *Configurer) KnownDirectives() []string {
pythonconfig.ExperimentalAllowRelativeImports,
pythonconfig.GenerateProto,
pythonconfig.PythonResolveSiblingImports,
pythonconfig.PythonIgnoreTarget,
}
}

Expand Down Expand Up @@ -254,6 +255,8 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
log.Fatal(err)
}
config.SetResolveSiblingImports(v)
case pythonconfig.PythonIgnoreTarget:
config.AddIgnoreTarget(strings.TrimSpace(d.Value))
}
}

Expand Down
17 changes: 15 additions & 2 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,21 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
}
}
// filter out targets that are ignored
var otherFileRules []*rule.Rule
if args.File != nil {
for _, target := range args.File.Rules {
if cfg.IgnoreTarget(target.Name()) {
continue
}
otherFileRules = append(otherFileRules, target)
}
}

// If a __test__.py file was not found on disk, search for targets that are
// named __test__.
if !hasPyTestEntryPointFile && args.File != nil {
for _, rule := range args.File.Rules {
for _, rule := range otherFileRules {
if rule.Name() == pyTestEntrypointTargetname {
hasPyTestEntryPointTarget = true
break
Expand Down Expand Up @@ -279,7 +289,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
return
}
generateEmptyLibrary := false
for _, r := range args.File.Rules {
for _, r := range otherFileRules {
if r.Kind() == actualPyLibraryKind && r.Name() == pyLibraryTargetName {
generateEmptyLibrary = true
}
Expand Down Expand Up @@ -585,6 +595,9 @@ func generateProtoLibraries(args language.GenerateArgs, cfg *pythonconfig.Config
pyProtoRulesForProto := map[string]string{}
if args.File != nil {
for _, r := range args.File.Rules {
if cfg.IgnoreTarget(r.Name()) {
continue
}
if r.Kind() == "py_proto_library" {
pyProtoRules[r.Name()] = false

Expand Down
16 changes: 16 additions & 0 deletions gazelle/pythonconfig/pythonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ const (
// like "import a" can be resolved to sibling modules. When disabled, they
// can only be resolved as an absolute import.
PythonResolveSiblingImports = "python_resolve_sibling_imports"
// PythonIgnoreTarget represents the directive that controls whether to ignore the specific target.
PythonIgnoreTarget = "python_ignore_target"
)

// GenerationModeType represents one of the generation modes for the Python
Expand Down Expand Up @@ -204,6 +206,7 @@ type Config struct {
generatePyiDeps bool
generateProto bool
resolveSiblingImports bool
ignoredTargets map[string]struct{}
}

type LabelNormalizationType int
Expand Down Expand Up @@ -244,6 +247,7 @@ func New(
generatePyiDeps: false,
generateProto: false,
resolveSiblingImports: false,
ignoredTargets: make(map[string]struct{}),
}
}

Expand Down Expand Up @@ -281,6 +285,7 @@ func (c *Config) NewChild() *Config {
generatePyiDeps: c.generatePyiDeps,
generateProto: c.generateProto,
resolveSiblingImports: c.resolveSiblingImports,
ignoredTargets: make(map[string]struct{}),
}
}

Expand Down Expand Up @@ -610,6 +615,17 @@ func (c *Config) ResolveSiblingImports() bool {
return c.resolveSiblingImports
}

// SetIgnoreTarget sets the target to be ignored.
func (c *Config) AddIgnoreTarget(target string) {
c.ignoredTargets[target] = struct{}{}
}

// IgnoreTarget returns whether the target should be ignored.
func (c *Config) IgnoreTarget(target string) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this IsIgnoredTarget.

_, ignores := c.ignoredTargets[target]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder... should we support wildcards/globbing/regex? And how about the $python_root$ replacement?

# gazelle:python_ignore_target //$python_root$/foo/bar:*_foo

py_library(name = "bar_foo", ...)  # ignored
py_library(name = "bar_bar", ...)  # not ignored
py_library(name = "foo_foo", ...)  # ignored

Regex is probably too complex, especially if we want to support the $python_root$ replacement literal. But simple glob-like matching might be OK. There's precedence for globs, too: TestFilePattern uses it and we can make use of matchesAnyGlob.

return ignores
}

// FormatThirdPartyDependency returns a label to a third-party dependency performing all formating and normalization.
func (c *Config) FormatThirdPartyDependency(repositoryName string, distributionName string) label.Label {
conventionalDistributionName := strings.ReplaceAll(c.labelConvention, distributionNameLabelConventionSubstitution, distributionName)
Expand Down