mirror of
https://github.com/encounter/tp.git
synced 2026-07-11 06:18:46 -07:00
diff.py: robust/autodected path processing for xplatform --source (#85)
Co-authored-by: Pheenoh <pheenoh@gmail.com>
This commit is contained in:
@@ -2,7 +2,8 @@
|
|||||||
# PYTHON_ARGCOMPLETE_OK
|
# PYTHON_ARGCOMPLETE_OK
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path, PurePath
|
import platform
|
||||||
|
from pathlib import Path, PurePath, PureWindowsPath
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Dict,
|
Dict,
|
||||||
@@ -252,10 +253,10 @@ parser.add_argument(
|
|||||||
help="If multiple occurence of the same symbol is found, use this to select the correct ocurrance."
|
help="If multiple occurence of the same symbol is found, use this to select the correct ocurrance."
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--source-wslpath",
|
"--source-path-postprocess",
|
||||||
dest="source_wslpath",
|
dest="source_path_postprocess",
|
||||||
action="store_true",
|
choices=["none", "wsl", "wine"],
|
||||||
help="Pass source code path through 'wslpath' before reading it."
|
help="Preprocess source path for a given platform before reading it. If unspecified, OS detection is used."
|
||||||
)
|
)
|
||||||
|
|
||||||
# Project-specific flags, e.g. different versions/make arguments.
|
# Project-specific flags, e.g. different versions/make arguments.
|
||||||
@@ -931,6 +932,24 @@ def make_difference_normalizer() -> DifferenceNormalizer:
|
|||||||
return DifferenceNormalizerAArch64()
|
return DifferenceNormalizerAArch64()
|
||||||
return DifferenceNormalizer()
|
return DifferenceNormalizer()
|
||||||
|
|
||||||
|
def in_wsl() -> bool:
|
||||||
|
"""
|
||||||
|
WSL is thought to be the only common Linux kernel with Microsoft in the name, per Microsoft:
|
||||||
|
|
||||||
|
https://github.com/microsoft/WSL/issues/4071#issuecomment-496715404
|
||||||
|
"""
|
||||||
|
|
||||||
|
return 'Microsoft' in platform.uname().release
|
||||||
|
|
||||||
|
def guess_sourcepath_processing() -> str:
|
||||||
|
if platform.system() == 'Windows':
|
||||||
|
return 'none' # we don't need to process
|
||||||
|
else:
|
||||||
|
if in_wsl():
|
||||||
|
return 'wsl'
|
||||||
|
else:
|
||||||
|
return 'unix'
|
||||||
|
|
||||||
|
|
||||||
def process(lines: List[str]) -> List[Line]:
|
def process(lines: List[str]) -> List[Line]:
|
||||||
file_cache = dict()
|
file_cache = dict()
|
||||||
@@ -972,8 +991,17 @@ def process(lines: List[str]) -> List[Line]:
|
|||||||
if m_file_line:
|
if m_file_line:
|
||||||
path = m_file_line.group(1)
|
path = m_file_line.group(1)
|
||||||
line = int(m_file_line.group(2))
|
line = int(m_file_line.group(2))
|
||||||
if args.source_wslpath:
|
|
||||||
path = subprocess.check_output(["wslpath","-ua", path],universal_newlines=True).strip()
|
if not args.source_path_postprocess:
|
||||||
|
args.source_path_postprocess = guess_sourcepath_processing()
|
||||||
|
|
||||||
|
if args.source_path_postprocess == 'none': ...
|
||||||
|
elif args.source_path_postprocess == 'wine':
|
||||||
|
# on Wine, use winepath to convert
|
||||||
|
path = subprocess.check_output(["winepath","-u", path], universal_newlines=True).strip()
|
||||||
|
elif args.source_path_postprocess == 'wsl':
|
||||||
|
# on WSL, use wslpath to convert
|
||||||
|
path = subprocess.check_output(["wslpath","-ua", path], universal_newlines=True).strip()
|
||||||
|
|
||||||
if path in file_cache:
|
if path in file_cache:
|
||||||
file_lines = file_cache[path]
|
file_lines = file_cache[path]
|
||||||
@@ -992,7 +1020,7 @@ def process(lines: List[str]) -> List[Line]:
|
|||||||
source_lines.append(file_lines[i - 1])
|
source_lines.append(file_lines[i - 1])
|
||||||
i += 1
|
i += 1
|
||||||
else:
|
else:
|
||||||
source_lines.append(f"// \"{elide_path(Path(path))}\"")
|
source_lines.append(f"// \"{elide_path(Path(path), n_lhs=0, n_rhs=2)}\"")
|
||||||
|
|
||||||
if line > 0 and line <= len(file_lines):
|
if line > 0 and line <= len(file_lines):
|
||||||
source_lines.append(file_lines[line - 1])
|
source_lines.append(file_lines[line - 1])
|
||||||
@@ -1373,8 +1401,13 @@ def chunk_diff(diff: List[OutputLine]) -> List[Union[List[OutputLine], OutputLin
|
|||||||
chunks.append(cur_right)
|
chunks.append(cur_right)
|
||||||
return chunks
|
return chunks
|
||||||
|
|
||||||
def elide_path(p: Path) -> PurePath:
|
def elide_path(p: Path, n_lhs: int = 1, n_rhs: int = 1) -> PurePath:
|
||||||
return PurePath(p.parts[0]) / '...' / PurePath(p.parts[-1])
|
if n_lhs == 0:
|
||||||
|
path = PurePath('...')
|
||||||
|
else:
|
||||||
|
path = PurePath(*p.parts[:n_lhs]) / '...'
|
||||||
|
path = path / PurePath(*p.parts[-n_rhs:])
|
||||||
|
return path
|
||||||
|
|
||||||
def format_diff(
|
def format_diff(
|
||||||
old_diff: List[OutputLine], new_diff: List[OutputLine],
|
old_diff: List[OutputLine], new_diff: List[OutputLine],
|
||||||
|
|||||||
Reference in New Issue
Block a user