Ember Rose | 42672eb | 2021-10-27 15:29:07 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Aurimas Liutikas | 8bd735d | 2019-11-14 13:11:11 -0800 | [diff] [blame] | 2 | |
| 3 | # |
| 4 | # Copyright 2019, 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 | """Script that will remind developers to run updateApi.""" |
| 20 | |
| 21 | import argparse |
Filip Pavlis | 1a46722 | 2019-11-28 18:00:53 +0000 | [diff] [blame] | 22 | import os.path |
Aurimas Liutikas | 8bd735d | 2019-11-14 13:11:11 -0800 | [diff] [blame] | 23 | import sys |
| 24 | |
| 25 | |
Filip Pavlis | 1a46722 | 2019-11-28 18:00:53 +0000 | [diff] [blame] | 26 | |
| 27 | WARNING_COLOR = '\033[33m' |
Aurimas Liutikas | 8bd735d | 2019-11-14 13:11:11 -0800 | [diff] [blame] | 28 | END_COLOR = '\033[0m' |
| 29 | |
Filip Pavlis | 1a46722 | 2019-11-28 18:00:53 +0000 | [diff] [blame] | 30 | WARNING_NO_API_FILES = """ |
Aurimas Liutikas | 8bd735d | 2019-11-14 13:11:11 -0800 | [diff] [blame] | 31 | {}********************************************************************** |
| 32 | You changed library classes, but you have no current.txt changes. |
| 33 | Did you forget to run ./gradlew updateApi? |
| 34 | **********************************************************************{} |
| 35 | """.format(WARNING_COLOR, END_COLOR) |
| 36 | |
Filip Pavlis | 1a46722 | 2019-11-28 18:00:53 +0000 | [diff] [blame] | 37 | WARNING_OLD_API_FILES = """ |
| 38 | {}********************************************************************** |
| 39 | Your current.txt is older than your current changes in library classes. |
| 40 | Did you forget to re-run ./gradlew updateApi? |
| 41 | **********************************************************************{} |
| 42 | """.format(WARNING_COLOR, END_COLOR) |
| 43 | |
Aurimas Liutikas | 8bd735d | 2019-11-14 13:11:11 -0800 | [diff] [blame] | 44 | |
| 45 | def main(args=None): |
Filip Pavlis | ab15641 | 2019-12-17 18:16:03 +0000 | [diff] [blame] | 46 | if not ('ENABLE_UPDATEAPI_WARNING' in os.environ): |
| 47 | sys.exit(0) |
| 48 | |
Aurimas Liutikas | 8bd735d | 2019-11-14 13:11:11 -0800 | [diff] [blame] | 49 | parser = argparse.ArgumentParser() |
| 50 | parser.add_argument('--file', '-f', nargs='*') |
| 51 | parser.set_defaults(format=False) |
| 52 | args = parser.parse_args() |
| 53 | api_files = [f for f in args.file |
| 54 | if f.endswith('.txt') and '/api/' in f] |
Filip Pavlis | 1a46722 | 2019-11-28 18:00:53 +0000 | [diff] [blame] | 55 | source_files = [f for f in args.file |
| 56 | if (not "buildSrc/" in f and |
| 57 | "/src/main/" in f or |
| 58 | "/src/commonMain/" in f or |
| 59 | "/src/androidMain/" in f)] |
| 60 | if len(source_files) == 0: |
Aurimas Liutikas | 8bd735d | 2019-11-14 13:11:11 -0800 | [diff] [blame] | 61 | sys.exit(0) |
| 62 | |
Filip Pavlis | 1a46722 | 2019-11-28 18:00:53 +0000 | [diff] [blame] | 63 | if len(api_files) == 0: |
| 64 | print(WARNING_NO_API_FILES) |
| 65 | sys.exit(77) # 77 is a warning code in repohooks |
| 66 | |
| 67 | last_source_timestamp = max([os.path.getmtime(f) for f in source_files]) |
| 68 | last_api_timestamp = max([os.path.getmtime(f) for f in api_files]) |
| 69 | |
| 70 | if last_source_timestamp > last_api_timestamp: |
| 71 | print(WARNING_OLD_API_FILES) |
| 72 | sys.exit(77) # 77 is a warning code in repohooks |
Aurimas Liutikas | 8bd735d | 2019-11-14 13:11:11 -0800 | [diff] [blame] | 73 | sys.exit(0) |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | main() |