David Su | c366516 | 2021-04-14 18:58:28 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
David Su | 6e83fea | 2020-05-01 21:44:22 -0700 | [diff] [blame] | 2 | |
| 3 | # |
| 4 | # Copyright 2020, The Android Open Source Project |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | from argparse import ArgumentParser |
| 22 | import subprocess |
| 23 | import sys |
| 24 | |
| 25 | |
| 26 | def is_in_aosp(): |
| 27 | branches = subprocess.check_output(['git', 'branch', '-vv']).splitlines() |
| 28 | |
| 29 | for branch in branches: |
| 30 | # current branch starts with a '*' |
David Su | c366516 | 2021-04-14 18:58:28 -0700 | [diff] [blame] | 31 | if branch.startswith(b'*'): |
| 32 | return b'[aosp/' in branch |
David Su | 6e83fea | 2020-05-01 21:44:22 -0700 | [diff] [blame] | 33 | |
| 34 | # otherwise assume in AOSP |
| 35 | return True |
| 36 | |
| 37 | |
| 38 | def is_commit_msg_valid(commit_msg): |
| 39 | for line in commit_msg.splitlines(): |
| 40 | line = line.strip().lower() |
David Su | 0496387 | 2020-06-25 17:54:14 -0700 | [diff] [blame] | 41 | if line.startswith('updated-pdd'): |
David Su | 6e83fea | 2020-05-01 21:44:22 -0700 | [diff] [blame] | 42 | return True |
| 43 | |
| 44 | return False |
| 45 | |
| 46 | |
| 47 | def main(): |
| 48 | parser = ArgumentParser(description='Check if the Privacy Design Doc (PDD) has been updated') |
| 49 | parser.add_argument('metrics_file', type=str, help='path to the metrics Protobuf file') |
| 50 | parser.add_argument('commit_msg', type=str, help='commit message') |
| 51 | parser.add_argument('commit_files', type=str, nargs='*', help='files changed in the commit') |
| 52 | args = parser.parse_args() |
| 53 | |
| 54 | metrics_file = args.metrics_file |
| 55 | commit_msg = args.commit_msg |
| 56 | commit_files = args.commit_files |
| 57 | |
| 58 | if is_in_aosp(): |
| 59 | return 0 |
| 60 | |
| 61 | if metrics_file not in commit_files: |
| 62 | return 0 |
| 63 | |
| 64 | if is_commit_msg_valid(commit_msg): |
| 65 | return 0 |
| 66 | |
| 67 | print('This commit has changed {metrics_file}.'.format(metrics_file=metrics_file)) |
David Su | 0496387 | 2020-06-25 17:54:14 -0700 | [diff] [blame] | 68 | print('If this change added/changed/removed metrics collected from the device,') |
| 69 | print('please update the Wifi Metrics Privacy Design Doc (PDD) at go/wifi-metrics-pdd') |
David Su | 6e83fea | 2020-05-01 21:44:22 -0700 | [diff] [blame] | 70 | print('and acknowledge you have done so by adding this line to your commit message:') |
| 71 | print() |
| 72 | print('Updated-PDD: TRUE') |
| 73 | print() |
David Su | 0496387 | 2020-06-25 17:54:14 -0700 | [diff] [blame] | 74 | print('Otherwise, please explain why the PDD does not need to be updated:') |
| 75 | print() |
| 76 | print('Updated-PDD: Not applicable - reformatted file') |
| 77 | print() |
David Su | 6e83fea | 2020-05-01 21:44:22 -0700 | [diff] [blame] | 78 | print('Please reach out to the OWNERS for more information about the Wifi Metrics PDD.') |
| 79 | return 1 |
| 80 | |
| 81 | |
| 82 | if __name__ == '__main__': |
| 83 | exit_code = main() |
| 84 | sys.exit(exit_code) |