From 4e1ee07a79a1d4f696c259c1774e215e389c8eee Mon Sep 17 00:00:00 2001 From: marmeladema Date: Mon, 14 Oct 2019 00:15:02 +0100 Subject: [PATCH] Improve print statement compatibility with Python3 --- python/servo/command_base.py | 12 +++++++----- python/servo/lints/wpt_lint.py | 4 +++- python/servo/mutation/mutator.py | 6 ++++-- python/tidy/servo_tidy/tidy.py | 18 ++++++++++-------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/python/servo/command_base.py b/python/servo/command_base.py index 7f4e941a4c7..67f91e6a1d4 100644 --- a/python/servo/command_base.py +++ b/python/servo/command_base.py @@ -7,6 +7,8 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. +from __future__ import print_function + from errno import ENOENT as NO_SUCH_FILE_OR_DIRECTORY from glob import glob import shutil @@ -350,15 +352,15 @@ class CommandBase(object): version_line = subprocess.check_output(["rustup" + BIN_SUFFIX, "--version"]) except OSError as e: if e.errno == NO_SUCH_FILE_OR_DIRECTORY: - print "It looks like rustup is not installed. See instructions at " \ - "https://github.com/servo/servo/#setting-up-your-environment" - print + print("It looks like rustup is not installed. See instructions at " + "https://github.com/servo/servo/#setting-up-your-environment") + print() return 1 raise version = tuple(map(int, re.match("rustup (\d+)\.(\d+)\.(\d+)", version_line).groups())) if version < (1, 11, 0): - print "rustup is at version %s.%s.%s, Servo requires 1.11.0 or more recent." % version - print "Try running 'rustup self update'." + print("rustup is at version %s.%s.%s, Servo requires 1.11.0 or more recent." % version) + print("Try running 'rustup self update'.") return 1 toolchain = self.toolchain() if platform.system() == "Windows": diff --git a/python/servo/lints/wpt_lint.py b/python/servo/lints/wpt_lint.py index a1c7e528c77..e2e44c729ae 100644 --- a/python/servo/lints/wpt_lint.py +++ b/python/servo/lints/wpt_lint.py @@ -7,6 +7,8 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. +from __future__ import print_function + import os import sys @@ -20,7 +22,7 @@ class Lint(LintRunner): def _get_wpt_files(self, suite): working_dir = os.path.join(WPT_PATH, suite, '') file_iter = self.get_files(working_dir, exclude_dirs=[]) - print '\nRunning the WPT lint on %s...' % working_dir + print('\nRunning the WPT lint on %s...' % working_dir) for f in file_iter: if filter_file(f): yield f[len(working_dir):] diff --git a/python/servo/mutation/mutator.py b/python/servo/mutation/mutator.py index f92b388e487..8018ddc6116 100644 --- a/python/servo/mutation/mutator.py +++ b/python/servo/mutation/mutator.py @@ -7,6 +7,8 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. +from __future__ import print_function + import fileinput import re import random @@ -28,7 +30,7 @@ def init_variables(if_blocks): def deleteStatements(file_name, line_numbers): for line in fileinput.input(file_name, inplace=True): if fileinput.lineno() not in line_numbers: - print line.rstrip() + print(line.rstrip()) class Strategy: @@ -48,7 +50,7 @@ class Strategy: for line in fileinput.input(file_name, inplace=True): if fileinput.lineno() == mutation_line_number: line = re.sub(self._replace_strategy['regex'], self._replace_strategy['replaceString'], line) - print line.rstrip() + print(line.rstrip()) return mutation_line_number diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index 0069f344fa7..efd3648b2ab 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -7,6 +7,8 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. +from __future__ import print_function + import contextlib import fnmatch import imp @@ -514,7 +516,7 @@ def check_manifest_dirs(config_file, print_text=True): lines = conf_file.splitlines(True) if print_text: - print '\rChecking the wpt manifest file...' + print('\rChecking the wpt manifest file...') p = parser.parse(lines) paths = rec_parse(wpt_path("web-platform-tests"), p) @@ -908,7 +910,7 @@ def check_config_file(config_file, print_text=True): lines = conf_file.splitlines(True) if print_text: - print '\rChecking the config file...' + print('\rChecking the config file...') config_content = toml.loads(conf_file) exclude = config_content.get("ignore", {}) @@ -995,7 +997,7 @@ def parse_config(config_file): def check_directory_files(directories, print_text=True): if print_text: - print '\rChecking directories for correct file extensions...' + print('\rChecking directories for correct file extensions...') for directory, file_extensions in directories.items(): files = sorted(os.listdir(directory)) for filename in files: @@ -1015,7 +1017,7 @@ def collect_errors_for_files(files_to_check, checking_functions, line_checking_f if not has_element: raise StopIteration if print_text: - print '\rChecking files for tidiness...' + print('\rChecking files for tidiness...') for filename in files_to_check: if not os.path.exists(filename): @@ -1037,7 +1039,7 @@ def collect_errors_for_files(files_to_check, checking_functions, line_checking_f def get_dep_toml_files(only_changed_files=False): if not only_changed_files: - print '\nRunning the dependency licensing lint...' + print('\nRunning the dependency licensing lint...') for root, directories, filenames in os.walk(".cargo"): for filename in filenames: if filename == "Cargo.toml": @@ -1136,11 +1138,11 @@ def scan(only_changed_files=False, progress=True, stylo=False): error = None for error in errors: colorama.init() - print "\r\033[94m{}\033[0m:\033[93m{}\033[0m: \033[91m{}\033[0m".format(*error) + print("\r\033[94m{}\033[0m:\033[93m{}\033[0m: \033[91m{}\033[0m".format(*error)) - print + print() if error is None: colorama.init() - print "\033[92mtidy reported no errors.\033[0m" + print("\033[92mtidy reported no errors.\033[0m") return int(error is not None)