-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcreate-parameters.py
65 lines (55 loc) · 1.81 KB
/
create-parameters.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
#!/usr/bin/env python3
import json
import os
import re
import subprocess
output_path = os.environ.get('OUTPUT_PATH')
head = os.environ.get('CIRCLE_SHA1')
base = subprocess.run(
['git', 'merge-base', os.environ.get('BASE_REVISION'), head],
check=True,
capture_output=True
).stdout.decode('utf-8').strip()
if head == base:
try:
# If building on the same branch as BASE_REVISION, we will get the
# current commit as merge base. In that case try to go back to the
# first parent, i.e. the last state of this branch before the
# merge, and use that as the base.
base = subprocess.run(
['git', 'rev-parse', 'HEAD~1'], # FIXME this breaks on the first commit, fallback to something
check=True,
capture_output=True
).stdout.decode('utf-8').strip()
except:
# This can fail if this is the first commit of the repo, so that
# HEAD~1 actually doesn't resolve. In this case we can compare
# against this magic SHA below, which is the empty tree. The diff
# to that is just the first commit as patch.
base = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
print('Comparing {}...{}'.format(base, head))
changes = subprocess.run(
['git', 'diff', '--name-only', base, head],
check=True,
capture_output=True
).stdout.decode('utf-8').splitlines()
mappings = [
m.split() for m in
os.environ.get('MAPPING').splitlines()
]
def check_mapping(m):
if 3 != len(m):
raise Exception("Invalid mapping")
path, param, value = m
regex = re.compile(r'^' + path + r'$')
for change in changes:
if regex.match(change):
return True
return False
def convert_mapping(m):
return [m[1], json.loads(m[2])]
mappings = filter(check_mapping, mappings)
mappings = map(convert_mapping, mappings)
mappings = dict(mappings)
with open(output_path, 'w') as fp:
fp.write(json.dumps(mappings))