Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce compatibility with native namespace packages #117

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 16 additions & 6 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@

BLACK_VERSION = "black==19.3b0"

CURRENT_DIRECTORY = Path(__file__).parent.absolute()

DEFAULT_PYTHON_VERSION = "3.8"

UNIT_TEST_PYTHON_VERSIONS = ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

# NOTE: Pin the version of grpcio-tools to 1.48.2 for compatibility with
# Protobuf 3.19.5. Please ensure that the minimum required version of
# protobuf in setup.py is compatible with the pb2 files generated
# by grpcio-tools before changing the pinned version below.
GRPCIO_TOOLS_VERSION = "grpcio-tools==1.48.2"


@nox.session(python="3.8")
@nox.session(python=DEFAULT_PYTHON_VERSION)
def blacken(session):
"""Run black.
Format code to uniform standard.
Expand All @@ -46,7 +52,7 @@ def blacken(session):



@nox.session(python="3.8")
@nox.session(python=DEFAULT_PYTHON_VERSION)
def generate_protos(session):
"""Generates the protos using protoc.
Some notes on the `google` directory:
Expand All @@ -70,7 +76,7 @@ def generate_protos(session):
*protos,
)

@nox.session(python="3.8")
@nox.session(python=DEFAULT_PYTHON_VERSION)
def lint_setup_py(session):
"""Verify that setup.py is valid"""
session.install("docutils", "pygments")
Expand All @@ -80,13 +86,17 @@ def lint_setup_py(session):
def default(session):
# Install all test dependencies, then install this package in-place.
session.install("mock", "pytest", "pytest-cov")
session.install("-e", ".")

constraints_path = str(
CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
)
session.install("-e", ".", "-c", constraints_path)

# Run py.test against the unit tests.
session.run(
"py.test",
"--quiet",
"--cov=google.cloud",
"--cov=google",
"--cov=tests.unit",
"--cov-append",
"--cov-config=.coveragerc",
Expand All @@ -96,7 +106,7 @@ def default(session):
*session.posargs,
)

@nox.session(python="3.8")
@nox.session(python=UNIT_TEST_PYTHON_VERSIONS)
def unit(session):
"""Run the unit test suite."""
default(session)
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os

import setuptools
from setuptools import setup, find_packages
from setuptools import find_namespace_packages

name = "google-cloud-audit-log"
description = "Google Cloud Audit Protos"
Expand Down Expand Up @@ -45,7 +45,6 @@
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
Expand All @@ -59,10 +58,9 @@
long_description_content_type="text/markdown",
install_requires=dependencies,
license="Apache-2.0",
packages=find_packages(),
packages=find_namespace_packages(exclude=("tests*", "testing*")),
package_data={"": ["*.proto"]},
python_requires=">=3.7",
namespace_packages=["google", "google.cloud"],
url="https://github.com/googleapis/python-audit-log",
include_package_data=True,
)
15 changes: 2 additions & 13 deletions google/__init__.py → testing/constraints-3.10.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
# -*- coding: utf-8 -*-
#
# Copyright 2020 Google LLC
# Copyright 2024 Google LLC
#
# 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
#
# https://www.apache.org/licenses/LICENSE-2.0
# 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.

try:
import pkg_resources

pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
15 changes: 2 additions & 13 deletions google/cloud/__init__.py → testing/constraints-3.11.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
# -*- coding: utf-8 -*-
#
# Copyright 2020 Google LLC
# Copyright 2024 Google LLC
#
# 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
#
# https://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#

Choose a reason for hiding this comment

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

nit: both http and https work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

http matches the license here which could be why we use that in templates such as https://github.com/googleapis/gapic-generator-python/blob/main/gapic/templates/_license.j2

# 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.

try:
import pkg_resources

pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
13 changes: 13 additions & 0 deletions testing/constraints-3.12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Google LLC
#
# 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.
22 changes: 22 additions & 0 deletions testing/constraints-3.7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2024 Google LLC
#
# 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.
#
# This constraints file is used to check that lower bounds
# are correct in setup.py
# List all library dependencies and extras in this file.
# Pin the version to the lower bound.
# e.g., if setup.py has "google-cloud-foo >= 1.14.0, < 2.0.0dev",
# Then this file should have google-cloud-foo==1.14.0
protobuf==3.19.5
googleapis-common-protos==1.56.2
13 changes: 13 additions & 0 deletions testing/constraints-3.8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Google LLC
#
# 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.
13 changes: 13 additions & 0 deletions testing/constraints-3.9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Google LLC
#
# 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.
37 changes: 37 additions & 0 deletions tests/unit/test_packaging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2023 Google LLC
#
# 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 os
import subprocess
import sys


def test_namespace_package_compat(tmp_path):

Choose a reason for hiding this comment

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

Q: how does the testing infra know that tmp path is a pathlib.Path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment in 433522c with more information about the tmp_path fixture of pytest

# The ``google`` namespace package should not be masked
# by the presence of ``google-cloud-audit-log``.
google = tmp_path / "google"
google.mkdir()
google.joinpath("othermod.py").write_text("")
env = dict(os.environ, PYTHONPATH=str(tmp_path))
cmd = [sys.executable, "-m", "google.othermod"]
subprocess.check_call(cmd, env=env)

# The ``google.cloud`` namespace package should not be masked
# by the presence of ``google-cloud-audit-log`.
google_cloud = tmp_path / "google" / "cloud"
google_cloud.mkdir()
google_cloud.joinpath("othermod.py").write_text("")
env = dict(os.environ, PYTHONPATH=str(tmp_path))
cmd = [sys.executable, "-m", "google.cloud.othermod"]
subprocess.check_call(cmd, env=env)
Loading