Skip to content

Commit 1c3b501

Browse files
authored
Merge pull request #3674 from pypa/distutils-e0787fa
Sync with distutils at e0787fa
2 parents a0e8e53 + e034926 commit 1c3b501

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+558
-631
lines changed

changelog.d/3674.change.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sync with pypa/distutils@e0787fa, including pypa/distutils#183 updating distutils to use the Python logging framework.

setuptools/_distutils/README

Lines changed: 0 additions & 11 deletions
This file was deleted.

setuptools/_distutils/__init__.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
"""distutils
2-
3-
The main package for the Python Module Distribution Utilities. Normally
4-
used from a setup script as
5-
6-
from distutils.core import setup
7-
8-
setup (...)
9-
"""
10-
111
import sys
122
import importlib
133

14-
__version__ = sys.version[: sys.version.index(' ')]
4+
__version__, _, _ = sys.version.partition(' ')
155

166

177
try:

setuptools/_distutils/_collections.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import collections
2+
import functools
23
import itertools
4+
import operator
35

46

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

5557
def __len__(self):
5658
return len(list(iter(self)))
59+
60+
61+
# from jaraco.collections 3.7
62+
class RangeMap(dict):
63+
"""
64+
A dictionary-like object that uses the keys as bounds for a range.
65+
Inclusion of the value for that range is determined by the
66+
key_match_comparator, which defaults to less-than-or-equal.
67+
A value is returned for a key if it is the first key that matches in
68+
the sorted list of keys.
69+
70+
One may supply keyword parameters to be passed to the sort function used
71+
to sort keys (i.e. key, reverse) as sort_params.
72+
73+
Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'
74+
75+
>>> r = RangeMap({3: 'a', 6: 'b'}) # boy, that was easy
76+
>>> r[1], r[2], r[3], r[4], r[5], r[6]
77+
('a', 'a', 'a', 'b', 'b', 'b')
78+
79+
Even float values should work so long as the comparison operator
80+
supports it.
81+
82+
>>> r[4.5]
83+
'b'
84+
85+
But you'll notice that the way rangemap is defined, it must be open-ended
86+
on one side.
87+
88+
>>> r[0]
89+
'a'
90+
>>> r[-1]
91+
'a'
92+
93+
One can close the open-end of the RangeMap by using undefined_value
94+
95+
>>> r = RangeMap({0: RangeMap.undefined_value, 3: 'a', 6: 'b'})
96+
>>> r[0]
97+
Traceback (most recent call last):
98+
...
99+
KeyError: 0
100+
101+
One can get the first or last elements in the range by using RangeMap.Item
102+
103+
>>> last_item = RangeMap.Item(-1)
104+
>>> r[last_item]
105+
'b'
106+
107+
.last_item is a shortcut for Item(-1)
108+
109+
>>> r[RangeMap.last_item]
110+
'b'
111+
112+
Sometimes it's useful to find the bounds for a RangeMap
113+
114+
>>> r.bounds()
115+
(0, 6)
116+
117+
RangeMap supports .get(key, default)
118+
119+
>>> r.get(0, 'not found')
120+
'not found'
121+
122+
>>> r.get(7, 'not found')
123+
'not found'
124+
125+
One often wishes to define the ranges by their left-most values,
126+
which requires use of sort params and a key_match_comparator.
127+
128+
>>> r = RangeMap({1: 'a', 4: 'b'},
129+
... sort_params=dict(reverse=True),
130+
... key_match_comparator=operator.ge)
131+
>>> r[1], r[2], r[3], r[4], r[5], r[6]
132+
('a', 'a', 'a', 'b', 'b', 'b')
133+
134+
That wasn't nearly as easy as before, so an alternate constructor
135+
is provided:
136+
137+
>>> r = RangeMap.left({1: 'a', 4: 'b', 7: RangeMap.undefined_value})
138+
>>> r[1], r[2], r[3], r[4], r[5], r[6]
139+
('a', 'a', 'a', 'b', 'b', 'b')
140+
141+
"""
142+
143+
def __init__(self, source, sort_params={}, key_match_comparator=operator.le):
144+
dict.__init__(self, source)
145+
self.sort_params = sort_params
146+
self.match = key_match_comparator
147+
148+
@classmethod
149+
def left(cls, source):
150+
return cls(
151+
source, sort_params=dict(reverse=True), key_match_comparator=operator.ge
152+
)
153+
154+
def __getitem__(self, item):
155+
sorted_keys = sorted(self.keys(), **self.sort_params)
156+
if isinstance(item, RangeMap.Item):
157+
result = self.__getitem__(sorted_keys[item])
158+
else:
159+
key = self._find_first_match_(sorted_keys, item)
160+
result = dict.__getitem__(self, key)
161+
if result is RangeMap.undefined_value:
162+
raise KeyError(key)
163+
return result
164+
165+
def get(self, key, default=None):
166+
"""
167+
Return the value for key if key is in the dictionary, else default.
168+
If default is not given, it defaults to None, so that this method
169+
never raises a KeyError.
170+
"""
171+
try:
172+
return self[key]
173+
except KeyError:
174+
return default
175+
176+
def _find_first_match_(self, keys, item):
177+
is_match = functools.partial(self.match, item)
178+
matches = list(filter(is_match, keys))
179+
if matches:
180+
return matches[0]
181+
raise KeyError(item)
182+
183+
def bounds(self):
184+
sorted_keys = sorted(self.keys(), **self.sort_params)
185+
return (sorted_keys[RangeMap.first_item], sorted_keys[RangeMap.last_item])
186+
187+
# some special values for the RangeMap
188+
undefined_value = type(str('RangeValueUndefined'), (), {})()
189+
190+
class Item(int):
191+
"RangeMap Item"
192+
193+
first_item = Item(0)
194+
last_item = Item(-1)

setuptools/_distutils/_log.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import logging
2+
3+
4+
log = logging.getLogger()

setuptools/_distutils/_msvccompiler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
with contextlib.suppress(ImportError):
2323
import winreg
2424

25-
from distutils.errors import (
25+
from .errors import (
2626
DistutilsExecError,
2727
DistutilsPlatformError,
2828
CompileError,
2929
LibError,
3030
LinkError,
3131
)
32-
from distutils.ccompiler import CCompiler, gen_lib_options
33-
from distutils import log
34-
from distutils.util import get_platform
32+
from .ccompiler import CCompiler, gen_lib_options
33+
from ._log import log
34+
from .util import get_platform
3535

3636
from itertools import count
3737

setuptools/_distutils/archive_util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
zipfile = None
1414

1515

16-
from distutils.errors import DistutilsExecError
17-
from distutils.spawn import spawn
18-
from distutils.dir_util import mkpath
19-
from distutils import log
16+
from .errors import DistutilsExecError
17+
from .spawn import spawn
18+
from .dir_util import mkpath
19+
from ._log import log
2020

2121
try:
2222
from pwd import getpwnam

setuptools/_distutils/bcppcompiler.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
import os
1616
import warnings
1717

18-
from distutils.errors import (
18+
from .errors import (
1919
DistutilsExecError,
2020
CompileError,
2121
LibError,
2222
LinkError,
2323
UnknownFileError,
2424
)
25-
from distutils.ccompiler import CCompiler, gen_preprocess_options
26-
from distutils.file_util import write_file
27-
from distutils.dep_util import newer
28-
from distutils import log
25+
from .ccompiler import CCompiler, gen_preprocess_options
26+
from .file_util import write_file
27+
from .dep_util import newer
28+
from ._log import log
2929

3030

3131
warnings.warn(
@@ -210,7 +210,7 @@ def link( # noqa: C901
210210
)
211211

212212
if runtime_library_dirs:
213-
log.warn(
213+
log.warning(
214214
"I don't know what to do with 'runtime_library_dirs': %s",
215215
str(runtime_library_dirs),
216216
)

setuptools/_distutils/ccompiler.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
import os
88
import re
99

10-
from distutils.errors import (
10+
from .errors import (
1111
CompileError,
1212
LinkError,
1313
UnknownFileError,
1414
DistutilsPlatformError,
1515
DistutilsModuleError,
1616
)
17-
from distutils.spawn import spawn
18-
from distutils.file_util import move_file
19-
from distutils.dir_util import mkpath
20-
from distutils.dep_util import newer_group
21-
from distutils.util import split_quoted, execute
22-
from distutils import log
17+
from .spawn import spawn
18+
from .file_util import move_file
19+
from .dir_util import mkpath
20+
from .dep_util import newer_group
21+
from .util import split_quoted, execute
22+
from ._log import log
2323

2424

2525
class CCompiler:

setuptools/_distutils/cmd.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import sys
88
import os
99
import re
10-
from distutils.errors import DistutilsOptionError
11-
from distutils import util, dir_util, file_util, archive_util, dep_util
12-
from distutils import log
10+
import logging
11+
12+
from .errors import DistutilsOptionError
13+
from . import util, dir_util, file_util, archive_util, dep_util
14+
from ._log import log
1315

1416

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

157159
if header is None:
158160
header = "command options for '%s':" % self.get_command_name()
159-
self.announce(indent + header, level=log.INFO)
161+
self.announce(indent + header, level=logging.INFO)
160162
indent = indent + " "
161163
for (option, _, _) in self.user_options:
162164
option = option.translate(longopt_xlate)
163165
if option[-1] == "=":
164166
option = option[:-1]
165167
value = getattr(self, option)
166-
self.announce(indent + "{} = {}".format(option, value), level=log.INFO)
168+
self.announce(indent + "{} = {}".format(option, value), level=logging.INFO)
167169

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

182-
def announce(self, msg, level=1):
183-
"""If the current verbosity level is of greater than or equal to
184-
'level' print 'msg' to stdout.
185-
"""
184+
def announce(self, msg, level=logging.DEBUG):
186185
log.log(level, msg)
187186

188187
def debug_print(self, msg):
@@ -334,7 +333,7 @@ def get_sub_commands(self):
334333
# -- External world manipulation -----------------------------------
335334

336335
def warn(self, msg):
337-
log.warn("warning: %s: %s\n", self.get_command_name(), msg)
336+
log.warning("warning: %s: %s\n", self.get_command_name(), msg)
338337

339338
def execute(self, func, args, msg=None, level=1):
340339
util.execute(func, args, msg, dry_run=self.dry_run)

0 commit comments

Comments
 (0)