Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/build/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ def _make_logger() -> _ctx.Logger:
def log(message: str, *, origin: tuple[str, ...] | None = None) -> None:
if _ctx.verbosity >= -1:
if origin is None:
(first, *rest) = message.splitlines()
_cprint('{bold}{}{reset}', fill(first, initial_indent='* '), file=sys.stderr)
for line in rest:
print(fill(line, initial_indent=' '), file=sys.stderr)
print(message, file=sys.stderr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed fill so these lines won't be wrapped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any software that wraps log lines itself? I always thought it is a job of a terminal/viewer.

For log analysis it is better no have output that doesn't depend on terminal width, if, for example, need one to save it for comparison later.

So why maintain this extra code if terminal already does it for us?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the lines are indented, then yes, they should be wrapped by the tool. That's another reason not to split these messages up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@layday I put the fill back and renamed log message origin to log message kind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill also removes linefeeds, and it garbles multiline output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are fixed. Should be good now.

elif origin[0] == 'step':
_cprint('{bold}{}{reset}', fill(message, initial_indent='* '), file=sys.stderr)

elif origin[0] == 'subprocess':
initial_indent = '> ' if origin[1] == 'cmd' else '< '
Expand Down Expand Up @@ -313,7 +312,7 @@ def build_package_via_sdist(
with tarfile.TarFile.open(sdist) as t:
t.extractall(sdist_out)
try:
_ctx.log(f'Building {_natural_language_list(distributions)} from sdist')
_ctx.log(f'Building {_natural_language_list(distributions)} from sdist', origin=('step',))
srcdir = os.path.join(sdist_out, sdist_name[: -len('.tar.gz')])
for distribution in distributions:
out = _build(
Expand Down
7 changes: 4 additions & 3 deletions src/build/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def get_requires_for_build(
(``sdist`` or ``wheel``)
:param config_settings: Config settings for the build backend
"""
_ctx.log(f'Getting build dependencies for {distribution}...')
_ctx.log(f'Getting build dependencies for {distribution}...', origin=('step',))
hook_name = f'get_requires_for_build_{distribution}'
get_requires = getattr(self._hook, hook_name)

Expand Down Expand Up @@ -256,7 +256,7 @@ def prepare(
:param config_settings: Config settings for the build backend
:returns: The full path to the prepared metadata directory
"""
_ctx.log(f'Getting metadata for {distribution}...')
_ctx.log(f'Getting metadata for {distribution}...', origin=('step',))
try:
return self._call_backend(
f'prepare_metadata_for_build_{distribution}',
Expand Down Expand Up @@ -286,7 +286,8 @@ def build(
previous ``prepare`` call on the same ``distribution`` kind
:returns: The full path to the built distribution
"""
_ctx.log(f'Building {distribution}...')
_ctx.log(f'Building {distribution}...', origin=('step',))

kwargs = {} if metadata_directory is None else {'metadata_directory': metadata_directory}
return self._call_backend(f'build_{distribution}', output_directory, config_settings, **kwargs)

Expand Down
4 changes: 2 additions & 2 deletions src/build/_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __call__(self, message: str, *, origin: tuple[str, ...] | None = None) -> No


def _log_default(message: str, *, origin: tuple[str, ...] | None = None) -> None:
if origin is None:
_default_logger.log(logging.INFO, message, stacklevel=2)
# the log function that works in tests, real log function is set in __main__
_default_logger.log(logging.INFO, message, stacklevel=2)


LOGGER = contextvars.ContextVar('LOGGER', default=_log_default)
Expand Down
5 changes: 3 additions & 2 deletions src/build/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __enter__(self) -> DefaultIsolatedEnv:
else:
self._env_backend = _PipBackend()

_ctx.log(f'Creating isolated environment: {self._env_backend.display_name}...')
_ctx.log(f'Creating isolated environment: {self._env_backend.display_name}...', origin=('step',))
self._env_backend.create(self._path)

except Exception: # cleanup folder if creation fails
Expand Down Expand Up @@ -144,7 +144,8 @@ def install(self, requirements: Collection[str], constraints: Collection[str] =
if not requirements:
return

_ctx.log('Installing packages in isolated environment:\n' + '\n'.join(f'- {r}' for r in sorted(requirements)))
_ctx.log('Installing packages in isolated environment:', origin=('step',))
_ctx.log('\n'.join(f' - {r}' for r in sorted(requirements)))
self._env_backend.install_dependencies(requirements, constraints)


Expand Down
8 changes: 0 additions & 8 deletions tests/test_ctx_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ def test_default_ctx_logger(caplog: pytest.LogCaptureFixture):
assert record.message == 'foo'


def test_default_ctx_logger_only_logs_null_origin_messages(caplog: pytest.LogCaptureFixture):
build._ctx.log('foo', origin=None)
build._ctx.log('bar', origin=('bar',))

[record] = caplog.records
assert record.message == 'foo'


def test_ctx_custom_logger(mocker: pytest_mock.MockerFixture):
log_stub = mocker.stub('custom_logger')

Expand Down
3 changes: 2 additions & 1 deletion tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def test_isolated_env_log(

assert [(record.levelname, record.message) for record in caplog.records] == [
('INFO', 'Creating isolated environment: venv+pip...'),
('INFO', 'Installing packages in isolated environment:\n- something'),
('INFO', 'Installing packages in isolated environment:'),
('INFO', ' - something'),
]


Expand Down