blob: cc0aca7c663bfb729890dd347037373345f0e325 [file] [log] [blame] [edit]
"""Contains utils for processing coverage files."""
import pathlib
_RBE_PREFIX = pathlib.Path('/b/f/w')
def rewrite_file_paths_in_lcov(input_path, output_path, workspace_path):
"""Copy the input lcov file to the output, after fixing the paths in it."""
with open(output_path, 'w') as out_file:
with open(input_path, 'r') as in_file:
for line in in_file:
if line.startswith('SF:'):
path = pathlib.Path(line.removeprefix('SF:').strip())
path = _convert_to_workspace_relative_path(path, workspace_path)
out_file.write(f'SF:{path}\n')
else:
out_file.write(line)
def _convert_to_workspace_relative_path(path, workspace_path):
if not path.is_absolute():
raise ValueError(f'Expected absolute paths in lcov file, got {path}')
absolute_workspace_path_or_symlink = _fix_rbe_prefix(path, workspace_path)
absolute_workspace_path = absolute_workspace_path_or_symlink.resolve()
return absolute_workspace_path.relative_to(workspace_path)
def _fix_rbe_prefix(path, workspace_path):
if path.is_relative_to(_RBE_PREFIX):
return workspace_path / path.relative_to(_RBE_PREFIX)
return path