Skip to content

Commit 8a48206

Browse files
build-integration-branch: add a main function
Follow typical python best practicies and add a if-name-equals-main script guard and a main function. Body of the if statement is largely unchanged from the bulk of the script. View with: git diff -w Signed-off-by: John Mulligan <[email protected]>
1 parent 9c45e5d commit 8a48206

File tree

1 file changed

+82
-76
lines changed

1 file changed

+82
-76
lines changed

src/script/build-integration-branch

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -58,84 +58,90 @@ except ImportError:
5858
assert len(sys.argv) == 2
5959
branch = label + postfix
6060

61-
token = ''
62-
try:
63-
nrc = netrc.netrc()
64-
nrauth = nrc.authenticators("api.github.com")
65-
if nrauth:
66-
token = nrauth[2]
67-
if not token:
68-
nrauth = nrc.authenticators("github.com")
61+
62+
def main():
63+
token = ''
64+
try:
65+
nrc = netrc.netrc()
66+
nrauth = nrc.authenticators("api.github.com")
6967
if nrauth:
7068
token = nrauth[2]
71-
except FileNotFoundError:
72-
pass
73-
if not token:
74-
try:
75-
with open(os.path.expanduser('~/.github_token')) as myfile:
76-
token = myfile.readline().strip()
69+
if not token:
70+
nrauth = nrc.authenticators("github.com")
71+
if nrauth:
72+
token = nrauth[2]
7773
except FileNotFoundError:
7874
pass
79-
if not token:
80-
print('No github api access token found')
81-
print(' Add a token to .netrc for [api.]github.com')
82-
print(' OR add a token to $HOME/.github_token')
83-
84-
# get prs
85-
baseurl = urljoin('https://api.github.com',
86-
('repos/{repo}/issues?labels={label}'
87-
'&sort=created'
88-
'&direction=asc'))
89-
url = baseurl.format(label=label,
90-
repo=repo)
91-
r = requests.get(url,
92-
headers={'Authorization': 'token %s' % token})
93-
if not r.ok:
94-
print("Failed to access github api")
95-
print("(Do you have a valid, unexpired github api token?)")
96-
sys.exit(1)
97-
98-
j = json.loads(r.text or r.content)
99-
print("--- found %d issues tagged with %s" % (len(j), label))
100-
101-
prs = []
102-
prtext = []
103-
for issue in j:
104-
if 'pull_request' not in issue:
105-
continue
106-
r = requests.get(issue['pull_request']['url'],
75+
if not token:
76+
try:
77+
with open(os.path.expanduser('~/.github_token')) as myfile:
78+
token = myfile.readline().strip()
79+
except FileNotFoundError:
80+
pass
81+
if not token:
82+
print('No github api access token found')
83+
print(' Add a token to .netrc for [api.]github.com')
84+
print(' OR add a token to $HOME/.github_token')
85+
86+
# get prs
87+
baseurl = urljoin('https://api.github.com',
88+
('repos/{repo}/issues?labels={label}'
89+
'&sort=created'
90+
'&direction=asc'))
91+
url = baseurl.format(label=label,
92+
repo=repo)
93+
r = requests.get(url,
10794
headers={'Authorization': 'token %s' % token})
108-
pr = json.loads(r.text or r.content)
109-
prs.append(pr)
110-
prtext.append(pr['html_url'] + ' - ' + pr['title'])
111-
print("--- queried %s prs" % len(prs))
112-
113-
print("branch %s" % branch)
114-
115-
# assemble
116-
print('--- creating branch %s' % branch)
117-
r = call(['git', 'branch', '-D', branch])
118-
r = call(['git', 'checkout', '-b', branch])
119-
assert not r
120-
for pr in prs:
121-
pr_number = pr['number']
122-
pr_url = pr['head']['repo']['clone_url']
123-
pr_ref = pr['head']['ref']
124-
print(f'--- pr {pr_number} --- pulling {pr_url} branch {pr_ref}')
125-
while True:
126-
r = call(['git', 'pull', '--no-ff', '--no-edit', pr_url, pr_ref])
127-
if r == 0:
128-
break
129-
elif r == 1:
130-
print(f'Unable to access {pr_url}, retrying..')
131-
elif r == 128:
132-
message = f'Unable to resolve conflict when merging PR#{pr_number}'
133-
raise Exception(message)
134-
else:
135-
message = ('Exiting due to an unknown failure when pulling '
136-
f'PR#{pr_number}')
137-
raise Exception(message)
138-
139-
print('--- done. these PRs were included:')
140-
print('\n'.join(prtext).encode('ascii', errors='ignore').decode())
141-
print('--- perhaps you want to: ./run-make-check.sh && git push ci %s' % branch)
95+
if not r.ok:
96+
print("Failed to access github api")
97+
print("(Do you have a valid, unexpired github api token?)")
98+
sys.exit(1)
99+
100+
j = json.loads(r.text or r.content)
101+
print("--- found %d issues tagged with %s" % (len(j), label))
102+
103+
prs = []
104+
prtext = []
105+
for issue in j:
106+
if 'pull_request' not in issue:
107+
continue
108+
r = requests.get(issue['pull_request']['url'],
109+
headers={'Authorization': 'token %s' % token})
110+
pr = json.loads(r.text or r.content)
111+
prs.append(pr)
112+
prtext.append(pr['html_url'] + ' - ' + pr['title'])
113+
print("--- queried %s prs" % len(prs))
114+
115+
print("branch %s" % branch)
116+
117+
# assemble
118+
print('--- creating branch %s' % branch)
119+
r = call(['git', 'branch', '-D', branch])
120+
r = call(['git', 'checkout', '-b', branch])
121+
assert not r
122+
for pr in prs:
123+
pr_number = pr['number']
124+
pr_url = pr['head']['repo']['clone_url']
125+
pr_ref = pr['head']['ref']
126+
print(f'--- pr {pr_number} --- pulling {pr_url} branch {pr_ref}')
127+
while True:
128+
r = call(['git', 'pull', '--no-ff', '--no-edit', pr_url, pr_ref])
129+
if r == 0:
130+
break
131+
elif r == 1:
132+
print(f'Unable to access {pr_url}, retrying..')
133+
elif r == 128:
134+
message = f'Unable to resolve conflict when merging PR#{pr_number}'
135+
raise Exception(message)
136+
else:
137+
message = ('Exiting due to an unknown failure when pulling '
138+
f'PR#{pr_number}')
139+
raise Exception(message)
140+
141+
print('--- done. these PRs were included:')
142+
print('\n'.join(prtext).encode('ascii', errors='ignore').decode())
143+
print('--- perhaps you want to: ./run-make-check.sh && git push ci %s' % branch)
144+
145+
146+
if __name__ == '__main__':
147+
main()

0 commit comments

Comments
 (0)