forked from horovod/horovod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_changed_code_files.py
70 lines (54 loc) · 2.07 KB
/
get_changed_code_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import logging
import os
import sys
import re
import requests
# this script outputs all code files that have changed between commit and master
# environment variable BUILDKITE_COMMIT provides the commit SHA
# environment variable BUILDKITE_PIPELINE_DEFAULT_BRANCH provides the default branch (master)
# files that match any of these regexps are considered non-code files
# even though those files have changed, they will not be in the output of this script
non_code_file_patterns = [
r'^.buildkite/get_changed_code_files.py$',
r'^.github/',
r'^docs/',
r'^.*\.md',
r'^.*\.rst'
]
def get_changed_files(base, head):
response = requests.get(
'https://api.github.com/repos/horovod/horovod/compare/{base}...{head}'.format(
base=base, head=head
)
)
if response.status_code != 200:
logging.error('Request failed: {}'.format(json.loads(response.text).get('message')))
return []
compare_json = response.text
compare = json.loads(compare_json)
return [file.get('filename') for file in compare.get('files')]
def is_code_file(file):
return not is_non_code_file(file)
def is_non_code_file(file):
return any([pattern
for pattern in non_code_file_patterns
if re.match(pattern, file)])
if __name__ == "__main__":
logging.getLogger().level = logging.DEBUG
commit = os.environ.get('BUILDKITE_COMMIT')
default = os.environ.get('BUILDKITE_PIPELINE_DEFAULT_BRANCH')
if commit is None or default is None:
logging.warning('no commit ({}) or default branch ({}) given'.format(commit, default))
sys.exit(1)
logging.debug('commit = {}'.format(commit))
logging.debug('default = {}'.format(default))
commit_files = get_changed_files(default, commit)
if len(commit_files) == 0:
logging.warning('could not find any commit files')
sys.exit(1)
changed_code_files = [file
for file in commit_files
if is_code_file(file)]
for file in changed_code_files:
print(file)