Convert contrib/mklog script to Python 3

Upstream will drop support for Python 2.x on January 1, 2020.  This
patch converts the contrib/mklog script to Python 3.  The process for
the conversion was as follows.

- Use the futurize tool (https://python-future.org ) to apply the
  print_with_import, except, and dict transformations.

- Remove the "from __future__ import print_function".

- Change the shebang line to search for python3 in the environment.

- Modify the run() function to return a str instead of bytes.

- Update the copyright year.

contrib/ChangeLog:

2019-05-21  Janne Blomqvist  <jb@gcc.gnu.org>

        * mklog: Convert to Python 3.

From-SVN: r271456
This commit is contained in:
Janne Blomqvist 2019-05-21 09:59:16 +03:00
parent cb7ac91b12
commit eaa6a39d42
2 changed files with 16 additions and 12 deletions

View File

@ -1,3 +1,7 @@
2019-05-21 Janne Blomqvist <jb@gcc.gnu.org>
* mklog: Convert to Python 3.
2019-05-03 Jakub Jelinek <jakub@redhat.com> 2019-05-03 Jakub Jelinek <jakub@redhat.com>
* gennews (files): Add files for GCC 9. * gennews (files): Add files for GCC 9.

View File

@ -1,6 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python3
# Copyright (C) 2017 Free Software Foundation, Inc. # Copyright (C) 2017-2019 Free Software Foundation, Inc.
# #
# This file is part of GCC. # This file is part of GCC.
# #
@ -67,7 +67,7 @@ class RegexCache(object):
cache = RegexCache() cache = RegexCache()
def print_help_and_exit(): def print_help_and_exit():
print """\ print("""\
Usage: %s [-i | --inline] [PATCH] Usage: %s [-i | --inline] [PATCH]
Generate ChangeLog template for PATCH. Generate ChangeLog template for PATCH.
PATCH must be generated using diff(1)'s -up or -cp options PATCH must be generated using diff(1)'s -up or -cp options
@ -78,7 +78,7 @@ When PATCH is - or missing, read standard input.
When -i is used, prepends ChangeLog to PATCH. When -i is used, prepends ChangeLog to PATCH.
If PATCH is not stdin, modifies PATCH in-place, otherwise writes If PATCH is not stdin, modifies PATCH in-place, otherwise writes
to stdout. to stdout.
""" % me """ % me)
sys.exit(1) sys.exit(1)
def run(cmd, die_on_error): def run(cmd, die_on_error):
@ -87,7 +87,7 @@ def run(cmd, die_on_error):
(out, err) = proc.communicate() (out, err) = proc.communicate()
if die_on_error and proc.returncode != 0: if die_on_error and proc.returncode != 0:
error("`%s` failed:\n" % (cmd, proc.stderr)) error("`%s` failed:\n" % (cmd, proc.stderr))
return proc.returncode, out, err return proc.returncode, out.decode(), err
def read_user_info(): def read_user_info():
dot_mklog_format_msg = """\ dot_mklog_format_msg = """\
@ -153,9 +153,9 @@ class FileDiff:
self.clname, self.relname = get_parent_changelog(filename); self.clname, self.relname = get_parent_changelog(filename);
def dump(self): def dump(self):
print "Diff for %s:\n ChangeLog = %s\n rel name = %s\n" % (self.filename, self.clname, self.relname) print("Diff for %s:\n ChangeLog = %s\n rel name = %s\n" % (self.filename, self.clname, self.relname))
for i, h in enumerate(self.hunks): for i, h in enumerate(self.hunks):
print "Next hunk %d:" % i print("Next hunk %d:" % i)
h.dump() h.dump()
class Hunk: class Hunk:
@ -167,8 +167,8 @@ class Hunk:
self.ctx_diff = is_ctx_hunk_start(hdr) self.ctx_diff = is_ctx_hunk_start(hdr)
def dump(self): def dump(self):
print '%s' % self.hdr print('%s' % self.hdr)
print '%s' % '\n'.join(self.lines) print('%s' % '\n'.join(self.lines))
def is_file_addition(self): def is_file_addition(self):
"""Does hunk describe addition of file?""" """Does hunk describe addition of file?"""
@ -358,7 +358,7 @@ def main():
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'hiv', ['help', 'verbose', 'inline']) opts, args = getopt.getopt(sys.argv[1:], 'hiv', ['help', 'verbose', 'inline'])
except getopt.GetoptError, err: except getopt.GetoptError as err:
error(str(err)) error(str(err))
inline = False inline = False
@ -388,7 +388,7 @@ def main():
diffs = parse_patch(contents) diffs = parse_patch(contents)
if verbose: if verbose:
print "Parse results:" print("Parse results:")
for d in diffs: for d in diffs:
d.dump() d.dump()
@ -449,7 +449,7 @@ def main():
# Print log # Print log
date = time.strftime('%Y-%m-%d') date = time.strftime('%Y-%m-%d')
for log_name, msg in sorted(logs.iteritems()): for log_name, msg in sorted(logs.items()):
out.write("""\ out.write("""\
%s: %s: