From a920a17b2f418535870788ae81234dc6b8aa6661 Mon Sep 17 00:00:00 2001 From: Paul Burton Date: Tue, 27 Sep 2016 16:03:50 +0100 Subject: [PATCH] patman: Make print statements python 3.x safe In python 3.x, print must be used as a function call. Convert all print statements to the function call style, importing from __future__ where we print with no trailing newline or print to a file object. Signed-off-by: Paul Burton Acked-by: Simon Glass --- tools/patman/checkpatch.py | 16 ++++++++-------- tools/patman/get_maintainer.py | 2 +- tools/patman/gitutil.py | 6 +++--- tools/patman/patchstream.py | 6 +++--- tools/patman/patman.py | 12 ++++++------ tools/patman/series.py | 38 ++++++++++++++++++++------------------ tools/patman/settings.py | 16 +++++++++------- tools/patman/terminal.py | 12 +++++++----- tools/patman/test.py | 2 +- 9 files changed, 58 insertions(+), 52 deletions(-) diff --git a/tools/patman/checkpatch.py b/tools/patman/checkpatch.py index 3eef6de..be78fc5 100644 --- a/tools/patman/checkpatch.py +++ b/tools/patman/checkpatch.py @@ -83,7 +83,7 @@ def CheckPatch(fname, verbose=False): for line in result.stdout.splitlines(): if verbose: - print line + print(line) # A blank line indicates the end of a message if not line and item: @@ -151,17 +151,17 @@ def CheckPatches(verbose, args): error_count += result.errors warning_count += result.warnings check_count += result.checks - print '%d errors, %d warnings, %d checks for %s:' % (result.errors, - result.warnings, result.checks, col.Color(col.BLUE, fname)) + print('%d errors, %d warnings, %d checks for %s:' % (result.errors, + result.warnings, result.checks, col.Color(col.BLUE, fname))) if (len(result.problems) != result.errors + result.warnings + result.checks): - print "Internal error: some problems lost" + print("Internal error: some problems lost") for item in result.problems: - print GetWarningMsg(col, item.get('type', ''), + print(GetWarningMsg(col, item.get('type', ''), item.get('file', ''), - item.get('line', 0), item.get('msg', 'message')) + item.get('line', 0), item.get('msg', 'message'))) print - #print stdout + #print(stdout) if error_count or warning_count or check_count: str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)' color = col.GREEN @@ -169,6 +169,6 @@ def CheckPatches(verbose, args): color = col.YELLOW if error_count: color = col.RED - print col.Color(color, str % (error_count, warning_count, check_count)) + print(col.Color(color, str % (error_count, warning_count, check_count))) return False return True diff --git a/tools/patman/get_maintainer.py b/tools/patman/get_maintainer.py index 00b4939..2deb5db 100644 --- a/tools/patman/get_maintainer.py +++ b/tools/patman/get_maintainer.py @@ -40,7 +40,7 @@ def GetMaintainer(fname, verbose=False): get_maintainer = FindGetMaintainer() if not get_maintainer: if verbose: - print "WARNING: Couldn't find get_maintainer.pl" + print("WARNING: Couldn't find get_maintainer.pl") return [] stdout = command.Output(get_maintainer, '--norolestats', fname) diff --git a/tools/patman/gitutil.py b/tools/patman/gitutil.py index bb7c9e0..c0fe093 100644 --- a/tools/patman/gitutil.py +++ b/tools/patman/gitutil.py @@ -491,7 +491,7 @@ def LookupEmail(lookup_name, alias=None, raise_on_error=True, level=0): if raise_on_error: raise OSError, msg else: - print col.Color(col.RED, msg) + print(col.Color(col.RED, msg)) return out_list if lookup_name: @@ -500,7 +500,7 @@ def LookupEmail(lookup_name, alias=None, raise_on_error=True, level=0): if raise_on_error: raise ValueError, msg else: - print col.Color(col.RED, msg) + print(col.Color(col.RED, msg)) return out_list for item in alias[lookup_name]: todo = LookupEmail(item, alias, raise_on_error, level + 1) @@ -508,7 +508,7 @@ def LookupEmail(lookup_name, alias=None, raise_on_error=True, level=0): if not new_item in out_list: out_list.append(new_item) - #print "No match for alias '%s'" % lookup_name + #print("No match for alias '%s'" % lookup_name) return out_list def GetTopLevel(): diff --git a/tools/patman/patchstream.py b/tools/patman/patchstream.py index 69d5cfb..cd4667f 100644 --- a/tools/patman/patchstream.py +++ b/tools/patman/patchstream.py @@ -480,12 +480,12 @@ def FixPatches(series, fnames): commit.patch = fname result = FixPatch(backup_dir, fname, series, commit) if result: - print '%d warnings for %s:' % (len(result), fname) + print('%d warnings for %s:' % (len(result), fname)) for warn in result: - print '\t', warn + print('\t', warn) print count += 1 - print 'Cleaned %d patches' % count + print('Cleaned %d patches' % count) return series def InsertCoverLetter(fname, series, count): diff --git a/tools/patman/patman.py b/tools/patman/patman.py index fe50eb4..fdbee67 100755 --- a/tools/patman/patman.py +++ b/tools/patman/patman.py @@ -93,11 +93,11 @@ elif options.test: suite.run(result) # TODO: Surely we can just 'print' result? - print result + print(result) for test, err in result.errors: - print err + print(err) for test, err in result.failures: - print err + print(err) # Called from git with a patch filename as argument # Printout a list of additional CC recipients for this patch @@ -110,7 +110,7 @@ elif options.cc_cmd: for cc in match.group(2).split(', '): cc = cc.strip() if cc: - print cc + print(cc) fd.close() elif options.full_help: @@ -166,12 +166,12 @@ else: options.dry_run, not options.ignore_bad_tags, cc_file, in_reply_to=options.in_reply_to, thread=options.thread) else: - print col.Color(col.RED, "Not sending emails due to errors/warnings") + print(col.Color(col.RED, "Not sending emails due to errors/warnings")) # For a dry run, just show our actions as a sanity check if options.dry_run: series.ShowActions(args, cmd, options.process_tags) if not its_a_go: - print col.Color(col.RED, "Email would not be sent") + print(col.Color(col.RED, "Email would not be sent")) os.remove(cc_file) diff --git a/tools/patman/series.py b/tools/patman/series.py index 1ce30c6..38a452e 100644 --- a/tools/patman/series.py +++ b/tools/patman/series.py @@ -3,6 +3,8 @@ # SPDX-License-Identifier: GPL-2.0+ # +from __future__ import print_function + import itertools import os @@ -101,38 +103,38 @@ class Series(dict): cc_set = set(gitutil.BuildEmailList(self.cc)); col = terminal.Color() - print 'Dry run, so not doing much. But I would do this:' - print - print 'Send a total of %d patch%s with %scover letter.' % ( + print('Dry run, so not doing much. But I would do this:') + print() + print('Send a total of %d patch%s with %scover letter.' % ( len(args), '' if len(args) == 1 else 'es', - self.get('cover') and 'a ' or 'no ') + self.get('cover') and 'a ' or 'no ')) # TODO: Colour the patches according to whether they passed checks for upto in range(len(args)): commit = self.commits[upto] - print col.Color(col.GREEN, ' %s' % args[upto]) + print(col.Color(col.GREEN, ' %s' % args[upto])) cc_list = list(self._generated_cc[commit.patch]) for email in set(cc_list) - to_set - cc_set: if email == None: email = col.Color(col.YELLOW, "" % tag) if email: - print ' Cc: ',email + print(' Cc: ', email) print for item in to_set: - print 'To:\t ', item + print('To:\t ', item) for item in cc_set - to_set: - print 'Cc:\t ', item - print 'Version: ', self.get('version') - print 'Prefix:\t ', self.get('prefix') + print('Cc:\t ', item) + print('Version: ', self.get('version')) + print('Prefix:\t ', self.get('prefix')) if self.cover: - print 'Cover: %d lines' % len(self.cover) + print('Cover: %d lines' % len(self.cover)) cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) all_ccs = itertools.chain(cover_cc, *self._generated_cc.values()) for email in set(all_ccs) - to_set - cc_set: - print ' Cc: ',email + print(' Cc: ', email) if cmd: - print 'Git command: %s' % cmd + print('Git command: %s' % cmd) def MakeChangeLog(self, commit): """Create a list of changes for each version. @@ -191,13 +193,13 @@ class Series(dict): else: if version > 1: str = 'Change log missing for v%d' % version - print col.Color(col.RED, str) + print(col.Color(col.RED, str)) for version in changes_copy: str = 'Change log for unknown version v%d' % version - print col.Color(col.RED, str) + print(col.Color(col.RED, str)) elif self.changes: str = 'Change log exists, but no version is set' - print col.Color(col.RED, str) + print(col.Color(col.RED, str)) def MakeCcFile(self, process_tags, cover_fname, raise_on_error, add_maintainers): @@ -228,12 +230,12 @@ class Series(dict): if add_maintainers: list += get_maintainer.GetMaintainer(commit.patch) all_ccs += list - print >>fd, commit.patch, ', '.join(set(list)) + print(commit.patch, ', '.join(set(list)), file=fd) self._generated_cc[commit.patch] = list if cover_fname: cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) - print >>fd, cover_fname, ', '.join(set(cover_cc + all_ccs)) + print(cover_fname, ', '.join(set(cover_cc + all_ccs)), file=fd) fd.close() return fname diff --git a/tools/patman/settings.py b/tools/patman/settings.py index ba2a68f..01f6c38 100644 --- a/tools/patman/settings.py +++ b/tools/patman/settings.py @@ -3,6 +3,8 @@ # SPDX-License-Identifier: GPL-2.0+ # +from __future__ import print_function + import ConfigParser import os import re @@ -156,7 +158,7 @@ def ReadGitAliases(fname): try: fd = open(fname, 'r') except IOError: - print "Warning: Cannot find alias file '%s'" % fname + print("Warning: Cannot find alias file '%s'" % fname) return re_line = re.compile('alias\s+(\S+)\s+(.*)') @@ -167,7 +169,7 @@ def ReadGitAliases(fname): m = re_line.match(line) if not m: - print "Warning: Alias file line '%s' not understood" % line + print("Warning: Alias file line '%s' not understood" % line) continue list = alias.get(m.group(1), []) @@ -200,10 +202,10 @@ def CreatePatmanConfigFile(config_fname): try: f = open(config_fname, 'w') except IOError: - print "Couldn't create patman config file\n" + print("Couldn't create patman config file\n") raise - print >>f, "[alias]\nme: %s <%s>" % (name, email) + print("[alias]\nme: %s <%s>" % (name, email), file=f) f.close(); def _UpdateDefaults(parser, config): @@ -233,7 +235,7 @@ def _UpdateDefaults(parser, config): val = config.getint('settings', name) parser.set_default(name, val) else: - print "WARNING: Unknown setting %s" % name + print("WARNING: Unknown setting %s" % name) def _ReadAliasFile(fname): """Read in the U-Boot git alias file if it exists. @@ -258,7 +260,7 @@ def _ReadAliasFile(fname): continue alias[words[1]] = [s.strip() for s in words[2].split(',')] if bad_line: - print bad_line + print(bad_line) def Setup(parser, project_name, config_fname=''): """Set up the settings module by reading config files. @@ -276,7 +278,7 @@ def Setup(parser, project_name, config_fname=''): config_fname = '%s/.patman' % os.getenv('HOME') if not os.path.exists(config_fname): - print "No config file found ~/.patman\nCreating one...\n" + print("No config file found ~/.patman\nCreating one...\n") CreatePatmanConfigFile(config_fname) config.read(config_fname) diff --git a/tools/patman/terminal.py b/tools/patman/terminal.py index af3593e..137265f 100644 --- a/tools/patman/terminal.py +++ b/tools/patman/terminal.py @@ -8,6 +8,8 @@ This module handles terminal interaction including ANSI color codes. """ +from __future__ import print_function + import os import sys @@ -52,9 +54,9 @@ def Print(text='', newline=True, colour=None): if colour: col = Color() text = col.Color(colour, text) - print text, + print(text, end='') if newline: - print + print() else: sys.stdout.flush() @@ -81,11 +83,11 @@ def EchoPrintTestLines(): for line in print_test_list: if line.colour: col = Color() - print col.Color(line.colour, line.text), + print(col.Color(line.colour, line.text), end='') else: - print line.text, + print(line.text, end='') if line.newline: - print + print() class Color(object): diff --git a/tools/patman/test.py b/tools/patman/test.py index e8f7472..8c39f66 100644 --- a/tools/patman/test.py +++ b/tools/patman/test.py @@ -181,7 +181,7 @@ index 0000000..2234c87 elif data_type == 'indent': indent = tab else: - print 'not implemented' + print('not implemented') return data % (signoff, tab, indent, tab) def SetupData(self, data_type):