Make it runnable on Python 2/3

This commit is contained in:
Myoungdo Park 2016-12-14 02:17:09 +09:00
parent eb4b5670ab
commit c468212966

View file

@ -24,19 +24,25 @@
# then pr[#PR] branch will be created.
#
import sys, os, subprocess
import json, urllib
from __future__ import print_function
import sys, os, subprocess, json, codecs
if sys.version_info[0] == 2:
from urllib import urlopen
else:
from urllib.request import urlopen
if len(sys.argv) == 1:
print "usage) " + sys.argv[0] + " [#PR]"
print " eg) " + sys.argv[0] + " 122"
print("usage) " + sys.argv[0] + " [#PR]")
print(" eg) " + sys.argv[0] + " 122")
sys.exit(1)
pr=sys.argv[1]
githubApi="https://api.github.com/repos/apache/zeppelin"
prInfo = json.load(urllib.urlopen(githubApi + "/pulls/" + pr))
reader = codecs.getreader("utf-8")
prInfo = json.load(reader(urlopen(githubApi + "/pulls/" + pr)))
if "message" in prInfo and prInfo["message"] == "Not Found":
sys.stderr.write("PullRequest #" + pr + " not found\n")
sys.exit(1)
@ -44,6 +50,7 @@ if "message" in prInfo and prInfo["message"] == "Not Found":
prUser=prInfo['user']['login']
prRepoUrl=prInfo['head']['repo']['clone_url']
prBranch=prInfo['head']['label'].replace(":", "/")
print(prBranch)
# create local branch
exitCode = os.system("git checkout -b pr" + pr)
@ -63,21 +70,21 @@ if exitCode != 0:
sys.exit(1)
currentBranch = subprocess.check_output("git rev-parse --abbrev-ref HEAD", shell=True).rstrip()
currentBranch = subprocess.check_output("git rev-parse --abbrev-ref HEAD", shell=True).rstrip().decode("utf-8")
print "Merge branch " + prBranch + " into " + currentBranch
print("Merge branch " + prBranch + " into " + currentBranch)
rev = subprocess.check_output("git rev-parse " + prBranch, shell=True).rstrip()
prAuthor = subprocess.check_output("git --no-pager show -s --format=\"%an <%ae>\" " + rev, shell=True).rstrip()
prAuthorDate = subprocess.check_output("git --no-pager show -s --format=\"%ad\" " + rev, shell=True).rstrip()
rev = subprocess.check_output("git rev-parse " + prBranch, shell=True).rstrip().decode("utf-8")
prAuthor = subprocess.check_output("git --no-pager show -s --format=\"%an <%ae>\" " + rev, shell=True).rstrip().decode("utf-8")
prAuthorDate = subprocess.check_output("git --no-pager show -s --format=\"%ad\" " + rev, shell=True).rstrip().decode("utf-8")
prTitle = prInfo['title']
prBody = prInfo['body']
commitList = subprocess.check_output("git log --pretty=format:\"%h\" " + currentBranch + ".." + prBranch, shell=True).rstrip()
commitList = subprocess.check_output("git log --pretty=format:\"%h\" " + currentBranch + ".." + prBranch, shell=True).rstrip().decode("utf-8")
authorList = []
for commitHash in commitList.split("\n"):
a = subprocess.check_output("git show -s --pretty=format:\"%an <%ae>\" "+commitHash, shell=True).rstrip()
a = subprocess.check_output("git show -s --pretty=format:\"%an <%ae>\" "+commitHash, shell=True).rstrip().decode("utf-8")
if a not in authorList:
authorList.append(a)
@ -85,20 +92,20 @@ commitMsg = prTitle + "\n"
if prBody :
commitMsg += prBody + "\n\n"
for author in authorList:
commitMsg += "Author: " + author+"\n"
commitMsg += "Author: " + author +"\n"
commitMsg += "\n"
commitMsg += "Closes #" + pr + " from " + prBranch + " and squashes the following commits:\n\n"
commitMsg += subprocess.check_output("git log --pretty=format:\"%h [%an] %s\" " + currentBranch + ".." + prBranch, shell=True).rstrip()
commitMsg += subprocess.check_output("git log --pretty=format:\"%h [%an] %s\" " + currentBranch + ".." + prBranch, shell=True).rstrip().decode("utf-8")
exitCode = os.system("git merge --no-commit --squash " + prBranch)
if exitCode != 0:
sys.stderr.write("Can not merge\n")
sys.exit(1)
exitCode = os.system('git commit -a --author "' + prAuthor + '" --date "' + prAuthorDate + '" -m"' + commitMsg.encode('utf-8') + '"')
exitCode = os.system('git commit -a --author "' + prAuthor + '" --date "' + prAuthorDate + '" -m"' + commitMsg + '"')
if exitCode != 0:
sys.stderr.write("Commit failed\n")
sys.exit(1)
os.system("git remote remove " + prUser)
print "Branch " + prBranch + " is merged into " + currentBranch
print("Branch " + prBranch + " is merged into " + currentBranch)