This repository was archived by the owner on Apr 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgerrit_changes.py
More file actions
executable file
·178 lines (143 loc) · 3.87 KB
/
gerrit_changes.py
File metadata and controls
executable file
·178 lines (143 loc) · 3.87 KB
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
177
178
#!/usr/bin/env python3
# Based on CyanogenMod's repopick.py from https://github.com/CyanogenMod/hudson
import json
import os
import re
import subprocess
import sys
import urllib.request
def run_command(command, \
stdin_data = None, \
cwd = None, \
universal_newlines = True):
try:
process = subprocess.Popen(
command,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
cwd = cwd,
universal_newlines = universal_newlines
)
output, error = process.communicate(input = stdin_data)
exit_status = process.returncode
return (exit_status, output, error)
except:
exit_with("Failed to run command: \"%s\"" % ' '.join(command), fail = True)
sys.exit(1)
if not "GERRIT_URL" in os.environ:
print("GERRIT_URL not specified!")
sys.exit(1)
gerrit_url = os.environ['GERRIT_URL']
if gerrit_url[-1] == '/':
gerrit_url = gerrit_url[:-1]
repo = ""
# Look for repo tool
for path in os.environ["PATH"].split(os.pathsep):
temp = os.path.join(path, "repo")
if os.path.isfile(temp) and os.access(temp, os.X_OK):
repo = temp
break
if not repo:
print("repo not found!")
sys.exit(1)
process = subprocess.Popen(
[ repo, 'list' ],
stdout = subprocess.PIPE,
universal_newlines = True
)
temp = process.communicate()[0].split('\n')
repos = []
for line in temp:
if not line:
continue
split = re.split('\s*:\s*', line)
repos.append(split)
special_exit_status = 0
for change in sys.argv[1:]:
if not change:
continue
if '://' in change:
match = re.search(r'#/c/([0-9]+)/?', change)
if not match:
print("Invalid URL: %s" % change)
sys.exit(1)
change = match.group(1)
print("=" * 80)
print("Cherrypicking %s ..." % change)
# Sometimes 504's a bit
counter = 0
while counter < 5:
try:
f = urllib.request.urlopen("%s/query?q=change:%s" % (gerrit_url, change))
break
except:
counter += 1
d = f.read().decode()
#print("Received from gerrit:")
#print("---")
#print(d)
#print("---")
d = d.split('\n')[0]
data = json.loads(d)
project = data['project']
projectpath = ""
number = data['number']
print("URL: %s/#/c/%s/ ..." % (gerrit_url, number))
for i in repos:
if project == i[1]:
projectpath = i[0]
break
if not projectpath:
for i in repos:
if re.sub("CyanogenMod/android", "chenxiaolong/CM", project) == i[1]:
projectpath = i[0]
break
if not projectpath:
print("Project %s not found!" % project)
sys.exit(1)
if not os.path.isdir(projectpath):
print("%s is not a directory!" % projectpath)
sys.exit(1)
f = urllib.request.urlopen("%s/changes/%s/revisions/current/review" % \
(gerrit_url, number))
d = f.read().decode()
d = '\n'.join(d.split('\n')[1:])
data = json.loads(d)
current_revision = data['current_revision']
patchset = 0
ref = ""
for i in data['revisions']:
if i == current_revision:
ref = data['revisions'][i]['fetch']['anonymous http']['ref']
patchset = data['revisions'][i]['_number']
break
print("Patch set: %i" % patchset)
print("Ref: %s" % ref)
exit_status, output, error = run_command(
[ 'git', 'fetch', gerrit_url + '/' + project, ref ],
cwd = projectpath
)
if exit_status != 0:
print("--- STDOUT ---")
print(output)
print("--- STDERR ---")
print(error)
print("--- END ---")
#sys.exit(1)
special_exit_status = 1;
exit_status, output, error = run_command(
[ 'git', 'merge', '--no-edit', 'FETCH_HEAD' ],
cwd = projectpath
)
if exit_status != 0:
print("--- STDOUT ---")
print(output)
print("--- STDERR ---")
print(error)
print("--- END ---")
#sys.exit(1)
special_exit_status = 1;
print("=" * 80)
if special_exit_status != 0:
sys.exit(special_exit_status)