From ab44f737ec6d86d79b113b8edf48a038ead967ef Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Fri, 11 Dec 2020 13:37:36 -0800 Subject: [PATCH] py2to3 sanity scripts --- .../test/sanity/Dockerfile.template | 2 +- tools/distrib/check_copyright.py | 7 +++--- tools/distrib/check_include_guards.py | 5 ++-- tools/dockerfile/test/sanity/Dockerfile | 2 +- .../run_tests/sanity/check_bazel_workspace.py | 23 ++++++++----------- .../sanity/check_deprecated_grpc++.py | 4 +--- tools/run_tests/sanity/check_port_platform.py | 6 ++--- .../run_tests/sanity/check_test_filtering.py | 12 +++++----- tools/run_tests/sanity/check_tracer_sanity.py | 4 +--- tools/run_tests/sanity/check_version.py | 6 ++--- .../run_tests/sanity/core_banned_functions.py | 4 +--- 11 files changed, 30 insertions(+), 45 deletions(-) diff --git a/templates/tools/dockerfile/test/sanity/Dockerfile.template b/templates/tools/dockerfile/test/sanity/Dockerfile.template index 17c6d048274..bba87819538 100644 --- a/templates/tools/dockerfile/test/sanity/Dockerfile.template +++ b/templates/tools/dockerfile/test/sanity/Dockerfile.template @@ -31,7 +31,7 @@ curl ${"\\"} shellcheck RUN python2 -m pip install simplejson mako virtualenv==16.7.9 lxml - RUN python3 -m pip install simplejson mako virtualenv==16.7.9 lxml + RUN python3 -m pip install simplejson mako virtualenv==16.7.9 lxml six # Add buster-backports for more recent clang packages RUN echo "deb http://deb.debian.org/debian buster-backports main" | tee /etc/apt/sources.list.d/buster-backports.list diff --git a/tools/distrib/check_copyright.py b/tools/distrib/check_copyright.py index 97378283d8c..905725d0000 100755 --- a/tools/distrib/check_copyright.py +++ b/tools/distrib/check_copyright.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2.7 +#!/usr/bin/env python3 # Copyright 2015 gRPC authors. # @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function import argparse import datetime import os @@ -107,7 +106,7 @@ RE_LICENSE = dict( (k, r'\n'.join(LICENSE_PREFIX[k] + (RE_YEAR if re.search(RE_YEAR, line) else re.escape(line)) for line in LICENSE_NOTICE)) - for k, v in LICENSE_PREFIX.iteritems()) + for k, v in LICENSE_PREFIX.items()) if args.precommit: FILE_LIST_COMMAND = 'git status -z | grep -Poz \'(?<=^[MARC][MARCD ] )[^\s]+\'' @@ -143,7 +142,7 @@ ok = True filename_list = [] try: filename_list = subprocess.check_output(FILE_LIST_COMMAND, - shell=True).splitlines() + shell=True).decode().splitlines() except subprocess.CalledProcessError: sys.exit(0) diff --git a/tools/distrib/check_include_guards.py b/tools/distrib/check_include_guards.py index 0d3729013d1..91fe46ea902 100755 --- a/tools/distrib/check_include_guards.py +++ b/tools/distrib/check_include_guards.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2.7 +#!/usr/bin/env python3 # Copyright 2016 gRPC authors. # @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function import argparse import os import os.path @@ -177,7 +176,7 @@ ok = True filename_list = [] try: filename_list = subprocess.check_output(FILE_LIST_COMMAND, - shell=True).splitlines() + shell=True).decode().splitlines() # Filter out non-existent files (ie, file removed or renamed) filename_list = (f for f in filename_list if os.path.isfile(f)) except subprocess.CalledProcessError: diff --git a/tools/dockerfile/test/sanity/Dockerfile b/tools/dockerfile/test/sanity/Dockerfile index 2d4c243b749..3be59ebd3cc 100644 --- a/tools/dockerfile/test/sanity/Dockerfile +++ b/tools/dockerfile/test/sanity/Dockerfile @@ -79,7 +79,7 @@ RUN apt-get update && apt-get install -y \ curl \ shellcheck RUN python2 -m pip install simplejson mako virtualenv==16.7.9 lxml -RUN python3 -m pip install simplejson mako virtualenv==16.7.9 lxml +RUN python3 -m pip install simplejson mako virtualenv==16.7.9 lxml six # Add buster-backports for more recent clang packages RUN echo "deb http://deb.debian.org/debian buster-backports main" | tee /etc/apt/sources.list.d/buster-backports.list diff --git a/tools/run_tests/sanity/check_bazel_workspace.py b/tools/run_tests/sanity/check_bazel_workspace.py index b73f1f2555e..4070e63b1f4 100755 --- a/tools/run_tests/sanity/check_bazel_workspace.py +++ b/tools/run_tests/sanity/check_bazel_workspace.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2016 gRPC authors. # @@ -14,8 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import ast import os import re @@ -27,8 +25,8 @@ os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..')) git_hash_pattern = re.compile('[0-9a-f]{40}') # Parse git hashes from submodules -git_submodules = subprocess.check_output('git submodule', - shell=True).strip().split('\n') +git_submodules = subprocess.check_output( + 'git submodule', shell=True).decode().strip().split('\n') git_submodule_hashes = { re.search(git_hash_pattern, s).group() for s in git_submodules } @@ -148,17 +146,14 @@ build_rules = { 'git_repository': lambda **args: eval_state.git_repository(**args), 'grpc_python_deps': lambda: None, } -exec(bazel_file) in build_rules +exec((bazel_file), build_rules) for name in _GRPC_DEP_NAMES: - assert name in names_and_urls.keys() -if len(_GRPC_DEP_NAMES) != len(names_and_urls.keys()): - assert False, "Diff: " + (str(set(_GRPC_DEP_NAMES) - set(names_and_urls)) + - "," + - str(set(names_and_urls) - set(_GRPC_DEP_NAMES))) + assert name in list(names_and_urls.keys()) +assert len(_GRPC_DEP_NAMES) == len(list(names_and_urls.keys())) # There are some "bazel-only" deps that are exceptions to this sanity check, # we don't require that there is a corresponding git module for these. -names_without_bazel_only_deps = names_and_urls.keys() +names_without_bazel_only_deps = list(names_and_urls.keys()) for dep_name in _GRPC_BAZEL_ONLY_DEPS: names_without_bazel_only_deps.remove(dep_name) archive_urls = [names_and_urls[name] for name in names_without_bazel_only_deps] @@ -195,7 +190,7 @@ for name in _GRPC_DEP_NAMES: 'git_repository': lambda **args: state.git_repository(**args), 'grpc_python_deps': lambda *args, **kwargs: None, } - exec(bazel_file) in rules - assert name not in names_and_urls_with_overridden_name.keys() + exec((bazel_file), rules) + assert name not in list(names_and_urls_with_overridden_name.keys()) sys.exit(0) diff --git a/tools/run_tests/sanity/check_deprecated_grpc++.py b/tools/run_tests/sanity/check_deprecated_grpc++.py index 02307fedaa8..64e2080d53f 100755 --- a/tools/run_tests/sanity/check_deprecated_grpc++.py +++ b/tools/run_tests/sanity/check_deprecated_grpc++.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2018 gRPC authors. # @@ -14,8 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import os import sys diff --git a/tools/run_tests/sanity/check_port_platform.py b/tools/run_tests/sanity/check_port_platform.py index b1ef13716f9..4b7c92f02ce 100755 --- a/tools/run_tests/sanity/check_port_platform.py +++ b/tools/run_tests/sanity/check_port_platform.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2017 gRPC authors. # @@ -64,6 +64,6 @@ all_bad_files += check_port_platform_inclusion(os.path.join('include', 'grpc')) if len(all_bad_files) > 0: for f in all_bad_files: - print(('port_platform.h is not the first included header or there ' - 'is not a blank line following its inclusion in %s') % f) + print((('port_platform.h is not the first included header or there ' + 'is not a blank line following its inclusion in %s') % f)) sys.exit(1) diff --git a/tools/run_tests/sanity/check_test_filtering.py b/tools/run_tests/sanity/check_test_filtering.py index 0b6b77ec17c..ea9039f7f4a 100755 --- a/tools/run_tests/sanity/check_test_filtering.py +++ b/tools/run_tests/sanity/check_test_filtering.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2016 gRPC authors. # @@ -67,8 +67,8 @@ class TestFilteringTest(unittest.TestCase): filtered_jobs = [ job for job in filtered_jobs if "sanity" not in job.labels ] - self.assertEquals(sanity_tests_in_all_jobs, - sanity_tests_in_filtered_jobs) + self.assertEqual(sanity_tests_in_all_jobs, + sanity_tests_in_filtered_jobs) for label in labels: for job in filtered_jobs: @@ -79,8 +79,8 @@ class TestFilteringTest(unittest.TestCase): for job in all_jobs: if (label in job.labels): jobs_matching_labels += 1 - self.assertEquals(len(filtered_jobs), - len(all_jobs) - jobs_matching_labels) + self.assertEqual(len(filtered_jobs), + len(all_jobs) - jobs_matching_labels) def test_individual_language_filters(self): # Changing unlisted file should trigger all languages @@ -152,7 +152,7 @@ class TestFilteringTest(unittest.TestCase): 'src/core/foo.bar', 'some_file_not_on_the_white_list', 'BUILD', 'etc/roots.pem', 'Makefile', 'tools/foo' ] - for key in whitelist.keys(): + for key in list(whitelist.keys()): for file_name in files_that_should_trigger_all_tests: self.assertFalse(re.match(key, file_name)) diff --git a/tools/run_tests/sanity/check_tracer_sanity.py b/tools/run_tests/sanity/check_tracer_sanity.py index c4c76530ded..019e44fd182 100755 --- a/tools/run_tests/sanity/check_tracer_sanity.py +++ b/tools/run_tests/sanity/check_tracer_sanity.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2017 gRPC authors. # @@ -14,8 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import os import sys import re diff --git a/tools/run_tests/sanity/check_version.py b/tools/run_tests/sanity/check_version.py index d53fa094aa6..5e963c480a3 100755 --- a/tools/run_tests/sanity/check_version.py +++ b/tools/run_tests/sanity/check_version.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2016 gRPC authors. # @@ -14,8 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import sys import yaml import os @@ -66,7 +64,7 @@ if not check_version(top_version): errors += 1 print(warning % ('version', top_version)) -for tag, value in settings.iteritems(): +for tag, value in settings.items(): if re.match(r'^[a-z]+_version$', tag): value = Version(value) if tag != 'core_version': diff --git a/tools/run_tests/sanity/core_banned_functions.py b/tools/run_tests/sanity/core_banned_functions.py index a551c801dfd..200ff3943c2 100755 --- a/tools/run_tests/sanity/core_banned_functions.py +++ b/tools/run_tests/sanity/core_banned_functions.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright 2016 gRPC authors. # @@ -14,8 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import os import sys