55from os import fspath
66from pathlib import Path
77
8+ from setuptools import Command
89from setuptools import find_packages
910from setuptools import setup
1011from setuptools .command .develop import develop
1112from setuptools .command .easy_install import easy_install
13+ from setuptools .command .editable_wheel import editable_wheel
1214from setuptools .command .install_lib import install_lib
1315
1416pth_file = Path (__file__ ).parent .joinpath ('src' , 'manhole.pth' )
@@ -20,6 +22,26 @@ def run(self):
2022 self .copy_file (fspath (pth_file ), fspath (Path (self .build_lib , pth_file .name )))
2123
2224
25+ class PTHWheelPiggyback :
26+ def __init__ (self , strategy ):
27+ self .strategy = strategy
28+
29+ def __enter__ (self ):
30+ self .strategy .__enter__ ()
31+
32+ def __exit__ (self , exc_type , exc_val , exc_tb ):
33+ self .strategy .__exit__ (exc_type , exc_val , exc_tb )
34+
35+ def __call__ (self , wheel , files , mapping ):
36+ self .strategy (wheel , files , mapping )
37+ wheel .writestr (fspath (pth_file .name ), pth_file .read_bytes ())
38+
39+
40+ class EditableWheelWithPTH (editable_wheel ):
41+ def _select_strategy (self , dist_name , tag , lib ):
42+ return PTHWheelPiggyback (super ()._select_strategy (dist_name , tag , lib ))
43+
44+
2345class EasyInstallWithPTH (easy_install ):
2446 def run (self , * args , ** kwargs ):
2547 super ().run (* args , ** kwargs )
@@ -43,6 +65,21 @@ def run(self):
4365 self .copy_file (fspath (pth_file ), str (Path (self .install_dir , pth_file .name )))
4466
4567
68+ class GeneratePTH (Command ):
69+ user_options = [] # noqa: RUF012
70+
71+ def initialize_options (self ):
72+ pass
73+
74+ def finalize_options (self ):
75+ pass
76+
77+ def run (self ):
78+ with pth_file .open ('w' ) as fh :
79+ with pth_file .with_suffix ('.embed' ).open () as sh :
80+ fh .write (f"import os, sys;exec({ sh .read ().replace (' ' , ' ' )!r} )" )
81+
82+
4683def read (* names , ** kwargs ):
4784 with Path (__file__ ).parent .joinpath (* names ).open (encoding = kwargs .get ('encoding' , 'utf8' )) as fh :
4885 return fh .read ()
@@ -118,5 +155,7 @@ def read(*names, **kwargs):
118155 'easy_install' : EasyInstallWithPTH ,
119156 'install_lib' : InstallLibWithPTH ,
120157 'develop' : DevelopWithPTH ,
158+ 'editable_wheel' : EditableWheelWithPTH ,
159+ 'genpth' : GeneratePTH ,
121160 },
122161)
0 commit comments