1#!/usr/bin/python3 2""" Script to enforce certain requirements on commits that modify allowed_deps.txt 3 4For more info, go/apex-allowed-deps-error 5""" 6 7import re 8import subprocess 9import sys 10 11sha = sys.argv[1] 12 13AllowedDepsTxt = "build/allowed_deps.txt" 14 15DisableAllowedDepsCheckKey = "No-Allowed-Deps-Check" 16ExpectedKeys = set(["Apex-Size-Increase", "Previous-Platform-Support", "Aosp-First", "Test-Info"]) 17 18def get_deps(allowed_deps): 19 """ Parse allowed_deps.txt contents returning just dependency names """ 20 deps = set() 21 for line in allowed_deps: 22 if line.startswith('#'): 23 continue 24 if len(line.strip()) == 0: 25 continue 26 dep = line[:line.find("(")] 27 deps.add(dep) 28 return deps 29 30 31commit_msg = subprocess.run(["git", "show", "--no-patch", "--format=%B", sha], 32 capture_output=True, check=True, text=True).stdout.splitlines() 33 34commit_msg_keys = set() 35for line in commit_msg: 36 key_match = re.match(r'(\S+):', line) 37 if key_match: 38 commit_msg_keys.add(key_match.group(1)) 39if DisableAllowedDepsCheckKey in commit_msg_keys: 40 # we are disabled 41 sys.exit(0) 42 43missing_keys = ExpectedKeys - commit_msg_keys 44 45if not missing_keys: 46 # Nothing to verify 47 sys.exit(0) 48 49 50git_show = subprocess.run(["git", "show", "--name-only", "--format=", sha], 51 capture_output=True, check=True, text=True) 52files = set(git_show.stdout.split("\n")) 53if AllowedDepsTxt not in files: 54 # nothing to check 55 sys.exit(0) 56 57before = subprocess.run(["git", "show", "%s^:%s" % (sha, AllowedDepsTxt)], 58 capture_output=True, check=True, text=True).stdout.splitlines() 59after = subprocess.run(["git", "show", "%s:%s" % (sha, AllowedDepsTxt)], 60 capture_output=True, check=True, text=True).stdout.splitlines() 61 62 63before_deps = get_deps(before) 64after_deps = get_deps(after) 65added = after_deps - before_deps 66if len(added) == 0: 67 # no new deps added, all good. Maybe just some minSdkVersion changed. 68 sys.exit(0) 69 70sys.stderr.write( 71""" 72\033[91m\033[1mError:\033[0m\033[1m You have added to allowed_deps.txt without providing necessary extra information\033[0m 73 74Added deps: 75%s 76 77Missing information from the commit message: 78%s 79 80See go/apex-allowed-deps-error for more details. 81 82To disable this check, please add "%s: <reason>" to your commit message. 83""" % ( 84 "\n".join([(" %s" % a) for a in added]), 85 "\n".join([(" %s:" % k) for k in missing_keys]), 86 DisableAllowedDepsCheckKey 87 )) 88sys.exit(1) 89