-
Notifications
You must be signed in to change notification settings - Fork 0
/
depends_on_stage2
executable file
·176 lines (138 loc) · 5 KB
/
depends_on_stage2
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
"Stage2: extract dependencies of the main changeset and call stage3."
import json
import os
import re
import sys
from depends_on.common import (
command,
extract_depends_on,
extract_gerrit_change,
extract_github_change,
extract_gitlab_change,
init_sensitive_strings,
is_gerrit,
is_gitlab,
log,
merge_main_branch,
save_depends_on,
unshallow,
)
def load_depends_on(from_dir):
"Load the data from the depends-on.json file"
fname = os.path.join(from_dir, "depends-on.json")
with open(fname, "r", encoding="UTF-8") as json_stream:
return json.load(json_stream)
def extract_origin_url(work_dir):
"Return the origin URL of the git repository in work_dir."
origin_url = os.popen(f"cd {work_dir} && git remote get-url origin").read().strip()
# convert ssh to https
if origin_url.startswith("git@"):
origin_url = origin_url.replace(":", "/", 1)
origin_url = origin_url.replace("git@", "https://")
# remove the .git suffix if any
if origin_url.endswith(".git"):
origin_url = origin_url[:-4]
return origin_url
def extract_change(change_info, main_url, work_dir):
"Extract the change into the work_dir directory."
log(f"Extracting change {change_info=} into {work_dir}")
fork_url = change_info["fork_url"]
main_url = change_info["main_url"]
main_branch = change_info["main_branch"]
if is_gerrit(fork_url):
return extract_gerrit_change(
fork_url, change_info["branch"], main_branch, work_dir
)
elif is_gitlab(fork_url):
return extract_gitlab_change(
main_url, fork_url, change_info["branch"], main_branch, work_dir
)
else:
return extract_github_change(
fork_url,
change_info["branch"],
main_url,
main_branch,
work_dir,
)
def main(check_mode):
"Main function."
init_sensitive_strings()
# get the current directory
main_dir = os.getcwd()
data = load_depends_on(main_dir)
if "description" not in data or data["description"] is None:
log("No description found.")
return 0
depends_on = [
u.strip().rstrip("\r")
for u in re.findall(
r"^Depends-On: (.*)",
data["description"],
re.IGNORECASE | re.MULTILINE,
)
]
if not check_mode:
# merge the main branch to be sure to test an up-to-date version
unshallow(".", data["main_branch"])
merge_main_branch(".", data["main_branch"])
if len(depends_on) == 0:
log("No Depends-On found.")
return 0
log(f"depends_on: {depends_on}")
# go to the top dir (above main_dir)
os.chdir(os.path.join(main_dir, ".."))
nb_unmerged_pr = 0
change_info = {data["change_url"]: data}
for depends_on_url in depends_on:
merged, depends_data = extract_depends_on(
depends_on_url,
check_mode,
data["extra_dirs"],
)
change_info[depends_on_url] = depends_data
if not merged:
nb_unmerged_pr += 1
log(f"{nb_unmerged_pr} unmerged PR")
if check_mode:
return 1 if nb_unmerged_pr > 0 else 0
if nb_unmerged_pr == 0:
log("No unmerged PR found.")
return 0
# extract the Main-Dir: <dir> string if any from the description
main_dir_res = re.findall(
r"^Main-Dir: (.*)", data["description"], re.IGNORECASE | re.MULTILINE
)
if len(main_dir_res) > 0:
main_dir = main_dir_res[-1].strip()
top_dir = os.path.dirname(os.path.realpath(main_dir))
log(f"change_info: {change_info}")
real_extra_dirs = []
for extra_dir in data["extra_dirs"]:
real_extra_dir = os.path.realpath(extra_dir)
if not os.path.isdir(real_extra_dir):
log(f"Extra directory {real_extra_dir} does not exist.")
return 1
save_depends_on(data, real_extra_dir)
real_extra_dirs.append(real_extra_dir)
# lookup if the remote of the directory is part of the depends_on
# if yes, then we need to extract the right branch
origin_url = extract_origin_url(real_extra_dir)
for depends_on_url in depends_on + [data["change_url"]]:
log(f"depends_on_url: {depends_on_url} {origin_url}")
if depends_on_url.startswith(origin_url):
log(f"extract {depends_on_url} in {real_extra_dir}")
extract_change(change_info[depends_on_url], origin_url, real_extra_dir)
# check that the yaml module is installed on MacOS
if sys.platform == "darwin":
command("python -c 'import yaml' || sudo pip install pyyaml")
for work_dir in [main_dir] + real_extra_dirs:
log(f"+ chdir {work_dir}")
os.chdir(work_dir)
stage3 = os.path.join(os.path.dirname(__file__), "depends_on_stage3")
command(f"{stage3} {top_dir}")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1] == "true"))
# depends_on_stage2 ends here