7
7
import os
8
8
import shutil
9
9
import sys
10
- from distutils .core import Command
11
10
from pathlib import Path
12
11
from tempfile import TemporaryDirectory
13
12
13
+ from setuptools import Command
14
+ from setuptools import namespaces
15
+
14
16
15
17
class editable_wheel (Command ):
16
18
"""Build 'editable' wheel for development"""
@@ -39,7 +41,7 @@ def finalize_options(self):
39
41
@property
40
42
def target (self ):
41
43
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 )
43
45
44
46
def run (self ):
45
47
self ._ensure_dist_info ()
@@ -65,6 +67,15 @@ def _ensure_dist_info(self):
65
67
assert str (self .dist_info_dir ).endswith (".dist-info" )
66
68
assert Path (self .dist_info_dir , "METADATA" ).exists ()
67
69
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
+
68
79
def _create_wheel_file (self , bdist_wheel ):
69
80
from wheel .wheelfile import WheelFile
70
81
@@ -82,13 +93,13 @@ def _create_wheel_file(self, bdist_wheel):
82
93
with TemporaryDirectory (suffix = archive_name ) as tmp :
83
94
tmp_dist_info = Path (tmp , Path (self .dist_info_dir ).name )
84
95
shutil .copytree (self .dist_info_dir , tmp_dist_info )
96
+ self ._install_namespaces (tmp , editable_name )
85
97
self ._populate_wheel (editable_name , tmp )
86
98
with WheelFile (wheel_path , "w" ) as wf :
87
99
wf .write_files (tmp )
88
100
89
101
return wheel_path
90
102
91
-
92
103
def _best_strategy (self ):
93
104
if self .strict :
94
105
return self ._link_tree
@@ -107,8 +118,25 @@ def _best_strategy(self):
107
118
# >>> self.dist.packages
108
119
109
120
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 ))
112
140
113
141
114
142
def _normalize_path (filename ):
0 commit comments