Skip to content

Commit

Permalink
Merge pull request #3674 from pypa/distutils-e0787fa
Browse files Browse the repository at this point in the history
Sync with distutils at e0787fa
  • Loading branch information
jaraco committed Nov 18, 2022
2 parents a0e8e53 + e034926 commit 1c3b501
Show file tree
Hide file tree
Showing 76 changed files with 558 additions and 631 deletions.
1 change: 1 addition & 0 deletions changelog.d/3674.change.rst
@@ -0,0 +1 @@
Sync with pypa/distutils@e0787fa, including pypa/distutils#183 updating distutils to use the Python logging framework.
11 changes: 0 additions & 11 deletions setuptools/_distutils/README

This file was deleted.

12 changes: 1 addition & 11 deletions setuptools/_distutils/__init__.py
@@ -1,17 +1,7 @@
"""distutils
The main package for the Python Module Distribution Utilities. Normally
used from a setup script as
from distutils.core import setup
setup (...)
"""

import sys
import importlib

__version__ = sys.version[: sys.version.index(' ')]
__version__, _, _ = sys.version.partition(' ')


try:
Expand Down
138 changes: 138 additions & 0 deletions setuptools/_distutils/_collections.py
@@ -1,5 +1,7 @@
import collections
import functools
import itertools
import operator


# from jaraco.collections 3.5.1
Expand Down Expand Up @@ -54,3 +56,139 @@ def __contains__(self, other):

def __len__(self):
return len(list(iter(self)))


# from jaraco.collections 3.7
class RangeMap(dict):
"""
A dictionary-like object that uses the keys as bounds for a range.
Inclusion of the value for that range is determined by the
key_match_comparator, which defaults to less-than-or-equal.
A value is returned for a key if it is the first key that matches in
the sorted list of keys.
One may supply keyword parameters to be passed to the sort function used
to sort keys (i.e. key, reverse) as sort_params.
Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'
>>> r = RangeMap({3: 'a', 6: 'b'}) # boy, that was easy
>>> r[1], r[2], r[3], r[4], r[5], r[6]
('a', 'a', 'a', 'b', 'b', 'b')
Even float values should work so long as the comparison operator
supports it.
>>> r[4.5]
'b'
But you'll notice that the way rangemap is defined, it must be open-ended
on one side.
>>> r[0]
'a'
>>> r[-1]
'a'
One can close the open-end of the RangeMap by using undefined_value
>>> r = RangeMap({0: RangeMap.undefined_value, 3: 'a', 6: 'b'})
>>> r[0]
Traceback (most recent call last):
...
KeyError: 0
One can get the first or last elements in the range by using RangeMap.Item
>>> last_item = RangeMap.Item(-1)
>>> r[last_item]
'b'
.last_item is a shortcut for Item(-1)
>>> r[RangeMap.last_item]
'b'
Sometimes it's useful to find the bounds for a RangeMap
>>> r.bounds()
(0, 6)
RangeMap supports .get(key, default)
>>> r.get(0, 'not found')
'not found'
>>> r.get(7, 'not found')
'not found'
One often wishes to define the ranges by their left-most values,
which requires use of sort params and a key_match_comparator.
>>> r = RangeMap({1: 'a', 4: 'b'},
... sort_params=dict(reverse=True),
... key_match_comparator=operator.ge)
>>> r[1], r[2], r[3], r[4], r[5], r[6]
('a', 'a', 'a', 'b', 'b', 'b')
That wasn't nearly as easy as before, so an alternate constructor
is provided:
>>> r = RangeMap.left({1: 'a', 4: 'b', 7: RangeMap.undefined_value})
>>> r[1], r[2], r[3], r[4], r[5], r[6]
('a', 'a', 'a', 'b', 'b', 'b')
"""

def __init__(self, source, sort_params={}, key_match_comparator=operator.le):
dict.__init__(self, source)
self.sort_params = sort_params
self.match = key_match_comparator

@classmethod
def left(cls, source):
return cls(
source, sort_params=dict(reverse=True), key_match_comparator=operator.ge
)

def __getitem__(self, item):
sorted_keys = sorted(self.keys(), **self.sort_params)
if isinstance(item, RangeMap.Item):
result = self.__getitem__(sorted_keys[item])
else:
key = self._find_first_match_(sorted_keys, item)
result = dict.__getitem__(self, key)
if result is RangeMap.undefined_value:
raise KeyError(key)
return result

def get(self, key, default=None):
"""
Return the value for key if key is in the dictionary, else default.
If default is not given, it defaults to None, so that this method
never raises a KeyError.
"""
try:
return self[key]
except KeyError:
return default

def _find_first_match_(self, keys, item):
is_match = functools.partial(self.match, item)
matches = list(filter(is_match, keys))
if matches:
return matches[0]
raise KeyError(item)

def bounds(self):
sorted_keys = sorted(self.keys(), **self.sort_params)
return (sorted_keys[RangeMap.first_item], sorted_keys[RangeMap.last_item])

# some special values for the RangeMap
undefined_value = type(str('RangeValueUndefined'), (), {})()

class Item(int):
"RangeMap Item"

first_item = Item(0)
last_item = Item(-1)
4 changes: 4 additions & 0 deletions setuptools/_distutils/_log.py
@@ -0,0 +1,4 @@
import logging


log = logging.getLogger()
8 changes: 4 additions & 4 deletions setuptools/_distutils/_msvccompiler.py
Expand Up @@ -22,16 +22,16 @@
with contextlib.suppress(ImportError):
import winreg

from distutils.errors import (
from .errors import (
DistutilsExecError,
DistutilsPlatformError,
CompileError,
LibError,
LinkError,
)
from distutils.ccompiler import CCompiler, gen_lib_options
from distutils import log
from distutils.util import get_platform
from .ccompiler import CCompiler, gen_lib_options
from ._log import log
from .util import get_platform

from itertools import count

Expand Down
8 changes: 4 additions & 4 deletions setuptools/_distutils/archive_util.py
Expand Up @@ -13,10 +13,10 @@
zipfile = None


from distutils.errors import DistutilsExecError
from distutils.spawn import spawn
from distutils.dir_util import mkpath
from distutils import log
from .errors import DistutilsExecError
from .spawn import spawn
from .dir_util import mkpath
from ._log import log

try:
from pwd import getpwnam
Expand Down
12 changes: 6 additions & 6 deletions setuptools/_distutils/bcppcompiler.py
Expand Up @@ -15,17 +15,17 @@
import os
import warnings

from distutils.errors import (
from .errors import (
DistutilsExecError,
CompileError,
LibError,
LinkError,
UnknownFileError,
)
from distutils.ccompiler import CCompiler, gen_preprocess_options
from distutils.file_util import write_file
from distutils.dep_util import newer
from distutils import log
from .ccompiler import CCompiler, gen_preprocess_options
from .file_util import write_file
from .dep_util import newer
from ._log import log


warnings.warn(
Expand Down Expand Up @@ -210,7 +210,7 @@ def link( # noqa: C901
)

if runtime_library_dirs:
log.warn(
log.warning(
"I don't know what to do with 'runtime_library_dirs': %s",
str(runtime_library_dirs),
)
Expand Down
14 changes: 7 additions & 7 deletions setuptools/_distutils/ccompiler.py
Expand Up @@ -7,19 +7,19 @@
import os
import re

from distutils.errors import (
from .errors import (
CompileError,
LinkError,
UnknownFileError,
DistutilsPlatformError,
DistutilsModuleError,
)
from distutils.spawn import spawn
from distutils.file_util import move_file
from distutils.dir_util import mkpath
from distutils.dep_util import newer_group
from distutils.util import split_quoted, execute
from distutils import log
from .spawn import spawn
from .file_util import move_file
from .dir_util import mkpath
from .dep_util import newer_group
from .util import split_quoted, execute
from ._log import log


class CCompiler:
Expand Down
19 changes: 9 additions & 10 deletions setuptools/_distutils/cmd.py
Expand Up @@ -7,9 +7,11 @@
import sys
import os
import re
from distutils.errors import DistutilsOptionError
from distutils import util, dir_util, file_util, archive_util, dep_util
from distutils import log
import logging

from .errors import DistutilsOptionError
from . import util, dir_util, file_util, archive_util, dep_util
from ._log import log


class Command:
Expand Down Expand Up @@ -156,14 +158,14 @@ def dump_options(self, header=None, indent=""):

if header is None:
header = "command options for '%s':" % self.get_command_name()
self.announce(indent + header, level=log.INFO)
self.announce(indent + header, level=logging.INFO)
indent = indent + " "
for (option, _, _) in self.user_options:
option = option.translate(longopt_xlate)
if option[-1] == "=":
option = option[:-1]
value = getattr(self, option)
self.announce(indent + "{} = {}".format(option, value), level=log.INFO)
self.announce(indent + "{} = {}".format(option, value), level=logging.INFO)

def run(self):
"""A command's raison d'etre: carry out the action it exists to
Expand All @@ -179,10 +181,7 @@ def run(self):
"abstract method -- subclass %s must override" % self.__class__
)

def announce(self, msg, level=1):
"""If the current verbosity level is of greater than or equal to
'level' print 'msg' to stdout.
"""
def announce(self, msg, level=logging.DEBUG):
log.log(level, msg)

def debug_print(self, msg):
Expand Down Expand Up @@ -334,7 +333,7 @@ def get_sub_commands(self):
# -- External world manipulation -----------------------------------

def warn(self, msg):
log.warn("warning: %s: %s\n", self.get_command_name(), msg)
log.warning("warning: %s: %s\n", self.get_command_name(), msg)

def execute(self, func, args, msg=None, level=1):
util.execute(func, args, msg, dry_run=self.dry_run)
Expand Down
8 changes: 4 additions & 4 deletions setuptools/_distutils/command/bdist.py
Expand Up @@ -6,14 +6,14 @@
import os
import warnings

from distutils.core import Command
from distutils.errors import DistutilsPlatformError, DistutilsOptionError
from distutils.util import get_platform
from ..core import Command
from ..errors import DistutilsPlatformError, DistutilsOptionError
from ..util import get_platform


def show_formats():
"""Print list of available formats (arguments to "--format" option)."""
from distutils.fancy_getopt import FancyGetopt
from ..fancy_getopt import FancyGetopt

formats = []
for format in bdist.format_commands:
Expand Down
12 changes: 6 additions & 6 deletions setuptools/_distutils/command/bdist_dumb.py
Expand Up @@ -5,12 +5,12 @@
$exec_prefix)."""

import os
from distutils.core import Command
from distutils.util import get_platform
from distutils.dir_util import remove_tree, ensure_relative
from distutils.errors import DistutilsPlatformError
from distutils.sysconfig import get_python_version
from distutils import log
from ..core import Command
from ..util import get_platform
from ..dir_util import remove_tree, ensure_relative
from ..errors import DistutilsPlatformError
from ..sysconfig import get_python_version
from distutils._log import log


class bdist_dumb(Command):
Expand Down
12 changes: 6 additions & 6 deletions setuptools/_distutils/command/bdist_rpm.py
Expand Up @@ -7,17 +7,17 @@
import sys
import os

from distutils.core import Command
from distutils.debug import DEBUG
from distutils.file_util import write_file
from distutils.errors import (
from ..core import Command
from ..debug import DEBUG
from ..file_util import write_file
from ..errors import (
DistutilsOptionError,
DistutilsPlatformError,
DistutilsFileError,
DistutilsExecError,
)
from distutils.sysconfig import get_python_version
from distutils import log
from ..sysconfig import get_python_version
from distutils._log import log


class bdist_rpm(Command):
Expand Down

0 comments on commit 1c3b501

Please sign in to comment.