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 # Allowlist androidx deps 25 if line.startswith("androidx."): 26 continue 27 if len(line.strip()) == 0: 28 continue 29 dep = line[:line.find("(")] 30 deps.add(dep) 31 return deps 32 33 34commit_msg = subprocess.run(["git", "show", "--no-patch", "--format=%B", sha], 35 capture_output=True, check=True, text=True).stdout.splitlines() 36 37commit_msg_keys = set() 38for line in commit_msg: 39 key_match = re.match(r'(\S+):', line) 40 if key_match: 41 commit_msg_keys.add(key_match.group(1)) 42if DisableAllowedDepsCheckKey in commit_msg_keys: 43 # we are disabled 44 sys.exit(0) 45 46missing_keys = ExpectedKeys - commit_msg_keys 47 48if not missing_keys: 49 # Nothing to verify 50 sys.exit(0) 51 52 53git_show = subprocess.run(["git", "show", "--name-only", "--format=", sha], 54 capture_output=True, check=True, text=True) 55files = set(git_show.stdout.split("\n")) 56if AllowedDepsTxt not in files: 57 # nothing to check 58 sys.exit(0) 59 60before = subprocess.run(["git", "show", "%s^:%s" % (sha, AllowedDepsTxt)], 61 capture_output=True, check=True, text=True).stdout.splitlines() 62after = subprocess.run(["git", "show", "%s:%s" % (sha, AllowedDepsTxt)], 63 capture_output=True, check=True, text=True).stdout.splitlines() 64 65 66before_deps = get_deps(before) 67after_deps = get_deps(after) 68added = after_deps - before_deps 69if len(added) == 0: 70 # no new deps added, all good. Maybe just some minSdkVersion changed. 71 sys.exit(0) 72 73sys.stderr.write( 74""" 75\033[91m\033[1mError:\033[0m\033[1m You have added to allowed_deps.txt without providing necessary extra information\033[0m 76 77Added deps: 78%s 79 80Missing information from the commit message: 81%s 82 83See go/apex-allowed-deps-error for more details. 84 85To disable this check, please add "%s: <reason>" to your commit message. 86""" % ( 87 "\n".join([(" %s" % a) for a in added]), 88 "\n".join([(" %s:" % k) for k in missing_keys]), 89 DisableAllowedDepsCheckKey 90 )) 91sys.exit(1) 92