1*7594170eSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*7594170eSAndroid Build Coastguard Worker# 3*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2022 The Android Open Source Project 4*7594170eSAndroid Build Coastguard Worker# 5*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*7594170eSAndroid Build Coastguard Worker# 9*7594170eSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*7594170eSAndroid Build Coastguard Worker# 11*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*7594170eSAndroid Build Coastguard Worker# limitations under the License.""" 16*7594170eSAndroid Build Coastguard Worker 17*7594170eSAndroid Build Coastguard Workerimport difflib 18*7594170eSAndroid Build Coastguard Workerimport pathlib 19*7594170eSAndroid Build Coastguard Workerfrom diffs.diff import Diff, ExtractInfo 20*7594170eSAndroid Build Coastguard Worker 21*7594170eSAndroid Build Coastguard Worker 22*7594170eSAndroid Build Coastguard Workerclass ContextDiff(Diff): 23*7594170eSAndroid Build Coastguard Worker def __init__(self, tool: ExtractInfo, tool_name: str): 24*7594170eSAndroid Build Coastguard Worker self.tool = tool 25*7594170eSAndroid Build Coastguard Worker self.tool_name = tool_name 26*7594170eSAndroid Build Coastguard Worker 27*7594170eSAndroid Build Coastguard Worker def diff(self, left_path: pathlib.Path, right_path: pathlib.Path) -> list[str]: 28*7594170eSAndroid Build Coastguard Worker errors = [] 29*7594170eSAndroid Build Coastguard Worker 30*7594170eSAndroid Build Coastguard Worker left = self.tool(left_path) 31*7594170eSAndroid Build Coastguard Worker right = self.tool(right_path) 32*7594170eSAndroid Build Coastguard Worker comparator = difflib.context_diff(left, right) 33*7594170eSAndroid Build Coastguard Worker difflines = list(comparator) 34*7594170eSAndroid Build Coastguard Worker if difflines: 35*7594170eSAndroid Build Coastguard Worker err = "\n".join(difflines) 36*7594170eSAndroid Build Coastguard Worker errors.append( 37*7594170eSAndroid Build Coastguard Worker f"{left_path}\ndiffers from\n{right_path}\nper {self.tool_name}:\n{err}") 38*7594170eSAndroid Build Coastguard Worker return errors 39