blob: e124d29fb9c34c78937c1dc77eb8dc800e045372 [file] [log] [blame]
markdr5b2a8d02017-09-25 17:49:38 -07001#!/usr/bin/env python3.4
2#
3# Copyright 2017 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16import logging
17import os
18import re
19import shellescape
20
21from acts.libs.proc import job
22
23GLOBAL_KEYWORDS_FILEPATH = 'vendor/google_testing/comms/framework/etc/' \
24 'commit_keywords'
25LOCAL_KEYWORDS_FILEPATH = '~/.repo_acts_commit_keywords'
26
27GIT_FILE_ADDITIONS = r'git diff --unified=0 %s^! | grep "^+" | ' \
28 r'grep -Ev "^(\+\+\+ b/)" | cut -c 2-'
29
30GIT_FILE_NAMES = r'git diff-tree --no-commit-id --name-only -r %s'
31
32GIT_DIFF_REGION_FOUND = 'git diff %s^! | grep -A10 -B10 %s'
33COMMIT_ID_ENV_KEY = 'PREUPLOAD_COMMIT'
34
35FIND_COMMIT_KEYWORDS = 'git diff %s^! | grep -i %s'
36GET_EMAIL_ADDRESS = 'git log --format=%ce -1'
37
38
39def find_in_commit_message(keyword_list):
40 """Looks for keywords in commit messages.
41
42 Args:
43 keyword_list: A list of keywords
44 """
45 grep_args = ''
46 for keyword in keyword_list:
47 grep_args += '-e %s' % keyword
48
49 result = job.run(
50 FIND_COMMIT_KEYWORDS % (os.environ[COMMIT_ID_ENV_KEY], grep_args),
51 ignore_status=True)
52
53 if result.stdout:
54 logging.error('Your commit message contains at least one keyword.')
55 logging.error('Keyword(s) found in the following line(s):')
56 logging.error(result.stdout)
57 logging.error('Please fix/remove these before committing.')
58 exit(1)
59
60
61def get_words(string):
62 """Splits a string into an array of alphabetical words."""
63 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', string)
64 s1 = re.sub('([a-z0-9])([A-Z])', r'\1 \2', s1).lower()
65 s1 = re.sub('[^a-z ]', ' ', s1)
66 return s1.split()
67
68
69def find_in_file_names(keyword_list):
70 """Looks for keywords in file names.
71
72 Args:
73 keyword_list: a list of keywords
74 """
75 changed_files = job.run(GIT_FILE_NAMES % os.environ[COMMIT_ID_ENV_KEY])
76
77 keyword_set = set(keyword_list)
78
79 for file_name in changed_files.stdout.split('\n'):
80 words = set(get_words(file_name))
81 if len(keyword_set.intersection(words)) > 0:
82 logging.error('Your commit has a file name that contain at least '
83 'one keyword: %s.' % file_name)
84 logging.error('Please update or remove this before committing.')
85 exit(1)
86
87
88def find_in_code_additions(keyword_list):
89 """Looks for keywords in code additions.
90
91 Args:
92 keyword_list: a list of keywords
93 """
94 all_additions = job.run(GIT_FILE_ADDITIONS % os.environ[COMMIT_ID_ENV_KEY])
95
96 keyword_set = set(keyword_list)
97
98 for line in all_additions.stdout.split('\n'):
99 words = set(get_words(line))
100 if len(keyword_set.intersection(words)) > 0:
101 result = job.run(GIT_DIFF_REGION_FOUND %
102 (os.environ[COMMIT_ID_ENV_KEY],
103 shellescape.quote(line)))
104 logging.error('Your commit has code changes that contain at least '
105 'one keyword. Below is an excerpt from the commit '
106 'diff:')
107 logging.error(result.stdout)
108 logging.error('Please update or remove these before committing.')
109 exit(1)
110
111
112def main():
113 if COMMIT_ID_ENV_KEY not in os.environ:
114 logging.error('Missing commit id in environment.')
115 exit(1)
116 keyword_file = os.path.join(
117 os.path.dirname(__file__), '../../../../%s' % GLOBAL_KEYWORDS_FILEPATH)
118
119 if not os.path.isfile(keyword_file):
120 keyword_file = os.path.expanduser(LOCAL_KEYWORDS_FILEPATH)
121 if not os.path.exists(keyword_file) or not os.path.isfile(keyword_file):
122 result = job.run(GET_EMAIL_ADDRESS)
123 if result.stdout.endswith('@google.com'):
124 logging.error(
125 'You do not have the necessary file %s. Please run '
126 'tools/ignore_commit_keywords.sh, or link it with the '
127 'following command:\n ln -sf <internal_master_root>/%s %s'
128 % (LOCAL_KEYWORDS_FILEPATH, GLOBAL_KEYWORDS_FILEPATH,
129 LOCAL_KEYWORDS_FILEPATH))
130 exit(1)
131 return
132
133 with open(keyword_file) as file:
134 keyword_list = file.read().lower().split()
135
136 find_in_code_additions(keyword_list)
137 find_in_commit_message(keyword_list)
138 find_in_file_names(keyword_list)
139
140
141if __name__ == '__main__':
142 main()