Skip to content

Commit 8e9f0be

Browse files
authored
feat: Introduce compatibility with native namespace packages (#232)
* feat: Introduce compatibility with native namespace packages * Add missing files * lint
1 parent 7df64c2 commit 8e9f0be

File tree

5 files changed

+56
-47
lines changed

5 files changed

+56
-47
lines changed

google/cloud/__init__.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

google/cloud/dns/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@
2424
(adding/deleting resource record sets) to a zone.
2525
"""
2626

27-
28-
from pkg_resources import get_distribution
29-
30-
__version__ = get_distribution("google-cloud-dns").version
31-
27+
from google.cloud.dns.version import __version__
3228
from google.cloud.dns.zone import Changes
3329
from google.cloud.dns.client import Client
3430
from google.cloud.dns.zone import ManagedZone
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Copyright 2016 Google LLC
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2023 Google LLC
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -11,12 +12,5 @@
1112
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1213
# See the License for the specific language governing permissions and
1314
# limitations under the License.
14-
15-
try:
16-
import pkg_resources
17-
18-
pkg_resources.declare_namespace(__name__)
19-
except ImportError:
20-
import pkgutil
21-
22-
__path__ = pkgutil.extend_path(__path__, __name__)
15+
#
16+
__version__ = "0.34.2"

setup.py

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

1515
import io
1616
import os
17+
import re
1718

1819
import setuptools
1920

@@ -22,7 +23,16 @@
2223

2324
name = "google-cloud-dns"
2425
description = "Google Cloud DNS API client library"
25-
version = "0.34.2"
26+
27+
package_root = os.path.abspath(os.path.dirname(__file__))
28+
29+
version = None
30+
31+
with open(os.path.join(package_root, "google/cloud/dns/version.py")) as fp:
32+
version_candidates = re.findall(r"(?<=\")\d+.\d+.\d+(?=\")", fp.read())
33+
assert len(version_candidates) == 1
34+
version = version_candidates[0]
35+
2636
# Should be one of:
2737
# 'Development Status :: 3 - Alpha'
2838
# 'Development Status :: 4 - Beta'
@@ -39,7 +49,6 @@
3949

4050
# Setup boilerplate below this line.
4151

42-
package_root = os.path.abspath(os.path.dirname(__file__))
4352

4453
readme_filename = os.path.join(package_root, "README.rst")
4554
with io.open(readme_filename, encoding="utf-8") as readme_file:
@@ -48,15 +57,11 @@
4857
# Only include packages under the 'google' namespace. Do not include tests,
4958
# benchmarks, etc.
5059
packages = [
51-
package for package in setuptools.find_packages() if package.startswith("google")
60+
package
61+
for package in setuptools.find_namespace_packages()
62+
if package.startswith("google")
5263
]
5364

54-
# Determine which namespaces are needed.
55-
namespaces = ["google"]
56-
if "google.cloud" in packages:
57-
namespaces.append("google.cloud")
58-
59-
6065
setuptools.setup(
6166
name=name,
6267
version=version,
@@ -81,7 +86,6 @@
8186
],
8287
platforms="Posix; MacOS X; Windows",
8388
packages=packages,
84-
namespace_packages=namespaces,
8589
install_requires=dependencies,
8690
extras_require=extras,
8791
python_requires=">=3.7",

tests/unit/test_packaging.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2023 Google LLC
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+
import os
16+
import subprocess
17+
import sys
18+
19+
20+
def test_namespace_package_compat(tmp_path):
21+
# The ``google`` namespace package should not be masked
22+
# by the presence of ``google-cloud-dns``.
23+
google = tmp_path / "google"
24+
google.mkdir()
25+
google.joinpath("othermod.py").write_text("")
26+
env = dict(os.environ, PYTHONPATH=str(tmp_path))
27+
cmd = [sys.executable, "-m", "google.othermod"]
28+
subprocess.check_call(cmd, env=env)
29+
30+
# The ``google.cloud`` namespace package should not be masked
31+
# by the presence of ``google-cloud-dns``.
32+
google_cloud = tmp_path / "google" / "cloud"
33+
google_cloud.mkdir()
34+
google_cloud.joinpath("othermod.py").write_text("")
35+
env = dict(os.environ, PYTHONPATH=str(tmp_path))
36+
cmd = [sys.executable, "-m", "google.cloud.othermod"]
37+
subprocess.check_call(cmd, env=env)

0 commit comments

Comments
 (0)