Skip to content

Commit

Permalink
Merge pull request #6260 from cjerdonek/issue-6259
Browse files Browse the repository at this point in the history
Improve logging in the case of a failed legacy build.
  • Loading branch information
cjerdonek authored Feb 15, 2019
2 parents f048eb7 + d3a1b85 commit b6a2be0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 26 deletions.
47 changes: 34 additions & 13 deletions src/pip/_internal/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,31 @@ def should_use_ephemeral_cache(
return True


def format_command(
command_args, # type: List[str]
command_output, # type: str
):
# type: (...) -> str
"""
Format command information for logging.
"""
text = 'Command arguments: {}\n'.format(command_args)

if not command_output:
text += 'Command output: None'
elif logger.getEffectiveLevel() > logging.DEBUG:
text += 'Command output: [use --verbose to show]'
else:
if not command_output.endswith('\n'):
command_output += '\n'
text += (
'Command output:\n{}'
'-----------------------------------------'
).format(command_output)

return text


def get_legacy_build_wheel_path(
names, # type: List[str]
temp_dir, # type: str
Expand All @@ -793,23 +818,19 @@ def get_legacy_build_wheel_path(
# Sort for determinism.
names = sorted(names)
if not names:
# call_subprocess() ensures that the command output ends in a newline.
msg = (
'Failed building wheel for {} with args: {}\n'
'Command output:\n{}'
'-----------------------------------------'
).format(req.name, command_args, command_output)
logger.error(msg)
'Legacy build of wheel for {!r} created no files.\n'
).format(req.name)
msg += format_command(command_args, command_output)
logger.warning(msg)
return None

if len(names) > 1:
# call_subprocess() ensures that the command output ends in a newline.
msg = (
'Found more than one file after building wheel for {} '
'with args: {}\n'
'Names: {}\n'
'Command output:\n{}'
'-----------------------------------------'
).format(req.name, command_args, names, command_output)
'Legacy build of wheel for {!r} created more than one file.\n'
'Filenames (choosing first): {}\n'
).format(req.name, names)
msg += format_command(command_args, command_output)
logger.warning(msg)

return os.path.join(temp_dir, names[0])
Expand Down
68 changes: 55 additions & 13 deletions tests/unit/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,53 @@ def test_should_use_ephemeral_cache__issue_6197(
assert ephem_cache is expected


def test_format_command__INFO(caplog):

caplog.set_level(logging.INFO)
actual = wheel.format_command(
command_args=['arg1', 'arg2'],
command_output='output line 1\noutput line 2\n',
)
assert actual.splitlines() == [
"Command arguments: ['arg1', 'arg2']",
'Command output: [use --verbose to show]',
]


@pytest.mark.parametrize('command_output', [
# Test trailing newline.
'output line 1\noutput line 2\n',
# Test no trailing newline.
'output line 1\noutput line 2',
])
def test_format_command__DEBUG(caplog, command_output):
caplog.set_level(logging.DEBUG)
actual = wheel.format_command(
command_args=['arg1', 'arg2'],
command_output=command_output,
)
assert actual.splitlines() == [
"Command arguments: ['arg1', 'arg2']",
'Command output:',
'output line 1',
'output line 2',
'-----------------------------------------',
]


@pytest.mark.parametrize('log_level', ['DEBUG', 'INFO'])
def test_format_command__empty_output(caplog, log_level):
caplog.set_level(log_level)
actual = wheel.format_command(
command_args=['arg1', 'arg2'],
command_output='',
)
assert actual.splitlines() == [
"Command arguments: ['arg1', 'arg2']",
'Command output: None',
]


def call_get_legacy_build_wheel_path(caplog, names):
req = make_test_install_req()
wheel_path = wheel.get_legacy_build_wheel_path(
Expand All @@ -122,13 +169,11 @@ def test_get_legacy_build_wheel_path__no_names(caplog):
assert actual is None
assert len(caplog.records) == 1
record = caplog.records[0]
assert record.levelname == 'ERROR'
assert record.levelname == 'WARNING'
assert record.message.splitlines() == [
"Failed building wheel for pendulum with args: ['arg1', 'arg2']",
"Command output:",
"output line 1",
"output line 2",
"-----------------------------------------",
"Legacy build of wheel for 'pendulum' created no files.",
"Command arguments: ['arg1', 'arg2']",
'Command output: [use --verbose to show]',
]


Expand All @@ -142,13 +187,10 @@ def test_get_legacy_build_wheel_path__multiple_names(caplog):
record = caplog.records[0]
assert record.levelname == 'WARNING'
assert record.message.splitlines() == [
("Found more than one file after building wheel for pendulum "
"with args: ['arg1', 'arg2']"),
"Names: ['name1', 'name2']",
"Command output:",
"output line 1",
"output line 2",
"-----------------------------------------",
"Legacy build of wheel for 'pendulum' created more than one file.",
"Filenames (choosing first): ['name1', 'name2']",
"Command arguments: ['arg1', 'arg2']",
'Command output: [use --verbose to show]',
]


Expand Down

0 comments on commit b6a2be0

Please sign in to comment.