Skip to content

Commit

Permalink
[Perf] Python 3 conversion: urllib
Browse files Browse the repository at this point in the history
Fix usage of urllib and urllib2 in tools/perf directory.
This CL was generated by the following command, and then manually
edited to resolve pylint errors.

    python3 -m modernize -w -f urllib_six tools/perf

Bug: 1199156
Change-Id: I2149804d12d1ffc76ec8c9fc242ec07559bdacde
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2838448
Reviewed-by: Wenbin Zhang <wenbinzhang@google.com>
Commit-Queue: John Chen <johnchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#874126}
  • Loading branch information
JohnChen0 authored and Chromium LUCI CQ committed Apr 20, 2021
1 parent bdb2269 commit 0c90633
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
11 changes: 6 additions & 5 deletions tools/perf/cli_tools/flakiness_cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# found in the LICENSE file.

import json
import urllib
import urllib2
import six.moves.urllib.parse # pylint: disable=import-error
import six.moves.urllib.request # pylint: disable=import-error


TEST_RESULTS_SERVER = 'http://test-results.appspot.com'
Expand All @@ -15,9 +15,10 @@ def _Request(path, params=None):
"""Request json data from the test results server."""
url = TEST_RESULTS_SERVER + path
if params:
url += '?' + urllib.urlencode(params)
request = urllib2.Request(url, headers={'User-Agent': TOOL_USER_AGENT})
response = urllib2.urlopen(request)
url += '?' + six.moves.urllib.parse.urlencode(params)
request = six.moves.urllib.request.Request(
url, headers={'User-Agent': TOOL_USER_AGENT})
response = six.moves.urllib.request.urlopen(request)
return json.load(response)


Expand Down
4 changes: 2 additions & 2 deletions tools/perf/core/bot_platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import urllib
import six.moves.urllib.parse # pylint: disable=import-error

from core import benchmark_finders
from core import benchmark_utils
Expand Down Expand Up @@ -123,7 +123,7 @@ def is_official(self):
@property
def builder_url(self):
return ('https://ci.chromium.org/p/chrome/builders/ci/%s' %
urllib.quote(self._name))
six.moves.urllib.parse.quote(self._name))


class BenchmarkConfig(object):
Expand Down
28 changes: 15 additions & 13 deletions tools/perf/core/results_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import sys
import time
import traceback
import urllib
import urllib2
import zlib
import logging
import six.moves.urllib.error # pylint: disable=import-error
import six.moves.urllib.parse # pylint: disable=import-error
import six.moves.urllib.request # pylint: disable=import-error

# TODO(crbug.com/996778): Figure out how to get httplib2 hermetically.
import httplib2 # pylint: disable=import-error
Expand Down Expand Up @@ -311,9 +312,9 @@ def _MakeStdioUrl(test_name, buildername, buildnumber):

return '%sbuilders/%s/builds/%s/steps/%s/logs/stdio' % (
_GetBuildBotUrl(),
urllib.quote(buildername),
urllib.quote(str(buildnumber)),
urllib.quote(test_name))
six.moves.urllib.parse.quote(buildername),
six.moves.urllib.parse.quote(str(buildnumber)),
six.moves.urllib.parse.quote(test_name))


def _MakeBuildStatusUrl(project, buildbucket, buildername, buildnumber):
Expand All @@ -324,10 +325,10 @@ def _MakeBuildStatusUrl(project, buildbucket, buildername, buildnumber):
if not buildbucket:
buildbucket = 'ci'
return 'https://ci.chromium.org/ui/p/%s/builders/%s/%s/%s' % (
urllib.quote(project),
urllib.quote(buildbucket),
urllib.quote(buildername),
urllib.quote(str(buildnumber)))
six.moves.urllib.parse.quote(project),
six.moves.urllib.parse.quote(buildbucket),
six.moves.urllib.parse.quote(buildername),
six.moves.urllib.parse.quote(str(buildnumber)))


def _GetStdioUriColumn(test_name, buildername, buildnumber):
Expand Down Expand Up @@ -455,14 +456,15 @@ def _SendResultsJson(url, results_json, token_generator_callback):
"""
# When data is provided to urllib2.Request, a POST is sent instead of GET.
# The data must be in the application/x-www-form-urlencoded format.
data = urllib.urlencode({'data': results_json})
req = urllib2.Request(url + SEND_RESULTS_PATH, data)
data = six.moves.urllib.parse.urlencode({'data': results_json})
req = six.moves.urllib.request.Request(url + SEND_RESULTS_PATH, data)
try:
oauth_token = token_generator_callback()
req.headers['Authorization'] = 'Bearer %s' % oauth_token

urllib2.urlopen(req, timeout=60 * 5)
except (urllib2.HTTPError, urllib2.URLError, httplib.HTTPException):
six.moves.urllib.request.urlopen(req, timeout=60 * 5)
except (six.moves.urllib.error.HTTPError, six.moves.urllib.error.URLError,
httplib.HTTPException):
error = traceback.format_exc()

if 'HTTPError: 400' in error:
Expand Down
5 changes: 3 additions & 2 deletions tools/perf/core/services/dashboard_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
https://chromium.googlesource.com/catapult.git/+/HEAD/dashboard/dashboard/api/README.md
"""

import urllib
import six.moves.urllib.parse # pylint: disable=import-error

from core.services import request

Expand Down Expand Up @@ -82,7 +82,8 @@ def Timeseries(test_path, days=30):
KeyError if the test_path is not found.
"""
try:
return Request('/api/timeseries/%s' % urllib.quote(test_path),
return Request('/api/timeseries/%s' %
six.moves.urllib.parse.quote(test_path),
params={'num_days': days})
except request.ClientError as exc:
if 'Invalid test_path' in exc.json['error']:
Expand Down
6 changes: 3 additions & 3 deletions tools/perf/core/services/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import json
import logging
import urllib
import six.moves.urllib.parse # pylint: disable=import-error

# TODO(crbug.com/996778): Figure out how to get httplib2 hermetically.
import httplib2 # pylint: disable=import-error
Expand Down Expand Up @@ -116,7 +116,7 @@ def Request(url, method='GET', params=None, data=None, accept=None,
del retries # Handled by the decorator.

if params:
url = '%s?%s' % (url, urllib.urlencode(params))
url = '%s?%s' % (url, six.moves.urllib.parse.urlencode(params))

body = None
headers = {}
Expand All @@ -131,7 +131,7 @@ def Request(url, method='GET', params=None, data=None, accept=None,
body = json.dumps(data, sort_keys=True, separators=(',', ':'))
headers['Content-Type'] = 'application/json'
elif content_type == 'urlencoded':
body = urllib.urlencode(data)
body = six.moves.urllib.parse.urlencode(data)
headers['Content-Type'] = 'application/x-www-form-urlencoded'
else:
raise NotImplementedError('Invalid content type: %s' % content_type)
Expand Down
8 changes: 4 additions & 4 deletions tools/perf/core/upload_results_to_perf_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import sys
import tempfile
import time
import urllib
import logging
import six.moves.urllib.parse # pylint: disable=import-error

from core import results_dashboard

Expand Down Expand Up @@ -201,9 +201,9 @@ def GetDashboardUrl(name, configuration_name, results_url,
"""
name = name.replace('.reference', '')
dashboard_url = results_url + RESULTS_LINK_PATH % (
urllib.quote(perf_dashboard_machine_group),
urllib.quote(configuration_name),
urllib.quote(name),
six.moves.urllib.parse.quote(perf_dashboard_machine_group),
six.moves.urllib.parse.quote(configuration_name),
six.moves.urllib.parse.quote(name),
_CommitPositionNumber(got_revision_cp))

return dashboard_url
Expand Down

0 comments on commit 0c90633

Please sign in to comment.