From 479e49da8a023d843755ce3168a0d05180aa8c87 Mon Sep 17 00:00:00 2001 From: pdmurray Date: Wed, 20 Aug 2025 17:14:00 -0700 Subject: [PATCH 1/4] Ship the raw protocol buffers with the wheel --- .gitignore | 10 ++++++++++ setup.py | 3 +++ 2 files changed, 13 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e1ba2cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Ignore bazel build artifacts +MODULE.bazel +MODULE.bazel.lock +bazel-bin +bazel-metadata +bazel-out +bazel-testlogs +build/ +tensorflow_metadata.egg-info/ +tensorflow_metadata/proto/v0/*.py diff --git a/setup.py b/setup.py index 3c3bf72..357f6e7 100644 --- a/setup.py +++ b/setup.py @@ -157,4 +157,7 @@ def run(self): "build": _BuildCommand, "bazel_build": _BazelBuildCommand, }, + package_data={ + "tensorflow_metadata.protobuf.v0": ["tensorflow_metadata/proto/v0/*.proto"] + } ) From 713b1045fe270a156968f5ddf1a1f0a1d0d3048d Mon Sep 17 00:00:00 2001 From: pdmurray Date: Wed, 20 Aug 2025 23:43:18 -0700 Subject: [PATCH 2/4] Add .bazelversion; modify setup.py to include `.proto` files --- .bazelversion | 1 + setup.py | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 .bazelversion diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..e8be684 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +7.6.1 diff --git a/setup.py b/setup.py index 357f6e7..f295eea 100644 --- a/setup.py +++ b/setup.py @@ -146,7 +146,6 @@ def run(self): "pytest>=8,<9", ], }, - include_package_data=True, description="Library and standards for schema and statistics.", long_description=_LONG_DESCRIPTION, long_description_content_type="text/markdown", @@ -158,6 +157,6 @@ def run(self): "bazel_build": _BazelBuildCommand, }, package_data={ - "tensorflow_metadata.protobuf.v0": ["tensorflow_metadata/proto/v0/*.proto"] + "tensorflow_metadata.proto.v0": ["*.proto"] } ) From 08a80fbf1723c407353258e339856b1e7a183a4a Mon Sep 17 00:00:00 2001 From: pdmurray Date: Thu, 21 Aug 2025 08:13:35 -0700 Subject: [PATCH 3/4] Finish implementation --- .gitignore | 2 +- setup.py | 60 +++++------------------- tensorflow_metadata/proto/v0/__init__.py | 13 +++++ 3 files changed, 26 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index e1ba2cd..4ca3625 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ bazel-out bazel-testlogs build/ tensorflow_metadata.egg-info/ -tensorflow_metadata/proto/v0/*.py +tensorflow_metadata/proto/v0/*_pb2.py diff --git a/setup.py b/setup.py index f295eea..4909d97 100644 --- a/setup.py +++ b/setup.py @@ -13,55 +13,20 @@ # limitations under the License. """Package Setup script for tf.Metadata.""" -import os +import pathlib import platform import shutil import subprocess -# pylint: disable=g-bad-import-order -# It is recommended to import setuptools prior to importing distutils to avoid -# using legacy behavior from distutils. -# https://setuptools.readthedocs.io/en/latest/history.html#v48-0-0 -from distutils.command import build - -import setuptools from setuptools import find_packages, setup +from setuptools.command import build_py -# pylint: enable=g-bad-import-order - - -class _BuildCommand(build.build): - """Build everything that is needed to install. - - This overrides the original distutils "build" command to to run bazel_build - command before any sub_commands. - - build command is also invoked from bdist_wheel and install command, therefore - this implementation covers the following commands: - - pip install . (which invokes bdist_wheel) - - python setup.py install (which invokes install command) - - python setup.py bdist_wheel (which invokes bdist_wheel command) - """ - - def _build_cc_extensions(self): - return True - - # Add "bazel_build" command as the first sub_command of "build". Each - # sub_command of "build" (e.g. "build_py", "build_ext", etc.) is executed - # sequentially when running a "build" command, if the second item in the tuple - # (predicate method) is evaluated to true. - sub_commands = [ - ("bazel_build", _build_cc_extensions), - ] + build.build.sub_commands - -class _BazelBuildCommand(setuptools.Command): - """Build Bazel artifacts and move generated files to the .""" +class _BazelBuildCommand(build_py.build_py): + """Build Bazel artifacts and move generated files to the source directory.""" def initialize_options(self): - pass - - def finalize_options(self): + super().initialize_options() self._bazel_cmd = shutil.which("bazel") if not self._bazel_cmd: raise RuntimeError( @@ -69,10 +34,11 @@ def finalize_options(self): "https://docs.bazel.build/versions/master/install.html for " "installation instruction." ) + + self._additional_build_options = [] if platform.system() == "Windows": self._additional_build_options = ["--copt=-DWIN32_LEAN_AND_MEAN"] - else: - self._additional_build_options = [] + def run(self): subprocess.check_call( @@ -86,9 +52,11 @@ def run(self): ], # Bazel should be invoked in a directory containing bazel WORKSPACE # file, which is the root directory. - cwd=os.path.dirname(os.path.realpath(__file__)), + cwd=str(pathlib.Path(__file__).parent), ) + super().run() + with open("tensorflow_metadata/version.py") as fp: globals_dict = {} @@ -117,7 +85,6 @@ def run(self): "Intended Audience :: Developers", "Intended Audience :: Education", "Intended Audience :: Science/Research", - "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3", @@ -152,10 +119,7 @@ def run(self): keywords="tensorflow metadata tfx", download_url="https://github.com/tensorflow/metadata/tags", requires=[], - cmdclass={ - "build": _BuildCommand, - "bazel_build": _BazelBuildCommand, - }, + cmdclass={"build_py": _BazelBuildCommand}, package_data={ "tensorflow_metadata.proto.v0": ["*.proto"] } diff --git a/tensorflow_metadata/proto/v0/__init__.py b/tensorflow_metadata/proto/v0/__init__.py index 290e3b0..a09fbf3 100644 --- a/tensorflow_metadata/proto/v0/__init__.py +++ b/tensorflow_metadata/proto/v0/__init__.py @@ -12,3 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. """Init module for tf.Metadata v0 protos.""" + +import pathlib + + +def get_protocol_buffer_path() -> pathlib.Path: + """Get the path to the directory containing the protocol buffers. + + Returns + ------- + pathlib.Path + Path to the directory containing *.proto files + """ + return pathlib.Path(__file__).parent From 585d8f7afe65144bb4614cf3f40acaf2043fb47e Mon Sep 17 00:00:00 2001 From: pdmurray Date: Thu, 21 Aug 2025 08:55:32 -0700 Subject: [PATCH 4/4] Ignore incompatible ruff rule; run pre-commit --- ruff.toml | 1 + setup.py | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ruff.toml b/ruff.toml index b4a3fce..4237c1e 100644 --- a/ruff.toml +++ b/ruff.toml @@ -52,6 +52,7 @@ ignore = [ "D105", # Missing docstring in magic method "D203", # 1 blank line before after class docstring "D204", # 1 blank line required after class docstring + "D213", # Make docstrings start on second line "D413", # 1 blank line after parameters "SIM108", # Simplify if/else to one line; not always clearer "D206", # Docstrings should be indented with spaces; unnecessary when running ruff-format diff --git a/setup.py b/setup.py index 4909d97..12b9455 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,6 @@ def initialize_options(self): if platform.system() == "Windows": self._additional_build_options = ["--copt=-DWIN32_LEAN_AND_MEAN"] - def run(self): subprocess.check_call( [ @@ -120,7 +119,5 @@ def run(self): download_url="https://github.com/tensorflow/metadata/tags", requires=[], cmdclass={"build_py": _BazelBuildCommand}, - package_data={ - "tensorflow_metadata.proto.v0": ["*.proto"] - } + package_data={"tensorflow_metadata.proto.v0": ["*.proto"]}, )