From 1cdd380e695f88cd6ffce1c23fe0bbd48f11db57 Mon Sep 17 00:00:00 2001 From: Dan Pasanen Date: Mon, 23 Jan 2017 15:08:52 -0600 Subject: [PATCH] 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: I95be26d51bfd31b53f3613e8dbfb7bba46324571 --- build/tools/repopick.py | 49 +++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/build/tools/repopick.py b/build/tools/repopick.py index b571f2b5..0132124f 100755 --- a/build/tools/repopick.py +++ b/build/tools/repopick.py @@ -32,16 +32,19 @@ import textwrap 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 @@ -100,11 +103,29 @@ def fetch_query_via_ssh(remote_url, query): 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')