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