aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/mlog.py
diff options
context:
space:
mode:
authorNiklas Claesson <nicke.claesson@gmail.com>2019-10-09 23:40:30 +0200
committerJussi Pakkanen <jpakkane@gmail.com>2019-10-10 00:40:30 +0300
commitb8fbbf596473ea3a2a2d7a6cf8ec7ee68527184c (patch)
treec2eca3373891df8bbc18057a6e976b15dd924204 /mesonbuild/mlog.py
parent763c1e32809c71e266d97e99f14b588c82284195 (diff)
downloadmeson-b8fbbf596473ea3a2a2d7a6cf8ec7ee68527184c.zip
meson-b8fbbf596473ea3a2a2d7a6cf8ec7ee68527184c.tar.gz
meson-b8fbbf596473ea3a2a2d7a6cf8ec7ee68527184c.tar.bz2
Make parser errors print relative path to meson.build file
Diffstat (limited to 'mesonbuild/mlog.py')
-rw-r--r--mesonbuild/mlog.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py
index d13defb..692a689 100644
--- a/mesonbuild/mlog.py
+++ b/mesonbuild/mlog.py
@@ -20,6 +20,7 @@ import platform
from contextlib import contextmanager
import typing
from typing import Any, Generator, List, Optional, Sequence, TextIO, Union
+from pathlib import Path
"""This is (mostly) a standalone module used to write logging
information about Meson runs. Some output goes to screen,
@@ -224,6 +225,21 @@ def warning(*args: Union[str, AnsiDecorator], **kwargs: Any) -> None:
def deprecation(*args: Union[str, AnsiDecorator], **kwargs: Any) -> None:
return _log_error('deprecation', *args, **kwargs, is_error=True)
+def get_relative_path(target: Path, current: Path) -> Path:
+ """Get the path to target from current"""
+ # Go up "current" until we find a common ancestor to target
+ acc = ['.']
+ for part in [current, *current.parents]:
+ try:
+ path = target.relative_to(part)
+ return Path(*acc, path)
+ except ValueError:
+ pass
+ acc += ['..']
+
+ # we failed, should not get here
+ return target
+
def exception(e: Exception, prefix: Optional[AnsiDecorator] = None) -> None:
if prefix is None:
prefix = red('ERROR:')
@@ -232,7 +248,8 @@ def exception(e: Exception, prefix: Optional[AnsiDecorator] = None) -> None:
if hasattr(e, 'file') and hasattr(e, 'lineno') and hasattr(e, 'colno'):
# Mypy can't figure this out, and it's pretty easy to vidual inspect
# that this is correct, so we'll just ignore it.
- args.append('%s:%d:%d:' % (e.file, e.lineno, e.colno)) # type: ignore
+ path = get_relative_path(Path(e.file), Path(os.getcwd()))
+ args.append('%s:%d:%d:' % (path, e.lineno, e.colno)) # type: ignore
if prefix:
args.append(prefix)
args.append(str(e))