From: Antonio Valentino <antonio.valentino@tiscali.it>
Date: Sun, 3 Dec 2023 14:32:55 +0000
Subject: Do not use distutils

---
 setup.py | 99 +++++++++++++++++++++++++++++++---------------------------------
 1 file changed, 48 insertions(+), 51 deletions(-)

diff --git a/setup.py b/setup.py
index 6d7f72b..4d19b94 100755
--- a/setup.py
+++ b/setup.py
@@ -57,16 +57,13 @@
 import sys
 import os
 import shutil
+import subprocess
 from pathlib import Path
 
-
-import distutils.spawn as ds
-import distutils.dir_util as dd
-
 # Always prefer setuptools over distutils
 from setuptools import setup, Extension
-from distutils.command.clean import clean
-from distutils.command.sdist import sdist
+# from distutils.command.clean import clean
+# from distutils.command.sdist import sdist
 
 # Test if Cython is installed
 USE_CYTHON = True
@@ -74,7 +71,7 @@ try:
     from Cython.Distutils import build_ext
 except ImportError:
     USE_CYTHON = False
-    from distutils.command.build_ext import build_ext
+    from setuptools.command.build_ext import build_ext
 
 USE_COPYRIGHT = True
 try:
@@ -121,18 +118,18 @@ def remove_generated_c_file():
 #################
 def run_cmake(root_dir):
     """ Runs CMake to determine configuration for this build """
-    if ds.find_executable('cmake') is None:
+    if shutil.which('cmake') is None:
         print("CMake is required to build ltfatpy")
         print("Please install cmake version >= 2.6 and re-run setup")
         sys.exit(-1)
     print("Configuring ltfatpy build with CMake.... ")
     print("Root dir : " + root_dir)
     new_dir = os.path.join(root_dir, 'build')
-    dd.mkpath(new_dir)
+    os.makedirs(new_dir, exist_ok=True)
     os.chdir(new_dir)
     try:
-        ds.spawn(['cmake', '..'])
-    except ds.DistutilsExecError:
+        subprocess.run(['cmake', '..'], check=True)
+    except subprocess.CalledProcessError:
         print("Error while running cmake")
         print("run 'setup.py build --help' for build options")
         print("You may also try editing the settings in CMakeLists.txt file " + 
@@ -169,8 +166,8 @@ def run_make(root_dir):
     build_dir = os.path.join(root_dir, 'build')
     os.chdir(build_dir)
     try:
-        ds.spawn(['make', 'VERBOSE=TRUE'])
-    except ds.DistutilsExecError:
+        subprocess.run(['make', 'VERBOSE=TRUE'], check=True)
+    except subprocess.CalledProcessError:
         print("Error while running make")
         print("run 'setup.py build --help' for build options")
         print("You may also try editing the settings in CMakeLists.txt file " + 
@@ -187,9 +184,9 @@ def run_make_install(root_dir):
     build_dir = os.path.join(root_dir, 'build')
     os.chdir(build_dir)
     try:
-        ds.spawn(['make', 'install'])
+        subprocess.run(['make', 'install'], check=True)
         # +cmake_args.split())
-    except ds.DistutilsExecError:
+    except subprocess.CalledProcessError:
         print("Error while running make install")
         print("run 'setup.py build --help' for build options")
         print("You may also try editing the settings in CMakeLists.txt file " + 
@@ -206,8 +203,8 @@ def run_uninstall(root_dir):
     build_dir = os.path.join(root_dir, 'build')
     os.chdir(build_dir)
     try:
-        ds.spawn(['make', 'uninstall'])
-    except ds.DistutilsExecError:
+        subprocess.run(['make', 'uninstall'], caller=True)
+    except subprocess.CalledProcessError:
         print("Error while running make uninstall")
         print("run 'setup.py build --help' for build options")
         print("You may also try editing the settings in CMakeLists.txt file " + 
@@ -286,43 +283,43 @@ def makeExtension(extName, fileExt, lib_dir, path_includes):
 ######################
 # Custom clean command
 ######################
-class m_clean(clean):
-    """ Remove build directories, and compiled file in the source tree"""
-
-    def run(self):
-        clean.run(self)
-        if os.path.exists('build'):
-            shutil.rmtree('build')
-        if os.path.exists('doc' + os.path.sep + '_build'):
-            shutil.rmtree('doc' + os.path.sep + '_build')
-        for dirpath, dirnames, filenames in os.walk('.'):
-            for filename in filenames:
-                if (filename.endswith('.so') or
-                        filename.endswith('.pyd') or
-                        filename.endswith('.dll') or
-                        filename.endswith('.pyc')):
-                    os.unlink(os.path.join(dirpath, filename))
-            for dirname in dirnames:
-                if dirname == '__pycache__':
-                    shutil.rmtree(os.path.join(dirpath, dirname))
+# class m_clean(clean):
+#     """ Remove build directories, and compiled file in the source tree"""
+#
+#     def run(self):
+#         clean.run(self)
+#         if os.path.exists('build'):
+#             shutil.rmtree('build')
+#         if os.path.exists('doc' + os.path.sep + '_build'):
+#             shutil.rmtree('doc' + os.path.sep + '_build')
+#         for dirpath, dirnames, filenames in os.walk('.'):
+#             for filename in filenames:
+#                 if (filename.endswith('.so') or
+#                         filename.endswith('.pyd') or
+#                         filename.endswith('.dll') or
+#                         filename.endswith('.pyc')):
+#                     os.unlink(os.path.join(dirpath, filename))
+#             for dirname in dirnames:
+#                 if dirname == '__pycache__':
+#                     shutil.rmtree(os.path.join(dirpath, dirname))
 
 
 ##############################
 # Custom sdist command
 ##############################
-class m_sdist(sdist):
-    """ Build source package
-
-    WARNING : The stamping must be done on an default utf8 machine !
-    """
-
-    def run(self):
-        if USE_COPYRIGHT:
-            writeStamp()
-            sdist.run(self)
-            # eraseStamp()
-        else:
-            sdist.run(self)
+# class m_sdist(sdist):
+#     """ Build source package
+#
+#     WARNING : The stamping must be done on an default utf8 machine !
+#     """
+#
+#     def run(self):
+#         if USE_COPYRIGHT:
+#             writeStamp()
+#             sdist.run(self)
+#             # eraseStamp()
+#         else:
+#             sdist.run(self)
 
 
 ####################
@@ -374,8 +371,8 @@ def setup_package():
           extras_require=extras_require,
           tests_require=['nose', 'coverage'],
           cmdclass={'build_ext': m_build_ext,
-                    'clean': m_clean, 'sdist': m_sdist},
-             )
+                    },  # 'clean': m_clean, 'sdist': m_sdist},
+          )
 
 
 if __name__ == "__main__":
