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')