-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgerrit_access.py
228 lines (199 loc) · 7.55 KB
/
gerrit_access.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#! /usr/bin/python
#
# python wrapper for parsing the return values from :
# ssh -p 29418 android.example.com gerrit query --format TEXT --current-patch-set 'status:open is:starred'
#
import subprocess, shlex
root = "change I"
change_keywords = [" project: " ," branch: " ," id: " , " number: " ,
" subject: " ," owner:", " url: " , " lastUpdated: " ," sortKey: " ,
" open: " , " status: ", " currentPatchSet:"]
patchset_keywords = [" number: ", " revision: ", " ref: ", " uploader:", " approvals:"]
approvals_keywords = [ " type: " ," description: " , " value: " ,
" grantedOn: ", " by:"]
cmd = ['repo', 'forall', '.', '-c', 'echo $REPO_PROJECT']
P = subprocess.Popen(args=cmd, stdout=subprocess.PIPE)
REPO_PROJECT = P.communicate()[0]
cmd = ['repo', 'forall', '.', '-c', 'echo $REPO_RREV']
P = subprocess.Popen(args=cmd, stdout=subprocess.PIPE)
REPO_RREV = P.communicate()[0]
class developer:
def __init__(self, lines):
self.name = lines[0][2+lines[0].find(": "):]
self.email = lines[1][2+lines[1].find(": "):]
class approval:
def __init__(self, lines):
i = 0
for l in lines:
i += 1
if 0 == l.find( approvals_keywords[0]):
self.Type = l[2+l.find(": "):]
continue
if 0 == l.find( approvals_keywords[1]):
self.description = l[2+l.find(": "):]
continue
if 0 == l.find( approvals_keywords[2]):
self.value = l[2+l.find(": "):]
continue
if 0 == l.find( approvals_keywords[3]):
self.grantedOn = l[2+l.find(": "):]
continue
if 0 == l.find( approvals_keywords[4]):
self.by = developer(lines[i:i+2])
break
def approvals(lines):
ret = []
for i in xrange(len(lines)):
if 0 == lines[i].find( approvals_keywords[0]):
ret.append(approval(lines[i:i+7]))
return ret
class patchSet:
def __init__(self, lines):
i = 0
for l in lines:
i += 1
if 0 == l.find( patchset_keywords[0]):
self.number = l[2+l.find(": "):]
continue
if 0 == l.find( patchset_keywords[1]):
self.revision = l[2+l.find(": "):]
continue
if 0 == l.find( patchset_keywords[2]):
self.ref = l[2+l.find(": "):]
continue
if 0 == l.find( patchset_keywords[3]):
self.uploader = developer(lines[i:i+2])
continue
if 0 == l.find( patchset_keywords[4]):
self.approvals = approvals(lines[i:])
break
class Change:
def __init__(self, lines):
i = 0
for l in lines:
i += 1
if 0 == l.find( change_keywords[0]):
self.project = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[1]):
self.branch = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[2]):
self.id = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[3]):
self.number = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[4]):
self.subject = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[5]):
self.owner = developer(lines[i:i+2])
continue
if 0 == l.find( change_keywords[6]):
self.url = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[7]):
self.lastUpdated = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[8]):
self.sortKey = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[9]):
self.Open = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[10]):
self.status = l[2+l.find(": "):]
continue
if 0 == l.find( change_keywords[11]):
self.currentPatchSet = patchSet(lines[i:])
break
def fetch(self):
"""git fetch ssh://[email protected]:29418/a/bsp/hardware/intel/libcamera refs/changes/16/4816/1"""
#assume CWD is correct.
git_cmd = ["git", "fetch", "ssh://[email protected]:29418/"+self.project, self.currentPatchSet.ref]
ret = subprocess.call(git_cmd)
print self.url
return ret
def make_patch(self):
git_cmd = ["git", "format-patch", "-1", "FETCH_HEAD"]
ret = subprocess.call(git_cmd)
return ret
def cherry_pick(self):
git_cmd = ["git", "cherry-pick", "FETCH_HEAD"]
ret = subprocess.call(git_cmd)
return ret
def dumb_buff_to_struct(lines):
"""one pass parser of query output spit into lines"""
changes = []
start = 0
Next = 0
i = 0
for l in lines:
i += 1
if root == l[:len(root)]:
start = Next
Next = i
if Next > start + len(change_keywords):
changes.append(Change(lines[start:Next]))
changes.append(Change(lines[Next:]))
return changes
def get_open_changes():
"""get changes that have + 1 or +2 approval marked by the uploader"""
cmd = shlex.split(" ssh -p 29418 android.intel.com gerrit query --format TEXT --current-patch-set 'status:open '")
cmd[-1] = cmd[-1] + " project:" + REPO_PROJECT + " branch:" + REPO_RREV
P = subprocess.Popen(args=cmd, stdout=subprocess.PIPE)
out = P.communicate()
if P.returncode == 0:
lines = out[0].split('\n')
#print lines
changes = dumb_buff_to_struct(lines)
return changes
def get_starred_changes():
cmd = shlex.split(" ssh -p 29418 android.intel.com gerrit query --format TEXT --current-patch-set 'status:open is:starred'")
cmd[-1] = cmd[-1] + " project:" + REPO_PROJECT + " branch:" + REPO_RREV
P = subprocess.Popen(args=cmd, stdout=subprocess.PIPE)
out = P.communicate()
if P.returncode == 0:
lines = out[0].split('\n')
#print lines
changes = dumb_buff_to_struct(lines)
return changes
def get_patches():
changes = get_starred_changes()
for c in changes:
c.fetch()
c.make_patch()
def list_ready_patches():
"""list changes that are marked by uploader as good"""
changes = get_open_changes()
for c in changes:
try:
for a in c.currentPatchSet.approvals:
if a.Type == 'CRVW':
if c.currentPatchSet.uploader.name == a.by.name:
if a.value == "1" or a.value == '2':
print c.url
break
except AttributeError:
continue
def get_ready_patches():
"""get patches for changes that are marked by uploader as good"""
changes = get_open_changes()
for c in changes:
try:
for a in c.currentPatchSet.approvals:
if a.Type == 'CRVW':
if c.currentPatchSet.uploader.name == a.by.name:
if a.value == "1" or a.value == '2':
c.fetch()
c.make_patch()
break
except AttributeError:
continue
def test():
#get_patches()
#get_ready_patches()
list_ready_patches()
if __name__ == "__main__":
test()