Skip to content

Commit

Permalink
Merge pull request #183 from pypa/debt/logging
Browse files Browse the repository at this point in the history
Replace bespoke logging facility with logging module
  • Loading branch information
jaraco authored Nov 13, 2022
2 parents f95d384 + d763948 commit e0787fa
Show file tree
Hide file tree
Showing 44 changed files with 202 additions and 309 deletions.
53 changes: 13 additions & 40 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import platform
import pathlib
import logging

import pytest
import path
Expand Down Expand Up @@ -36,39 +37,20 @@ def needs_zlib():
pytest.importorskip('zlib')


# from jaraco.collections
class Everything:
def __contains__(self, other):
return True


class SavedLogs(list):
def render(self, *levels):
return [
msg % args for level, msg, args in self if level in (levels or Everything())
]


@pytest.fixture
def logs(monkeypatch):
from distutils import log

logs = SavedLogs()
log_levels = log.DEBUG, log.INFO, log.WARN, log.ERROR, log.FATAL

def _log(self, level, msg, args):
self.logs.append((level, msg, args))
@pytest.fixture(autouse=True)
def log_everything():
"""
For tests, set the level on the logger to log everything.
"""
logging.getLogger('distutils').setLevel(0)

def save_log(self, level, msg, args):
if level not in log_levels:
raise ValueError(f'invalid log level {level}')
if not isinstance(msg, str):
raise TypeError(f'msg should be str, not {type(msg).__name__!r}')
logs.append((level, msg, args))

monkeypatch.setattr(log.Log, '_log', save_log)
monkeypatch.setattr(log._global_log, 'threshold', log.FATAL)
return logs
@pytest.fixture(autouse=True)
def capture_log_at_info(caplog):
"""
By default, capture logs at INFO and greater.
"""
caplog.set_level(logging.INFO)


def _save_cwd():
Expand Down Expand Up @@ -111,15 +93,6 @@ def temp_cwd(tmp_path):
yield


@pytest.fixture
def threshold_warn():
from distutils.log import set_threshold, WARN

orig = set_threshold(WARN)
yield
set_threshold(orig)


@pytest.fixture
def pypirc(request, save_env, distutils_managed_tempdir):
from distutils.core import PyPIRCCommand
Expand Down
4 changes: 4 additions & 0 deletions distutils/_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import logging


log = logging.getLogger()
2 changes: 1 addition & 1 deletion distutils/_msvccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
LinkError,
)
from .ccompiler import CCompiler, gen_lib_options
from . import log
from ._log import log
from .util import get_platform

from itertools import count
Expand Down
2 changes: 1 addition & 1 deletion distutils/archive_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .errors import DistutilsExecError
from .spawn import spawn
from .dir_util import mkpath
from . import log
from ._log import log

try:
from pwd import getpwnam
Expand Down
4 changes: 2 additions & 2 deletions distutils/bcppcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .ccompiler import CCompiler, gen_preprocess_options
from .file_util import write_file
from .dep_util import newer
from . import log
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
2 changes: 1 addition & 1 deletion distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .dir_util import mkpath
from .dep_util import newer_group
from .util import split_quoted, execute
from . import log
from ._log import log


class CCompiler:
Expand Down
15 changes: 7 additions & 8 deletions distutils/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import sys
import os
import re
import logging

from .errors import DistutilsOptionError
from . import util, dir_util, file_util, archive_util, dep_util, log
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
2 changes: 1 addition & 1 deletion distutils/command/bdist_dumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..dir_util import remove_tree, ensure_relative
from ..errors import DistutilsPlatformError
from ..sysconfig import get_python_version
from distutils import log
from distutils._log import log


class bdist_dumb(Command):
Expand Down
2 changes: 1 addition & 1 deletion distutils/command/bdist_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
DistutilsExecError,
)
from ..sysconfig import get_python_version
from distutils import log
from distutils._log import log


class bdist_rpm(Command):
Expand Down
2 changes: 1 addition & 1 deletion distutils/command/build_clib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ..core import Command
from ..errors import DistutilsSetupError
from ..sysconfig import customize_compiler
from distutils import log
from distutils._log import log


def show_compilers():
Expand Down
10 changes: 6 additions & 4 deletions distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from ..dep_util import newer_group
from ..extension import Extension
from ..util import get_platform
from distutils import log
from distutils._log import log
from . import py37compat

from site import USER_BASE
Expand Down Expand Up @@ -373,7 +373,7 @@ def check_extensions_list(self, extensions): # noqa: C901

ext_name, build_info = ext

log.warn(
log.warning(
"old-style (ext_name, build_info) tuple found in "
"ext_modules for extension '%s' "
"-- please convert to Extension instance",
Expand Down Expand Up @@ -413,7 +413,9 @@ def check_extensions_list(self, extensions): # noqa: C901
# Medium-easy stuff: same syntax/semantics, different names.
ext.runtime_library_dirs = build_info.get('rpath')
if 'def_file' in build_info:
log.warn("'def_file' element of build info dict " "no longer supported")
log.warning(
"'def_file' element of build info dict " "no longer supported"
)

# Non-trivial stuff: 'macros' split into 'define_macros'
# and 'undef_macros'.
Expand Down Expand Up @@ -597,7 +599,7 @@ def swig_sources(self, sources, extension):
# the temp dir.

if self.swig_cpp:
log.warn("--swig-cpp is deprecated - use --swig-opts=-c++")
log.warning("--swig-cpp is deprecated - use --swig-opts=-c++")

if (
self.swig_cpp
Expand Down
4 changes: 2 additions & 2 deletions distutils/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..core import Command
from ..errors import DistutilsOptionError, DistutilsFileError
from ..util import convert_path
from distutils import log
from distutils._log import log


class build_py(Command):
Expand Down Expand Up @@ -212,7 +212,7 @@ def check_package(self, package, package_dir):

def check_module(self, module, module_file):
if not os.path.isfile(module_file):
log.warn("file %s (for module %s) not found", module_file, module)
log.warning("file %s (for module %s) not found", module_file, module)
return False
else:
return True
Expand Down
2 changes: 1 addition & 1 deletion distutils/command/build_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..core import Command
from ..dep_util import newer
from ..util import convert_path
from distutils import log
from distutils._log import log
import tokenize

shebang_pattern = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
Expand Down
4 changes: 2 additions & 2 deletions distutils/command/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
from ..core import Command
from ..dir_util import remove_tree
from distutils import log
from distutils._log import log


class clean(Command):
Expand Down Expand Up @@ -64,7 +64,7 @@ def run(self):
if os.path.exists(directory):
remove_tree(directory, dry_run=self.dry_run)
else:
log.warn("'%s' does not exist -- can't clean it", directory)
log.warning("'%s' does not exist -- can't clean it", directory)

# just for the heck of it, try to remove the base build directory:
# we might have emptied it right now, but if not we don't care
Expand Down
2 changes: 1 addition & 1 deletion distutils/command/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..core import Command
from ..errors import DistutilsExecError
from ..sysconfig import customize_compiler
from distutils import log
from distutils._log import log

LANG_EXT = {"c": ".c", "c++": ".cxx"}

Expand Down
4 changes: 2 additions & 2 deletions distutils/command/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sysconfig
import itertools

from distutils import log
from distutils._log import log
from ..core import Command
from ..debug import DEBUG
from ..sysconfig import get_config_vars
Expand Down Expand Up @@ -644,7 +644,7 @@ def handle_extra_path(self):
self.extra_path = self.distribution.extra_path

if self.extra_path is not None:
log.warn(
log.warning(
"Distribution option extra_path is deprecated. "
"See issue27919 for details."
)
Expand Down
3 changes: 2 additions & 1 deletion distutils/command/install_egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import re

from ..cmd import Command
from distutils import log, dir_util
from .. import dir_util
from .._log import log


class install_egg_info(Command):
Expand Down
2 changes: 1 addition & 1 deletion distutils/command/install_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import os
from ..core import Command
from distutils import log
from distutils._log import log
from stat import ST_MODE


Expand Down
16 changes: 9 additions & 7 deletions distutils/command/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

import getpass
import io
import logging
import urllib.parse
import urllib.request
from warnings import warn

from ..core import PyPIRCCommand
from distutils import log
from distutils._log import log


class register(PyPIRCCommand):
Expand Down Expand Up @@ -153,7 +154,7 @@ def send_metadata(self): # noqa: C901
3. have the server generate a new password for you (and email it to you), or
4. quit
Your selection [default 1]: ''',
log.INFO,
logging.INFO,
)
choice = input()
if not choice:
Expand All @@ -174,7 +175,7 @@ def send_metadata(self): # noqa: C901
auth.add_password(self.realm, host, username, password)
# send the info to the server and report the result
code, result = self.post_to_server(self.build_post_data('submit'), auth)
self.announce('Server response ({}): {}'.format(code, result), log.INFO)
self.announce('Server response ({}): {}'.format(code, result), logging.INFO)

# possibly save the login
if code == 200:
Expand All @@ -188,11 +189,11 @@ def send_metadata(self): # noqa: C901
'I can store your PyPI login so future '
'submissions will be faster.'
),
log.INFO,
logging.INFO,
)
self.announce(
'(the login will be stored in %s)' % self._get_rc_file(),
log.INFO,
logging.INFO,
)
choice = 'X'
while choice.lower() not in 'yn':
Expand Down Expand Up @@ -265,7 +266,8 @@ def post_to_server(self, data, auth=None): # noqa: C901
'''Post a query to the server, and return a string response.'''
if 'name' in data:
self.announce(
'Registering {} to {}'.format(data['name'], self.repository), log.INFO
'Registering {} to {}'.format(data['name'], self.repository),
logging.INFO,
)
# Build up the MIME payload for the urllib2 POST data
boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
Expand Down Expand Up @@ -315,5 +317,5 @@ def post_to_server(self, data, auth=None): # noqa: C901
result = 200, 'OK'
if self.show_response:
msg = '\n'.join(('-' * 75, data, '-' * 75))
self.announce(msg, log.INFO)
self.announce(msg, logging.INFO)
return result
Loading

0 comments on commit e0787fa

Please sign in to comment.