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
7 changes: 6 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ module(

# Do not update to newer versions until you need a specific new feature.
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_python", version = "1.7.0")
bazel_dep(name = "rules_python", version = "2.3.0")
bazel_dep(name = "bazel_skylib", version = "1.9.0")

# Do not generate __init__.py files for our py_binary and py_test targets.
# We ship them explicitly.
rules_python_config = use_extension("@rules_python//python/extensions:config.bzl", "config")
rules_python_config.explicit_init_py(default = True)

# Only for development
bazel_dep(name = "platforms", version = "1.0.0", dev_dependency = True)
bazel_dep(name = "rules_cc", version = "0.2.16", dev_dependency = True)
Expand Down
13 changes: 13 additions & 0 deletions pkg/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ filegroup(
visibility = ["//visibility:public"],
)

# The top level package marker. Everything importing from `pkg.` must depend on
# this, since we do not rely on implicit __init__.py creation.
py_library(
name = "init",
srcs = ["__init__.py"],
imports = [".."],
srcs_version = "PY3",
visibility = ["//:__subpackages__"],
)

# Used by pkg_rpm in rpm.bzl.
py_binary(
name = "make_rpm",
Expand All @@ -92,6 +102,7 @@ py_library(
"//tests:__subpackages__",
],
deps = [
":init",
"//pkg/private:archive",
"//pkg/private:build_info",
"//pkg/private:helpers",
Expand All @@ -108,8 +119,10 @@ py_binary(
py_library(
name = "filter_directory_lib",
srcs = ["filter_directory.py"],
imports = [".."],
srcs_version = "PY3",
visibility = ["//visibility:public"],
deps = [":init"],
)

exports_files(["verify_archive_test_main.py.tpl"])
27 changes: 16 additions & 11 deletions pkg/private/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,49 @@ config_setting(
visibility = ["//visibility:public"],
)

py_library(
name = "init",
srcs = ["__init__.py"],
imports = ["../.."],
srcs_version = "PY3",
visibility = ["//:__subpackages__"],
deps = ["//pkg:init"],
)

py_library(
name = "build_info",
srcs = [
"build_info.py",
],
srcs = ["build_info.py"],
imports = ["../.."],
srcs_version = "PY3",
visibility = [
"//:__subpackages__",
"//tests:__pkg__",
],
deps = [":init"],
)

py_library(
name = "archive",
srcs = [
"__init__.py",
"archive.py",
],
srcs = ["archive.py"],
imports = ["../.."],
srcs_version = "PY3",
visibility = [
"//:__subpackages__",
"//tests:__subpackages__",
],
deps = [":init"],
)

py_library(
name = "helpers",
srcs = [
"__init__.py",
"helpers.py",
],
srcs = ["helpers.py"],
imports = ["../.."],
srcs_version = "PY3",
visibility = [
"//:__subpackages__",
"//tests:__pkg__",
],
deps = [":init"],
)

py_library(
Expand All @@ -107,4 +111,5 @@ py_library(
imports = ["../.."],
srcs_version = "PY3",
visibility = ["//visibility:public"],
deps = [":init"],
)
10 changes: 8 additions & 2 deletions pkg/private/deb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ py_binary(

py_library(
name = "make_deb_lib",
srcs = ["make_deb.py"],
srcs = [
"__init__.py",
"make_deb.py",
],
imports = ["../../.."],
srcs_version = "PY3",
visibility = ["//tests/deb:__pkg__"],
visibility = [
"//tests:__pkg__",
"//tests/deb:__pkg__",
],
deps = [
"//pkg/private:helpers",
],
Expand Down
Empty file added pkg/private/deb/__init__.py
Empty file.
24 changes: 20 additions & 4 deletions pkg/private/install.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ def locate(short_path, repository):
return RUNFILES.Rlocation(posixpath.normpath(posixpath.join(repository, short_path)))


def _resolve_runfile(path):
"""Return the real file behind a runfile, so that it can be opened.

With --enable_runfiles on Windows, Bazel may create a directory junction
for a file runfile. Such a junction can not be opened, and
os.path.realpath fails on it, so we read the junction target instead.
"""
if os.name != "nt":
return os.path.realpath(path)
try:
target = os.readlink(path)
except OSError:
# Not a link.
return path
if target.startswith("\\\\?\\"):
target = target[4:]
return os.path.join(os.path.dirname(path), target)


# This is named "NativeInstaller" because it makes use of "native" python
# functionality for installing files that should be cross-platform.
#
Expand Down Expand Up @@ -81,10 +100,7 @@ class NativeInstaller(object):

def _do_file_copy(self, src, dest):
logging.debug("COPY %s <- %s", dest, src)
# Windows --enable_runfiles uses directory junctions for every runfile.
# Junctions cannot point at files, so open() fails with Permission denied.
# Follow the reparse point to the real bazel-out file.
src = os.path.realpath(src)
src = _resolve_runfile(src)
# Copy to a temporary directory and then move it to the destination.
# This ensures code-signed executables on certain platforms
# behave correctly.
Expand Down
2 changes: 2 additions & 0 deletions pkg/private/tar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ py_binary(
py_library(
name = "tar_writer",
srcs = [
"__init__.py",
"tar_writer.py",
],
imports = ["../../.."],
srcs_version = "PY3",
visibility = [
"//tests:__subpackages__",
],
deps = ["//pkg/private:init"],
)
Empty file added pkg/private/tar/__init__.py
Empty file.
1 change: 1 addition & 0 deletions pkg/releasing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ py_library(
],
imports = ["../.."],
srcs_version = "PY3",
deps = ["//pkg:init"],
)

py_binary(
Expand Down
23 changes: 22 additions & 1 deletion pkg/verify_archive_test_main.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,31 @@
# limitations under the License.
"""Tests for generated content manifest."""

import os
import re
import tarfile
import unittest


def _resolve_junction(path):
"""Returns the target of path if it is a Windows junction, else path.

With --enable_runfiles on Windows, Bazel may create a directory junction
for a file runfile. Such a junction can not be opened, and
os.path.realpath fails on it, so we read the junction target instead.
"""
if os.name != 'nt':
return path
try:
target = os.readlink(path)
except OSError:
# Not a link.
return path
if target.startswith('\\\\?\\'):
target = target[4:]
return os.path.join(os.path.dirname(path), target)


class VerifyArchiveTest(unittest.TestCase):
"""Test harness to see if we wrote the content manifest correctly."""

Expand All @@ -37,7 +58,7 @@ class VerifyArchiveTest(unittest.TestCase):
def load_tar(self, path):
self.paths = []
self.links = {}
with tarfile.open(path, 'r:*') as f:
with tarfile.open(_resolve_junction(path), 'r:*') as f:
i = 0
for info in f:
self.paths.append(info.name)
Expand Down
13 changes: 13 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,22 @@ py_test(
srcs_version = "PY3",
deps = [
"//pkg/private:archive",
"//tests/util:runfiles_util",
"@rules_python//python/runfiles",
],
)

py_test(
name = "explicit_init_test",
srcs = ["explicit_init_test.py"],
python_version = "PY3",
srcs_version = "PY3",
deps = [
"//pkg/private/deb:make_deb_lib",
"//pkg/private/tar:tar_writer",
],
)

py_test(
name = "path_test",
srcs = ["path_test.py"],
Expand Down Expand Up @@ -216,6 +228,7 @@ py_test(
],
python_version = "PY3",
deps = [
"//tests/util:runfiles_util",
"@rules_python//python/runfiles",
],
)
Expand Down
6 changes: 4 additions & 2 deletions tests/archive_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import unittest

from python.runfiles import runfiles
from tests.util import runfiles_util
from pkg.private import archive


Expand Down Expand Up @@ -62,11 +63,12 @@ def assertArFileContent(self, arfile, content):

def testEmptyArFile(self):
self.assertArFileContent(
self.data_files.Rlocation("rules_pkg/tests/testdata/empty.ar"),
runfiles_util.rlocation(
self.data_files, "rules_pkg/tests/testdata/empty.ar"),
[])

def assertSimpleFileContent(self, names):
datafile = self.data_files.Rlocation(
datafile = runfiles_util.rlocation(self.data_files,
"rules_pkg/tests/testdata/" + "_".join(names) + ".ar")
# pylint: disable=g-complex-comprehension
content = [{"filename": n,
Expand Down
2 changes: 2 additions & 0 deletions tests/deb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ py_test(
python_version = "PY3",
deps = [
"//pkg/private:archive",
"//tests/util:runfiles_util",
"@rules_python//python/runfiles",
],
)
Expand Down Expand Up @@ -204,6 +205,7 @@ py_test(
python_version = "PY3",
deps = [
"//pkg/private:archive",
"//tests/util:runfiles_util",
"@rules_python//python/runfiles",
],
)
8 changes: 5 additions & 3 deletions tests/deb/pkg_deb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import unittest

from python.runfiles import runfiles
from tests.util import runfiles_util
from pkg.private import archive


Expand Down Expand Up @@ -63,9 +64,10 @@ def setUp(self):
super(PkgDebTest, self).setUp()
self.runfiles = runfiles.Create()
# Note: Rlocation requires forward slashes. os.path.join() will not work.
self.deb_path = self.runfiles.Rlocation('rules_pkg/tests/deb/fizzbuzz_4.5.6_all.deb')
self.deb_path = runfiles_util.rlocation(
self.runfiles, 'rules_pkg/tests/deb/fizzbuzz_4.5.6_all.deb')
self.deb_file = DebInspect(self.deb_path)
self.installed_size_file_deb_path = self.runfiles.Rlocation(
self.installed_size_file_deb_path = runfiles_util.rlocation(self.runfiles,
'rules_pkg/tests/deb/fizzbuzz-installed-size-file_1.0.0_all.deb')
self.installed_size_file_deb_file = DebInspect(self.installed_size_file_deb_path)

Expand Down Expand Up @@ -235,7 +237,7 @@ def test_triggers(self):
'some-trigger\n')

def test_changes(self):
changes_path = self.runfiles.Rlocation(
changes_path = runfiles_util.rlocation(self.runfiles,
'rules_pkg/tests/deb/fizzbuzz_4.5.6_all.changes')
with open(changes_path, 'r', encoding='utf-8') as f:
content = f.read()
Expand Down
3 changes: 2 additions & 1 deletion tests/deb/pkg_deb_variables_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import unittest

from python.runfiles import runfiles
from tests.util import runfiles_util
from pkg.private import archive


Expand Down Expand Up @@ -62,7 +63,7 @@ def setUp(self):
# my_package_variables provides arch=target_arch, label="some_value", so:
# package = "pkg-$(label)" -> "pkg-some_value"
# architecture = "$(arch)" -> "target_arch"
deb_path = self.runfiles.Rlocation(
deb_path = runfiles_util.rlocation(self.runfiles,
"rules_pkg/tests/deb/pkg-some_value_1.0_target_arch.deb"
)
self.deb_file = DebInspect(deb_path)
Expand Down
38 changes: 38 additions & 0 deletions tests/explicit_init_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2026 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.
"""Checks that our Python packages ship explicit __init__.py files.

This test runs with legacy_create_init = 0, so any package whose
__init__.py is not in the runfiles would be imported as an implicit
namespace package, which has no __file__.
"""

import importlib
import unittest


class ExplicitInitTest(unittest.TestCase):

def test_packages_are_regular_packages(self):
for name in ("pkg", "pkg.private", "pkg.private.deb", "pkg.private.tar"):
with self.subTest(package=name):
module = importlib.import_module(name)
self.assertIsNotNone(
module.__file__,
"%s is a namespace package; add its __init__.py to srcs" % name,
)


if __name__ == "__main__":
unittest.main()
Loading
Loading