Skip to content

Commit aa8d59b

Browse files
committed
Allow spaces in whl_librarys
1 parent 6babe59 commit aa8d59b

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

python/private/glob_excludes.bzl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"Utilities for glob exclusions."
16+
17+
def _version_dependent_exclusions():
18+
"""Returns glob exclusions that are sensitive to Bazel version.
19+
20+
Bazel 7.4.0+ added support for files with spaces. Prior versions of Bazel
21+
do not support files with spaces.
22+
23+
Returns:
24+
a list of glob exclusion patterns
25+
"""
26+
major, minor, _ = native.bazel_version.split(".")
27+
if major < 7 or (major == 7 and minor < 4):
28+
return ["**/* *"]
29+
else:
30+
return []
31+
32+
glob_excludes = struct(
33+
version_dependent_exclusions = _version_dependent_exclusions,
34+
)

python/private/hermetic_runtime_repo_setup.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library")
1717
load("//python:py_runtime.bzl", "py_runtime")
1818
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
1919
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
20+
load(":glob_excludes.bzl", "glob_excludes")
2021
load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
2122
load(":semver.bzl", "semver")
2223

@@ -64,7 +65,6 @@ def define_hermetic_runtime_toolchain_impl(
6465
# Platform-agnostic filegroup can't match on all patterns.
6566
allow_empty = True,
6667
exclude = [
67-
"**/* *", # Bazel does not support spaces in file names.
6868
# Unused shared libraries. `python` executable and the `:libpython` target
6969
# depend on `libpython{python_version}.so.1.0`.
7070
"lib/libpython{major}.{minor}.so".format(**version_dict),
@@ -74,7 +74,7 @@ def define_hermetic_runtime_toolchain_impl(
7474
"lib/python{major}.{minor}/**/test/**".format(**version_dict),
7575
"lib/python{major}.{minor}/**/tests/**".format(**version_dict),
7676
"**/__pycache__/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
77-
] + extra_files_glob_exclude,
77+
] + glob_excludes.version_dependent_exclusions() + extra_files_glob_exclude,
7878
),
7979
)
8080
cc_import(

python/private/pypi/deps.bzl

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1818
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
19+
load("//python/private:glob_excludes.bzl", "glob_excludes")
1920

2021
_RULE_DEPS = [
2122
# START: maintained by 'bazel run //tools/private/update_deps:update_pip_deps'
@@ -97,6 +98,31 @@ _RULE_DEPS = [
9798
# END: maintained by 'bazel run //tools/private/update_deps:update_pip_deps'
9899
]
99100

101+
_EXCLUSIONS = [
102+
("**/*.py", None),
103+
("**/*.pyc", None),
104+
("**/*.pyc.*", "During pyc creation, temp files named *.pyc.NNN are created"),
105+
("**/*.dist-info/RECORD", None),
106+
("BUILD", None),
107+
("WORKSPACE", None),
108+
] + [(e, None) for e in glob_excludes.version_dependent_exclusions()]
109+
110+
def _to_comment(comment):
111+
if comment:
112+
return " #" + comment
113+
return ""
114+
115+
def _format_exclusions(exclusions, indent_depth):
116+
indent = " " * indent_depth
117+
return "\n".join([
118+
"{indent}{excl},{comment}".format(
119+
indent = indent,
120+
excl = excl,
121+
comment = _to_comment(comment),
122+
)
123+
for excl, comment in exclusions
124+
])
125+
100126
_GENERIC_WHEEL = """\
101127
package(default_visibility = ["//visibility:public"])
102128
@@ -105,22 +131,19 @@ load("@rules_python//python:defs.bzl", "py_library")
105131
py_library(
106132
name = "lib",
107133
srcs = glob(["**/*.py"]),
108-
data = glob(["**/*"], exclude=[
134+
data = glob(
135+
["**/*"],
109136
# These entries include those put into user-installed dependencies by
110137
# data_exclude to avoid non-determinism.
111-
"**/*.py",
112-
"**/*.pyc",
113-
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
114-
"**/* *",
115-
"**/*.dist-info/RECORD",
116-
"BUILD",
117-
"WORKSPACE",
118-
]),
138+
exclude=[
139+
{exclusions}
140+
],
141+
),
119142
# This makes this directory a top-level in the python import
120143
# search path for anything that depends on this.
121144
imports = ["."],
122145
)
123-
"""
146+
""".format(exclusions = _format_exclusions(_EXCLUSIONS, 12))
124147

125148
# Collate all the repository names so they can be easily consumed
126149
all_repo_names = [name for (name, _, _) in _RULE_DEPS]

python/private/pypi/generate_whl_library_build_bazel.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Generate the BUILD.bazel contents for a repo defined by a whl_library."""
1616

17+
load("//python/private:glob_excludes.bzl", "glob_excludes")
1718
load("//python/private:normalize_name.bzl", "normalize_name")
1819
load("//python/private:text_util.bzl", "render")
1920
load(
@@ -257,15 +258,14 @@ def generate_whl_library_build_bazel(
257258
additional_content.append(annotation.additive_build_content)
258259

259260
_data_exclude = [
260-
"**/* *",
261261
"**/*.py",
262262
"**/*.pyc",
263263
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNNN are created
264264
# RECORD is known to contain sha256 checksums of files which might include the checksums
265265
# of generated files produced when wheels are installed. The file is ignored to avoid
266266
# Bazel caching issues.
267267
"**/*.dist-info/RECORD",
268-
]
268+
] + glob_excludes.version_dependent_exclusions()
269269
for item in data_exclude:
270270
if item not in _data_exclude:
271271
_data_exclude.append(item)

0 commit comments

Comments
 (0)