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
62 changes: 60 additions & 2 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,41 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
result.Imports = append(result.Imports, pyLibrary.PrivateAttr(config.GazelleImportsKey))
}
}

ruleBySrcs := make(map[string]string)
srcsByRule := make(map[string]*treeset.Set)
if args.File != nil {
for _, r := range args.File.Rules {
kindName := r.Kind()
// if kindInfo, ok := args.Config.KindMap[kindName]; ok {
// kindName = kindInfo.KindName
// }

if kindName == actualPyLibraryKind {
srcsList := r.AttrStrings("srcs")
for _, src := range srcsList {
ruleBySrcs[src] = r.Name()
if srcsByRule[r.Name()] == nil {
srcsByRule[r.Name()] = treeset.NewWith(godsutils.StringComparator)
}
srcsByRule[r.Name()].Add(src)
}
}
}
}

if cfg.PerFileGeneration() {
// Handle new py_library targets
hasInit, nonEmptyInit := hasLibraryEntrypointFile(args.Dir)
pyLibraryFilenames.Each(func(index int, filename interface{}) {
pyLibraryTargetName := strings.TrimSuffix(filepath.Base(filename.(string)), ".py")
var pyLibraryTargetName string
if _, ok := ruleBySrcs[filename.(string)]; ok {
return
// pyLibraryTargetName = strings.TrimSuffix(filepath.Base(filename.(string)), ".py")
} else {
}
pyLibraryTargetName = strings.TrimSuffix(filepath.Base(filename.(string)), ".py")

if filename == pyLibraryEntrypointFilename && !nonEmptyInit {
return // ignore empty __init__.py.
}
Expand All @@ -323,8 +354,35 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
appendPyLibrary(srcs, pyLibraryTargetName)
})

// Handle existing py_library targets
for rule := range srcsByRule {
srcs := srcsByRule[rule]
appendPyLibrary(srcs, rule)
}

} else {
appendPyLibrary(pyLibraryFilenames, cfg.RenderLibraryName(packageName))

// Handle existing py_library targets
for rule := range srcsByRule {
srcs := srcsByRule[rule]
appendPyLibrary(srcs, rule)
}

// Handle remaining files in one py_library target
// Create a new set with all the files not yet included in any rule
remaining := treeset.NewWith(godsutils.StringComparator)
pyLibraryFilenames.Each(func(index int, value interface{}) {
filename := value.(string)
if _, ok := ruleBySrcs[filename]; !ok {
remaining.Add(filename)
}
})

if !remaining.Empty() {
appendPyLibrary(remaining, cfg.RenderLibraryName(packageName))
}
// appendPyLibrary(pyLibraryFilenames, cfg.RenderLibraryName(packageName))
}

if hasPyBinaryEntryPointFile {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_python//python:defs.bzl", "py_library")

# gazelle:python_generation_mode file

# This target should be maintained by gazelle (but should get a new deps).
py_library(
name = "custom",
srcs = ["bar.py", "baz.py"],
visibility = ["//visibility:private"],
tags = ["cant_touch_this"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")

# gazelle:python_generation_mode file

# This target should be maintained by gazelle (but should get a new deps).
py_library(
name = "custom",
srcs = [
"bar.py",
"baz.py",
],
tags = ["cant_touch_this"],
visibility = ["//visibility:private"],
deps = [":foo"],
)

py_library(
name = "foo",
srcs = ["foo.py"],
visibility = ["//:__subpackages__"],
)

py_test(
name = "bar_test",
srcs = ["bar_test.py"],
deps = [":custom"],
)

py_test(
name = "foo_test",
srcs = ["foo_test.py"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Per-file generation with existing target spanning multiple files

This test case generates one `py_library` per file, but has an existing target containing 2 files. In this
case, the existing target should be preserved (and used for the existing 2 files), but new targets should be
created for new files.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a Bazel workspace for the Gazelle test data.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# For test purposes only.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@rules_python//python:defs.bzl", "py_library")

# gazelle:python_generation_mode project

# This target should be maintained and unchangedby gazelle
py_library(
name = "__init__",
srcs = ["__init__.py"],
visibility = ["//visibility:private"],
)

# This target should be maintained by gazelle (but should get a new deps).
py_library(
name = "custom",
srcs = ["bar.py", "baz.py"],
visibility = ["//visibility:private"],
tags = ["cant_touch_this"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")

# gazelle:python_generation_mode project

# This target should be maintained and unchangedby gazelle
py_library(
name = "__init__",
srcs = ["__init__.py"],
visibility = ["//visibility:private"],
)

# This target should be maintained by gazelle (but should get a new deps).
py_library(
name = "custom",
srcs = [
"bar.py",
"baz.py",
],
tags = ["cant_touch_this"],
visibility = ["//visibility:private"],
deps = [":project_generation_mode_respect_existing_multiple_srcs"],
)

py_library(
name = "project_generation_mode_respect_existing_multiple_srcs",
srcs = ["foo.py"],
visibility = ["//:__subpackages__"],
)

py_test(
name = "project_generation_mode_respect_existing_multiple_srcs_test",
srcs = [
"bar_test.py",
"foo_test.py",
],
deps = [":custom"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Project generation with existing target spanning multiple files

This test generates targets according to project mode, but it respects existing targets that have been created.
This should make it easier to incrementally migrate to project mode.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a Bazel workspace for the Gazelle test data.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# For test purposes only.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---