Skip to content

Commit

Permalink
[DEV] Support python3 for merge_livy_pr script (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaido91 committed Sep 7, 2023
1 parent bf30e3b commit 8b2e29f
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions dev/merge_livy_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@
import re
import subprocess
import sys
import urllib2

if sys.version_info[0] < 3:
import urllib2
from urllib2 import HTTPError
input_prompt_fn = raw_input
else:
import urllib.request as urllib2
from urllib.error import HTTPError
input_prompt_fn = input

try:
import jira.client
Expand Down Expand Up @@ -78,7 +86,7 @@ def get_json(url):
if GITHUB_OAUTH_KEY:
request.add_header('Authorization', 'token %s' % GITHUB_OAUTH_KEY)
return json.load(urllib2.urlopen(request))
except urllib2.HTTPError as e:
except HTTPError as e:
if "X-RateLimit-Remaining" in e.headers and e.headers["X-RateLimit-Remaining"] == '0':
print("Exceeded the GitHub API rate limit; see the instructions in " +
"dev/merge_livy_pr.py to configure an OAuth token for making authenticated " +
Expand All @@ -97,13 +105,17 @@ def fail(msg):
def run_cmd(cmd):
print(cmd)
if isinstance(cmd, list):
return subprocess.check_output(cmd)
out_bytes = subprocess.check_output(cmd)
else:
out_bytes = subprocess.check_output(cmd.split(" "))
if sys.version_info[0] > 2:
return out_bytes.decode()
else:
return subprocess.check_output(cmd.split(" "))
return out_bytes


def continue_maybe(prompt):
result = raw_input("\n%s (y/n): " % prompt)
result = input_prompt_fn("\n%s (y/n): " % prompt)
if result.lower() != "y":
fail("Okay, exiting")

Expand Down Expand Up @@ -141,7 +153,7 @@ def merge_pr(pr_num, target_ref, title, body, pr_repo_desc):
'--pretty=format:%an <%ae>']).split("\n")
distinct_authors = sorted(set(commit_authors),
key=lambda x: commit_authors.count(x), reverse=True)
primary_author = raw_input(
primary_author = input_prompt_fn(
"Enter primary author in the format of \"name <email>\" [%s]: " %
distinct_authors[0])
if primary_author == "":
Expand Down Expand Up @@ -191,7 +203,7 @@ def merge_pr(pr_num, target_ref, title, body, pr_repo_desc):


def cherry_pick(pr_num, merge_hash, default_branch):
pick_ref = raw_input("Enter a branch name [%s]: " % default_branch)
pick_ref = input_prompt_fn("Enter a branch name [%s]: " % default_branch)
if pick_ref == "":
pick_ref = default_branch

Expand Down Expand Up @@ -238,7 +250,7 @@ def resolve_jira_issue(merge_branches, comment, default_jira_id=""):
asf_jira = jira.client.JIRA({'server': JIRA_API_BASE},
basic_auth=(JIRA_USERNAME, JIRA_PASSWORD))

jira_id = raw_input("Enter a JIRA id [%s]: " % default_jira_id)
jira_id = input_prompt_fn("Enter a JIRA id [%s]: " % default_jira_id)
if jira_id == "":
jira_id = default_jira_id

Expand Down Expand Up @@ -280,7 +292,8 @@ def resolve_jira_issue(merge_branches, comment, default_jira_id=""):
default_fix_versions = filter(lambda x: x != v, default_fix_versions)
default_fix_versions = ",".join(default_fix_versions)

fix_versions = raw_input("Enter comma-separated fix version(s) [%s]: " % default_fix_versions)
fix_versions = input_prompt_fn(
"Enter comma-separated fix version(s) [%s]: " % default_fix_versions)
if fix_versions == "":
fix_versions = default_fix_versions
fix_versions = fix_versions.replace(" ", "").split(",")
Expand Down Expand Up @@ -370,7 +383,7 @@ def main():
# Assumes branch names can be sorted lexicographically
latest_branch = sorted(branch_names, reverse=True)[0]

pr_num = raw_input("Which pull request would you like to merge? (e.g. 34): ")
pr_num = input_prompt_fn("Which pull request would you like to merge? (e.g. 34): ")
pr = get_json("%s/pulls/%s" % (GITHUB_API_BASE, pr_num))
pr_events = get_json("%s/issues/%s/events" % (GITHUB_API_BASE, pr_num))

Expand All @@ -382,7 +395,7 @@ def main():
print("I've re-written the title as follows to match the standard format:")
print("Original: %s" % pr["title"])
print("Modified: %s" % modified_title)
result = raw_input("Would you like to use the modified title? (y/n): ")
result = input_prompt_fn("Would you like to use the modified title? (y/n): ")
if result.lower() == "y":
title = modified_title
print("Using modified title:")
Expand Down Expand Up @@ -433,7 +446,7 @@ def main():
merge_hash = merge_pr(pr_num, target_ref, title, body, pr_repo_desc)

pick_prompt = "Would you like to pick %s into another branch?" % merge_hash
while raw_input("\n%s (y/n): " % pick_prompt).lower() == "y":
while input_prompt_fn("\n%s (y/n): " % pick_prompt).lower() == "y":
merged_refs = merged_refs + [cherry_pick(pr_num, merge_hash, latest_branch)]

if JIRA_IMPORTED:
Expand All @@ -449,6 +462,7 @@ def main():
print("Could not find jira-python library. Run 'sudo pip install jira' to install.")
print("Exiting without trying to close the associated JIRA.")


if __name__ == "__main__":
import doctest
(failure_count, test_count) = doctest.testmod()
Expand Down

0 comments on commit 8b2e29f

Please sign in to comment.