From b46aff10e5c491d9389d9b68c9bfdf8eaae238e8 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sat, 25 Nov 2023 16:40:39 +0100 Subject: [PATCH] swig/python/setup.py.in: workaround easy_install behavior on Windows to keep .py scripts (fixes #8811) This is a follow-up of #8815 for Windows, and particularly to unbreak conda-forge builds that test the presence of the .py scripts. Before this PR, "python setup.py install" would cause the following logs: ``` Installing gdal2tiles.py script to C:\Miniconda3\envs\test\conda-bld\gdal-split_1700841951001\_h_env\Scripts [...] Deleting C:\Miniconda3\envs\test\conda-bld\gdal-split_1700841951001\_h_env\Scripts\gdal2tiles.py Installing gdal2tiles-script.py script to C:\Miniconda3\envs\test\conda-bld\gdal-split_1700841951001\_h_env\Scripts Installing gdal2tiles.exe script to C:\Miniconda3\envs\test\conda-bld\gdal-split_1700841951001\_h_env\Scripts ``` This PR hacks around easy_install to avoid the deletiong of the .py script --- swig/python/setup.py.in | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/swig/python/setup.py.in b/swig/python/setup.py.in index 27a0e37958d7..cc3bcff43f62 100644 --- a/swig/python/setup.py.in +++ b/swig/python/setup.py.in @@ -146,6 +146,39 @@ def has_flag(compiler, flagname): return False return True +# --------------------------------------------------------------------------- +# BEGIN monkey patching of setuptools.command.easy_install.easy_install class +# --------------------------------------------------------------------------- + +# "python setup.py install" uses setuptools.command.easy_install internally +# When installing the .exe wrapper executable for our command line utilities, +# that class removes the installed .py scripts themselves. Which we do not want, +# since they may be directly used by users. So we do a monkey patching of +# easy_install.install_wrapper_scripts to install a modified +# easy_install.delete_blockers method that does NOT remove .py files +if sys.platform == 'win32': + from setuptools.command.easy_install import easy_install + + original_install_wrapper_scripts = easy_install.install_wrapper_scripts + + def monkey_patched_install_wrapper_scripts(self, dist): + original_delete_blockers = easy_install.delete_blockers + def monkey_patched_delete_blockers(self, blockers): + blockers = filter(lambda x: not x.endswith('.py'), blockers) + return original_delete_blockers(self, blockers) + + easy_install.delete_blockers = monkey_patched_delete_blockers + try: + return original_install_wrapper_scripts(self, dist) + finally: + easy_install.delete_blockers = original_delete_blockers + + easy_install.install_wrapper_scripts = monkey_patched_install_wrapper_scripts + +# --------------------------------------------------------------------------- +# END monkey patching of setuptools.command.easy_install.easy_install class +# --------------------------------------------------------------------------- + # --------------------------------------------------------------------------- # Imports # ---------------------------------------------------------------------------