-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_config.py
107 lines (82 loc) · 3.33 KB
/
generate_config.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import re
import time
import json
import argparse
import subprocess
from pathlib import Path
NUMBER_OF_COMMITS = 10
target_project_path = Path.home() / 'Desktop/Experiments/ardupilot'
parser = argparse.ArgumentParser()
parser.add_argument("-p", help="The path of the codebase for which we generate the config",
required=False, default=target_project_path)
parser.add_argument("-n", help="The number of commits to analyze (default 10)",
required=False, default=NUMBER_OF_COMMITS)
args = parser.parse_args()
if args.p:
target_project_path = Path(args.p)
if args.n:
NUMBER_OF_COMMITS = args.n
JSON_data = {'commits': []}
result = subprocess.run(['git', '-C', str(target_project_path),
'log', '-' + str(NUMBER_OF_COMMITS), '--no-merges', '--pretty=format:"%h"'],
stdout=subprocess.PIPE)
commits = result.stdout.decode('utf-8')
commits = commits.replace('"', '')
commits = commits.split('\n')
print(commits)
# reverse list of commits to iterate from older to the newer ones
commits_rev = [ele for ele in reversed(commits)]
for commit in commits_rev:
JSON_changes = []
print('=======================================')
print('CHECKING OUT TO: ' + commit)
print('=======================================')
# checkout to the current commit
subprocess.run(['git', '-C', str(target_project_path),
'checkout', commit], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
# get raw commit log
changes = subprocess.run(['git', '-C', str(target_project_path),
'log', '-1', '--name-status', '--diff-filter=AMDR', '--format='], stdout=subprocess.PIPE)
changes = changes.stdout.decode('utf-8')
changes = changes.split("\n")
for change in changes:
if not change:
continue
print(change)
line_info = change.split("\t")
change_type = line_info[0]
filename = line_info[1] # deleted filename in case of R
renamed_filename = None
if len(line_info) > 2:
renamed_filename = line_info[2]
change_type = re.sub(r'\d', '', change_type)
if change_type != 'R':
JSON_changes.append({
'type': change_type,
'filename': filename
})
else:
JSON_changes.append({
'type': change_type,
'filename': [filename, renamed_filename]
})
JSON_data['commits'].append({
'id': commit,
'changes': JSON_changes
})
time.sleep(3)
print('=======================================')
print('CHECKING OUT BACK TO HEAD')
print('=======================================')
# checkout back to HEAD
subprocess.run(['git', '-C', str(target_project_path),
'checkout', '-'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
time.sleep(3)
# create config files directory
directory_name = 'configurations'
if not os.path.exists(directory_name):
os.makedirs(directory_name)
updates_filename = directory_name / Path(target_project_path.stem + '_updates.json')
with open(str(updates_filename), 'w') as outfile:
json.dump(JSON_data, outfile, indent=4)