Skip to content

Commit 372b617

Browse files
committed
Temporarily support deprecated namespace packages
1 parent 94d8157 commit 372b617

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

setuptools/command/editable_wheel.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import os
88
import shutil
99
import sys
10-
from distutils.core import Command
1110
from pathlib import Path
1211
from tempfile import TemporaryDirectory
1312

13+
from setuptools import Command
14+
from setuptools import namespaces
15+
1416

1517
class editable_wheel(Command):
1618
"""Build 'editable' wheel for development"""
@@ -39,7 +41,7 @@ def finalize_options(self):
3941
@property
4042
def target(self):
4143
package_dir = self.distribution.package_dir or {}
42-
return package_dir.get("") or self.project_dir
44+
return _normalize_path(package_dir.get("") or self.project_dir)
4345

4446
def run(self):
4547
self._ensure_dist_info()
@@ -65,6 +67,15 @@ def _ensure_dist_info(self):
6567
assert str(self.dist_info_dir).endswith(".dist-info")
6668
assert Path(self.dist_info_dir, "METADATA").exists()
6769

70+
def _install_namespaces(self, installation_dir, pth_prefix):
71+
# XXX: Only required to support the deprecated namespace practice
72+
dist = self.distribution
73+
if not dist.namespace_packages:
74+
return
75+
76+
installer = _NamespaceInstaller(dist, installation_dir, pth_prefix, self.target)
77+
installer.install_namespaces()
78+
6879
def _create_wheel_file(self, bdist_wheel):
6980
from wheel.wheelfile import WheelFile
7081

@@ -82,13 +93,13 @@ def _create_wheel_file(self, bdist_wheel):
8293
with TemporaryDirectory(suffix=archive_name) as tmp:
8394
tmp_dist_info = Path(tmp, Path(self.dist_info_dir).name)
8495
shutil.copytree(self.dist_info_dir, tmp_dist_info)
96+
self._install_namespaces(tmp, editable_name)
8597
self._populate_wheel(editable_name, tmp)
8698
with WheelFile(wheel_path, "w") as wf:
8799
wf.write_files(tmp)
88100

89101
return wheel_path
90102

91-
92103
def _best_strategy(self):
93104
if self.strict:
94105
return self._link_tree
@@ -107,8 +118,25 @@ def _best_strategy(self):
107118
# >>> self.dist.packages
108119

109120
def _populate_wheel(self, dist_id, unpacked_wheel_dir):
110-
pth = Path(unpacked_wheel_dir, f"_editable.{dist_id}.pth")
111-
pth.write_text(f"{_normalize_path(self.target)}\n", encoding="utf-8")
121+
pth = Path(unpacked_wheel_dir, f"__editable__.{dist_id}.pth")
122+
pth.write_text(f"{self.target}\n", encoding="utf-8")
123+
124+
125+
class _NamespaceInstaller(namespaces.Installer):
126+
def __init__(self, distribution, installation_dir, editable_name, src_root):
127+
self.distribution = distribution
128+
self.src_root = src_root
129+
self.installation_dir = installation_dir
130+
self.editable_name = editable_name
131+
self.outputs = []
132+
133+
def _get_target(self):
134+
"""Installation target."""
135+
return os.path.join(self.installation_dir, self.editable_name)
136+
137+
def _get_root(self):
138+
"""Where the modules/packages should be loaded from."""
139+
return repr(str(self.src_root))
112140

113141

114142
def _normalize_path(filename):

0 commit comments

Comments
 (0)