1*5a923131SAndroid Build Coastguard Worker#!/usr/bin/env python 2*5a923131SAndroid Build Coastguard Worker# 3*5a923131SAndroid Build Coastguard Worker# Copyright (C) 2021 The Android Open Source Project 4*5a923131SAndroid Build Coastguard Worker# 5*5a923131SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*5a923131SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*5a923131SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*5a923131SAndroid Build Coastguard Worker# 9*5a923131SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*5a923131SAndroid Build Coastguard Worker# 11*5a923131SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*5a923131SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*5a923131SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*5a923131SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*5a923131SAndroid Build Coastguard Worker# limitations under the License. 16*5a923131SAndroid Build Coastguard Worker# 17*5a923131SAndroid Build Coastguard Worker 18*5a923131SAndroid Build Coastguard Worker"""CLI script for linting .proto files inside update_engine.""" 19*5a923131SAndroid Build Coastguard Worker 20*5a923131SAndroid Build Coastguard Workerimport sys 21*5a923131SAndroid Build Coastguard Workerimport re 22*5a923131SAndroid Build Coastguard Workerimport subprocess 23*5a923131SAndroid Build Coastguard Worker 24*5a923131SAndroid Build Coastguard Workerdef check_proto_file(commit_hash, filename): 25*5a923131SAndroid Build Coastguard Worker """Check if |filename| is consistnet with our protobuf guidelines 26*5a923131SAndroid Build Coastguard Worker 27*5a923131SAndroid Build Coastguard Worker Args: 28*5a923131SAndroid Build Coastguard Worker commit_hash: Hash of the git commit to look 29*5a923131SAndroid Build Coastguard Worker filename: A filesystem path to a .proto file 30*5a923131SAndroid Build Coastguard Worker Returns: 31*5a923131SAndroid Build Coastguard Worker True if this file passes linting check, False otherwise 32*5a923131SAndroid Build Coastguard Worker """ 33*5a923131SAndroid Build Coastguard Worker output = subprocess.check_output( 34*5a923131SAndroid Build Coastguard Worker ["git", "diff", commit_hash+"~", commit_hash, "--", filename]) 35*5a923131SAndroid Build Coastguard Worker output = output.decode() 36*5a923131SAndroid Build Coastguard Worker p = re.compile(r"^[+]?\s*required .*$", re.M) 37*5a923131SAndroid Build Coastguard Worker m = p.search(output) 38*5a923131SAndroid Build Coastguard Worker if m: 39*5a923131SAndroid Build Coastguard Worker print("File", filename, 40*5a923131SAndroid Build Coastguard Worker "contains 'required' keyword. Usage of required", 41*5a923131SAndroid Build Coastguard Worker "is strongly discouraged in protobuf", m.group()) 42*5a923131SAndroid Build Coastguard Worker return False 43*5a923131SAndroid Build Coastguard Worker return True 44*5a923131SAndroid Build Coastguard Worker 45*5a923131SAndroid Build Coastguard Workerdef main(): 46*5a923131SAndroid Build Coastguard Worker if len(sys.argv) < 2: 47*5a923131SAndroid Build Coastguard Worker print("Usage:", sys.argv[0], "commit_hash", "<file1>", "<file2>", "...") 48*5a923131SAndroid Build Coastguard Worker sys.exit(1) 49*5a923131SAndroid Build Coastguard Worker commit_hash = sys.argv[1] 50*5a923131SAndroid Build Coastguard Worker for filename in sys.argv[2:]: 51*5a923131SAndroid Build Coastguard Worker if filename.endswith(".proto"): 52*5a923131SAndroid Build Coastguard Worker if not check_proto_file(commit_hash, filename): 53*5a923131SAndroid Build Coastguard Worker sys.exit(1) 54*5a923131SAndroid Build Coastguard Worker 55*5a923131SAndroid Build Coastguard Workerif __name__ == "__main__": 56*5a923131SAndroid Build Coastguard Worker main() 57