1#!/usr/bin/env python3
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
21import argparse
22import os.path
23import sys
24
25
26
27WARNING_COLOR = '\033[33m'
28END_COLOR = '\033[0m'
29
30WARNING_NO_API_FILES = """
31{}**********************************************************************
32You changed library classes, but you have no current.txt changes.
33Did you forget to run ./gradlew updateApi?
34**********************************************************************{}
35""".format(WARNING_COLOR, END_COLOR)
36
37WARNING_OLD_API_FILES = """
38{}**********************************************************************
39Your current.txt is older than your current changes in library classes.
40Did you forget to re-run ./gradlew updateApi?
41**********************************************************************{}
42""".format(WARNING_COLOR, END_COLOR)
43
44
45def main(args=None):
46  if not ('ENABLE_UPDATEAPI_WARNING' in os.environ):
47    sys.exit(0)
48
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]
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:
61    sys.exit(0)
62
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
73  sys.exit(0)
74
75if __name__ == '__main__':
76  main()
77