repopick: support auth'ing to gerrit and picking drafts
* Use requests if installed. If not fall back to urllib. This is
done because users may or may not have requests installed and
requiring them to do so for simple http stuff isn't really
reasonable.
* If requests is installed and a .gerritrc file exists in the
user's HOME directory, try to get credentials out of it
for the given gerrit instance. If auth for the correct gerrit
instance exists, use it to auth to gerrit. If no .gerritrc
exists, just use requests with no auth.
Example ~/.gerritrc entry:
review.lineageos.org|invisiblek|httppasswordhere
Change-Id: I86725d36c598877b74e2d3bef5c77e47790f276e
diff --git a/build/tools/repopick.py b/build/tools/repopick.py
index 124e4d3..2af462f 100644
--- a/build/tools/repopick.py
+++ b/build/tools/repopick.py
@@ -31,16 +31,19 @@
from xml.etree import ElementTree
try:
- # For python3
- import urllib.error
- import urllib.request
+ import requests
except ImportError:
- # For python2
- import imp
- import urllib2
- urllib = imp.new_module('urllib')
- urllib.error = urllib2
- urllib.request = urllib2
+ try:
+ # For python3
+ import urllib.error
+ import urllib.request
+ except ImportError:
+ # For python2
+ import imp
+ import urllib2
+ urllib = imp.new_module('urllib')
+ urllib.error = urllib2
+ urllib.request = urllib2
# Verifies whether pathA is a subdirectory (or the same) as pathB
@@ -99,11 +102,29 @@
def fetch_query_via_http(remote_url, query):
-
- """Given a query, fetch the change numbers via http"""
- url = '{0}/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS'.format(remote_url, query)
- data = urllib.request.urlopen(url).read().decode('utf-8')
- reviews = json.loads(data[5:])
+ if "requests" in sys.modules:
+ auth = None
+ if os.path.isfile(os.getenv("HOME") + "/.gerritrc"):
+ f = open(os.getenv("HOME") + "/.gerritrc", "r")
+ for line in f:
+ parts = line.rstrip().split("|")
+ if parts[0] in remote_url:
+ auth = requests.auth.HTTPBasicAuth(username=parts[1], password=parts[2])
+ statusCode = '-1'
+ if auth:
+ url = '{0}/a/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS'.format(remote_url, query)
+ data = requests.get(url, auth=auth)
+ statusCode = str(data.status_code)
+ if statusCode != '200':
+ #They didn't get good authorization or data, Let's try the old way
+ url = '{0}/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS'.format(remote_url, query)
+ data = requests.get(url)
+ reviews = json.loads(data.text[5:])
+ else:
+ """Given a query, fetch the change numbers via http"""
+ url = '{0}/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS'.format(remote_url, query)
+ data = urllib.request.urlopen(url).read().decode('utf-8')
+ reviews = json.loads(data[5:])
for review in reviews:
review['number'] = review.pop('_number')