Skip to content

Commit

Permalink
distutils direct replacements (#2134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam authored Nov 2, 2023
1 parent 4025d76 commit f7d0a79
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
1 change: 1 addition & 0 deletions com/win32com/test/pippo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def Method3(self, in1):


def BuildTypelib():
# https://github.com/pypa/setuptools/pull/4069
from distutils.dep_util import newer

this_dir = os.path.dirname(__file__)
Expand Down
2 changes: 2 additions & 0 deletions pywin32_postinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def flush(self):
# a list of actions for the uninstaller, the format is inspired by what
# the Wise installer also creates.
file_created
# 3.10 stopped supporting bdist_wininst, but we can still build them with 3.9.
# This can be kept until Python 3.9 or exe installers support is dropped.
is_bdist_wininst = True
except NameError:
is_bdist_wininst = False # we know what it is not - but not what it is :)
Expand Down
71 changes: 35 additions & 36 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,36 @@
"""
# Originally by Thomas Heller, started in 2000 or so.
import glob
import logging
import os
import platform
import re
import shutil
import subprocess
import sys
import winreg
from pathlib import Path
from tempfile import gettempdir
from typing import Iterable, List, Tuple, Union

# setuptools must be imported before distutils for markh in some python versions.
# CI doesn't hit this, so not sure what's going on.
from setuptools import setup
# setuptools must be imported before distutils because it monkey-patches it.
# distutils is also removed in Python 3.12 and deprecated with setuptools
from setuptools import Extension, setup
from setuptools.command.build import build
from setuptools.command.build_ext import build_ext
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib

from distutils import log
from distutils.command.build import build
from distutils.command.install import install
# https://github.com/pypa/setuptools/pull/4069
from distutils.dep_util import newer_group
from distutils.command.install_data import install_data
from distutils.command.install_lib import install_lib
from distutils.core import Extension
from pathlib import Path
from tempfile import gettempdir
from typing import Iterable, List, Tuple, Union


# some modules need a static CRT to avoid problems caused by them having a
# manifest.
static_crt_modules = ["winxpgui"]


import distutils.util
from distutils.dep_util import newer_group

build_id_patch = build_id
if not "." in build_id_patch:
build_id_patch = build_id_patch + ".0"
Expand Down Expand Up @@ -319,10 +317,8 @@ def __init__(self, name, **kw):
kw.setdefault("library_dirs", []).insert(0, d)

# The stand-alone exchange SDK has these libs
if distutils.util.get_platform() in ["win-amd64", "win-arm64"]:
# Additional utility functions are only available for 32-bit builds.
pass
else:
# Additional utility functions are only available for 32-bit builds.
if not platform.machine() in ("AMD64", "ARM64"):
libs += " version user32 advapi32 Ex2KSdk sadapi netapi32"
kw["libraries"] = libs
WinExt_win32com.__init__(self, name, **kw)
Expand Down Expand Up @@ -432,7 +428,7 @@ def _why_cant_build_extension(self, ext):
if os.path.isfile(os.path.join(d, h)):
break
else:
log.debug("Header '%s' not found in %s", h, look_dirs)
logging.debug("Header '%s' not found in %s", h, look_dirs)
return f"The header '{h}' can not be located."

common_dirs = self.compiler.library_dirs[:]
Expand All @@ -445,7 +441,7 @@ def _why_cant_build_extension(self, ext):
look_dirs = common_dirs + ext.library_dirs
found = self.compiler.find_library_file(look_dirs, lib, self.debug)
if not found:
log.debug("Lib '%s' not found in %s", lib, look_dirs)
logging.debug("Lib '%s' not found in %s", lib, look_dirs)
return "No library '%s'" % lib
self.found_libraries[lib.lower()] = found
patched_libs.append(os.path.splitext(os.path.basename(found))[0])
Expand Down Expand Up @@ -666,18 +662,18 @@ def build_extensions(self):
def build_exefile(self, ext):
_d = self.debug and "_d" or ""

log.info("building exe '%s'", ext.name)
logging.info("building exe '%s'", ext.name)
leaf_name = f"{ext.get_pywin32_dir()}\\{ext.name}{_d}.exe"
full_name = os.path.join(self.build_lib, leaf_name)

sources = list(ext.sources)
depends = sources + ext.depends
# unclear why we need to check this!?
if not (self.force or newer_group(depends, full_name, "newer")):
log.debug("skipping '%s' executable (up-to-date)", ext.name)
logging.debug("skipping '%s' executable (up-to-date)", ext.name)
return
else:
log.info("building '%s' executable", ext.name)
logging.info("building '%s' executable", ext.name)

objects = self.compiler.compile(
sources,
Expand Down Expand Up @@ -854,12 +850,15 @@ def swig_sources(self, sources, ext=None):

swig = self.find_swig()
for source in swig_sources:
swig_cmd = [swig, "-python", "-c++"]
swig_cmd.append(
swig_cmd = [
swig,
"-python",
"-c++",
# we never use the .doc files.
"-dnone",
) # we never use the .doc files.
]
swig_cmd.extend(self.current_extension.extra_swig_commands)
if distutils.util.get_platform() in ["win-amd64", "win-arm64"]:
if platform.machine() in ("AMD64", "ARM64"):
swig_cmd.append("-DSWIG_PY64BIT")
else:
swig_cmd.append("-DSWIG_PY32BIT")
Expand Down Expand Up @@ -894,10 +893,10 @@ def swig_sources(self, sources, ext=None):
if source == "com/win32comext/mapi/src/exchange.i":
rebuild = True

log.debug("should swig %s->%s=%s", source, target, rebuild)
logging.debug("should swig %s->%s=%s", source, target, rebuild)
if rebuild:
swig_cmd.extend(["-o", fqtarget, fqsource])
log.info("swigging %s to %s", source, target)
logging.info("swigging %s to %s", source, target)
out_dir = os.path.dirname(source)
cwd = os.getcwd()
os.chdir(out_dir)
Expand All @@ -906,7 +905,7 @@ def swig_sources(self, sources, ext=None):
finally:
os.chdir(cwd)
else:
log.info("skipping swig of %s", source)
logging.info("skipping swig of %s", source)

return new_sources

Expand Down Expand Up @@ -2184,7 +2183,7 @@ def convert_optional_data_files(files):
except RuntimeError as details:
if not str(details.args[0]).startswith("No file"):
raise
log.info("NOTE: Optional file %s not found - skipping" % file)
logging.info("NOTE: Optional file %s not found - skipping" % file)
else:
ret.append(temp[0])
return ret
Expand Down Expand Up @@ -2262,24 +2261,24 @@ def convert_optional_data_files(files):
"Programming Language :: Python :: Implementation :: CPython",
]

# 3.10 stopped supporting bdist_wininst, but we can still build them with 3.9.
# This can be kept until Python 3.9 or exe installers support is dropped.
if "bdist_wininst" in sys.argv:
# fixup https://github.com/pypa/setuptools/issues/3284
def maybe_fixup_exes():
import distutils.command.bdist_wininst
import site
from distutils.command import bdist_wininst

# setuptools can't find .exe stubs in `site-packages/setuptools/_distutils`
# but they might exist in the original `lib/distutils`.
expected_dir = os.path.dirname(distutils.command.bdist_wininst.__file__)
expected_dir = os.path.dirname(bdist_wininst.__file__)
if not len(glob.glob(f"{expected_dir}/*.exe")):
# might die, see if we can not!
for maybe in site.getsitepackages():
maybe_dir = os.path.abspath(f"{maybe}/../distutils/command")
if len(glob.glob(f"{maybe_dir}/*.exe")):
print(f"pointing setuptools at '{maybe_dir}'")
distutils.command.bdist_wininst.__file__ = os.path.join(
maybe_dir, "bdist_wininst.py"
)
bdist_wininst.__file__ = os.path.join(maybe_dir, "bdist_wininst.py")
break
else:
print("can't fixup distutils/setuptools exe stub location, good luck!")
Expand Down
2 changes: 1 addition & 1 deletion win32/Demos/c_extension/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Use 'python setup.py build' to build this extension.
import os
from distutils.core import Extension, setup
from setuptools import Extension, setup
from sysconfig import get_paths

sources = ["win32_extension.cpp"]
Expand Down

0 comments on commit f7d0a79

Please sign in to comment.