Skip to content

Commit

Permalink
Run Modernize on the common directory.
Browse files Browse the repository at this point in the history
- This does not address cases not covered by Modernize (e.g. StringIO).
- py_vulcanize/third_party is left as-is (it's already compatible with both)
- py_utils/webpagereplay_go_server.py will be handled in a followup CL

Change-Id: I586172dbff213d7f723306f24e358905f941abbb
Bug: chromium:1198408
Reviewed-on: https://chromium-review.googlesource.com/c/catapult/+/2906478
Commit-Queue: Ryan Heise <[email protected]>
Reviewed-by: John Chen <[email protected]>
  • Loading branch information
heiserya-g authored and Catapult LUCI CQ committed May 20, 2021
1 parent 7da48e9 commit 88390a1
Show file tree
Hide file tree
Showing 77 changed files with 119 additions and 37 deletions.
5 changes: 3 additions & 2 deletions common/bin/update_chrome_reference_binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
"""

from __future__ import print_function
from __future__ import absolute_import
import argparse
import collections
import logging
import os
import six
import shutil
import subprocess
import sys
import tempfile
import urllib2
import zipfile

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'py_utils'))
Expand Down Expand Up @@ -171,7 +172,7 @@ def _ChannelVersionsMap(channel):

def _OmahaReportVersionInfo(channel):
url ='https://omahaproxy.appspot.com/all?channel=%s' % channel
lines = urllib2.urlopen(url).readlines()
lines = six.moves.urllib.request.urlopen(url).readlines()
return [l.split(',') for l in lines]


Expand Down
12 changes: 6 additions & 6 deletions common/lab/commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"""Print statistics about the rate of commits to a repository."""

from __future__ import print_function
from __future__ import absolute_import
import datetime
import itertools
import json
import math
import urllib
import urllib2
import six


_BASE_URL = 'https://chromium.googlesource.com'
Expand All @@ -30,7 +30,7 @@ def Pairwise(iterable):
"""s -> (s0,s1), (s1,s2), (s2, s3), ..."""
a, b = itertools.tee(iterable)
next(b, None)
return itertools.izip(a, b)
return six.zip(a, b)


def Percentile(data, percentile):
Expand Down Expand Up @@ -59,9 +59,9 @@ def Percentile(data, percentile):


def CommitTimes(repository, revision_count):
parameters = urllib.urlencode((('n', revision_count), ('format', 'JSON')))
url = '%s/%s/+log?%s' % (_BASE_URL, urllib.quote(repository), parameters)
data = json.loads(''.join(urllib2.urlopen(url).read().splitlines()[1:]))
parameters = six.moves.urllib.parse.urlencode((('n', revision_count), ('format', 'JSON')))
url = '%s/%s/+log?%s' % (_BASE_URL, six.moves.urllib.parse.quote(repository), parameters)
data = json.loads(''.join(six.moves.urllib.request.urlopen(url).read().splitlines()[1:]))

commit_times = []
for revision in data['log']:
Expand Down
11 changes: 6 additions & 5 deletions common/lab/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

"""Query build slave hardware info, and print it to stdout as csv."""

from __future__ import absolute_import
import csv
import json
import logging
import six
import sys
import urllib2


_MASTERS = [
Expand Down Expand Up @@ -50,11 +51,11 @@ def main():
writer.writeheader()

for master_name in _MASTERS:
master_data = json.load(urllib2.urlopen(
master_data = json.load(six.moves.urllib.request.urlopen(
'http://build.chromium.org/p/%s/json/slaves' % master_name))

slaves = sorted(master_data.iteritems(),
key=lambda x: (x[1]['builders'].keys(), x[0]))
slaves = sorted(six.iteritems(master_data),
key=lambda x: (list(x[1]['builders'].keys()), x[0]))
for slave_name, slave_data in slaves:
for builder_name in slave_data['builders']:
row = {
Expand All @@ -76,7 +77,7 @@ def main():
row[key] = value

# Munge keys.
row = {key.replace('_', ' '): value for key, value in row.iteritems()}
row = {key.replace('_', ' '): value for key, value in six.iteritems(row)}
if 'osfamily' in row:
row['os family'] = row.pop('osfamily')
if 'product name' not in row and slave_name.startswith('slave'):
Expand Down
1 change: 1 addition & 0 deletions common/node_runner/node_runner/node_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import absolute_import
import os
import subprocess
import sys
Expand Down
1 change: 1 addition & 0 deletions common/py_trace_event/py_trace_event/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import os
import sys

Expand Down
1 change: 1 addition & 0 deletions common/py_trace_event/py_trace_event/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
from distutils.core import setup
setup(
name='py_trace_event',
Expand Down
6 changes: 4 additions & 2 deletions common/py_trace_event/py_trace_event/trace_event.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
from py_trace_event import trace_time
import six


r"""Instrumentation-based profiling for Python.
Expand Down Expand Up @@ -48,7 +50,7 @@ def bar(self):
"""

try:
import trace_event_impl
from . import trace_event_impl
except ImportError:
trace_event_impl = None

Expand Down Expand Up @@ -88,7 +90,7 @@ def trace_flush():
trace_event_impl.trace_flush()

def trace_begin(name, **kwargs):
args_to_log = {key: repr(value) for key, value in kwargs.iteritems()}
args_to_log = {key: repr(value) for key, value in six.iteritems(kwargs)}
trace_event_impl.add_trace_event("B", trace_time.Now(), "python", name,
args_to_log)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from log import *
from decorators import *
from meta_class import *
import multiprocessing_shim
from __future__ import absolute_import
from .log import *
from .decorators import *
from .meta_class import *
from . import multiprocessing_shim
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import contextlib
import inspect
import time
import functools

import log
from . import log
from py_trace_event import trace_time
import six
from six.moves import map


@contextlib.contextmanager
def trace(name, **kwargs):
category = "python"
start = trace_time.Now()
args_to_log = {key: repr(value) for key, value in kwargs.iteritems()}
args_to_log = {key: repr(value) for key, value in six.iteritems(kwargs)}
log.add_trace_event("B", start, category, name, args_to_log)
try:
yield
Expand Down Expand Up @@ -42,7 +45,7 @@ def arg_spec_tuple(name):
default = None
return (name, arg_index, default)

args_to_log = map(arg_spec_tuple, arg_names)
args_to_log = list(map(arg_spec_tuple, arg_names))

@functools.wraps(func)
def traced_function(*args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import decorators
from __future__ import absolute_import
from . import decorators
import logging
import unittest

from trace_test import TraceTest
#from .trace_test import TraceTest
from .trace_test import TraceTest

def generator():
yield 1
Expand Down
6 changes: 4 additions & 2 deletions common/py_trace_event/py_trace_event/trace_event_impl/log.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import atexit
import json
import os
import sys
import time
import threading
import multiprocessing
import multiprocessing_shim

from py_trace_event.trace_event_impl import perfetto_trace_writer
from py_trace_event import trace_time

from py_utils import lock
import six


# Trace file formats:
Expand Down Expand Up @@ -170,7 +171,7 @@ def _trace_enable(log_file=None, format=None):
log_file = open("%s.pb" % n, "ab", False)
else:
log_file = open("%s.json" % n, "ab", False)
elif isinstance(log_file, basestring):
elif isinstance(log_file, six.string_types):
log_file = open("%s" % log_file, "ab", False)
elif not hasattr(log_file, 'fileno'):
raise TraceException(
Expand All @@ -191,6 +192,7 @@ def _trace_enable(log_file=None, format=None):
_note("trace_event: Opened existing tracelog")
_log_file.flush()
# Monkeypatch in our process replacement for the multiprocessing.Process class
from . import multiprocessing_shim
if multiprocessing.Process != multiprocessing_shim.ProcessShim:
multiprocessing.Process = multiprocessing_shim.ProcessShim

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import logging
import os
import sys
import unittest

from log import *
from parsed_trace_events import *
from .log import *
from .parsed_trace_events import *
from py_utils import tempfile_ext


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import absolute_import
import types

from py_trace_event.trace_event_impl import decorators
import six


class TracedMetaClass(type):
def __new__(cls, name, bases, attrs):
for attr_name, attr_value in attrs.iteritems():
for attr_name, attr_value in six.iteritems(attrs):
if (not attr_name.startswith('_') and
isinstance(attr_value, types.FunctionType)):
attrs[attr_name] = decorators.traced(attr_value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import multiprocessing
import log
import time


Expand All @@ -16,6 +16,7 @@ def __init__(self, shim, *args, **kwards):
self._shim = shim

def run(self,*args,**kwargs):
from . import log
log._disallow_tracing_control()
try:
r = _RealProcess.run(self, *args, **kwargs)
Expand All @@ -37,6 +38,7 @@ def start(self):
self._proc.start()

def terminate(self):
from . import log
if log.trace_is_enabled():
# give the flush a chance to finish --> TODO: find some other way.
time.sleep(0.25)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import math
import json

Expand Down Expand Up @@ -47,7 +48,7 @@ def __init__(self, events = None, trace_filename = None):
events = events['traceEvents']

if not hasattr(events, '__iter__'):
raise Exception, 'events must be iteraable.'
raise Exception('events must be iteraable.')
self.events = events
self.pids = None
self.tids = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
https://android.googlesource.com/platform/external/perfetto/+/refs/heads/master/protos/perfetto/trace/
"""

from __future__ import absolute_import
import encoder
import wire_format

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
""" Functions to write trace data in perfetto protobuf format.
"""

from __future__ import absolute_import
import collections

import perfetto_proto_classes as proto
from . import perfetto_proto_classes as proto
import six

CLOCK_BOOTTIME = 6
CLOCK_TELEMETRY = 64
Expand Down Expand Up @@ -128,7 +130,7 @@ def write_event(output, ph, category, name, ts, args, tid):
legacy_event.name_iid = _intern_event_name(name, packet, tid)
packet.track_event.legacy_event = legacy_event

for name, value in args.iteritems():
for name, value in six.iteritems(args):
debug_annotation = proto.DebugAnnotation()
debug_annotation.name = name
if isinstance(value, int):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import absolute_import
import unittest
import StringIO

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import unittest

#from .log import *
#from .parsed_trace_events import *

from log import *
from parsed_trace_events import *
from .log import *
from .parsed_trace_events import *
from py_utils import tempfile_ext

class TraceTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import absolute_import
import contextlib
import json
import logging
Expand Down
Loading

0 comments on commit 88390a1

Please sign in to comment.